opencode icon indicating copy to clipboard operation
opencode copied to clipboard

shell: PowerShell output shows U+FFFD for every non-ASCII character on Windows

Open adgarboc opened this issue 1 month ago • 5 comments

Description

Commands that print non-ASCII text through shell come back with � instead of the characters. Not limited to CJK: accented Latin (á é í ó ú ñ ¿ ¡ ü) breaks the same way, so it shows up on es-* Windows locales.

shell runs pwsh 7.6.7 (no shell key in config). The spawned process has no attached console, so [Console]::OutputEncoding stays at the OEM code page (ibm437 here) and PowerShell encodes stdout with it. The output is then decoded as UTF-8, so every non-ASCII byte becomes U+FFFD.

Does not help: assigning [Console]::OutputEncoding = [Text.UTF8Encoding]::new($false) (silently ignored, no console to set), and chcp 65001 ([Console]::OutputEncoding keeps the value cached from startup).

Writing raw UTF-8 bytes to stdout works, so the pipe itself is fine: [Console]::OpenStandardOutput().Write([Text.Encoding]::UTF8.GetBytes("áéíóú")).

Related closed issues: #23636, #31345, #31187, #36985, #30055.

Steps to reproduce

  1. "accented: áéíóú ñÑ ¿¡ ü" -> accented: ����� �� �� �
  2. [Console]::OutputEncoding -> ibm437; [Console]::CursorVisible throws, so there is no console attached.
  3. Control: write the same text as raw UTF-8 bytes with [Console]::OpenStandardOutput().Write(...) -> renders correctly.

Plugins

engram

OpenCode version

2.0.31

Operating System

Windows 11 (NT 10.0.26300.0)

Terminal

Orca (TERM_PROGRAM=Orca, TERM=xterm-256color)

adgarboc avatar Oct 17 '26 03:10 adgarboc

Thanks for the detailed report and the raw-bytes control case, which narrow this down a lot.

I couldn't run a reproduction here because the sandbox runs Linux, where pwsh writes UTF-8 by default and the mismatch never happens. What I could check is the code on the latest dev (512165c): PowerShell is still started as -NoLogo -NoProfile -NonInteractive -Command <command> with no output-encoding setup, and the captured output is still decoded as UTF-8. Nothing on dev changes what you describe, so this needs a maintainer on Windows to confirm it.

Related:

  • #54511: open PR that prepends [Console]::OutputEncoding = UTF8 to PowerShell commands. Your note that this assignment is silently ignored without an attached console matters when testing that PR.
  • #53038: earlier (v1) report of the same code-page vs. UTF-8 mismatch.
  • #48439, #23636, #31345, #36985: earlier reports of the same problem and attempted fixes.

opencode-agent[bot] avatar Oct 17 '26 03:10 opencode-agent[bot]

Confirmation on pwsh 7.7.0 — the in-session encoding assignment works on the pty path

Environment: opencode v2.3.0 (latest), Windows 10 Home 19045 (en-US, ACP 1252 / OEMCP 437), pwsh 7.7.0 (Microsoft Store package), "shell" set to the WindowsApps pwsh alias, no plugins.

Reproduction through the shell tool:

  1. 'áéíóú ñÑ ü' → every non-ASCII character becomes U+FFFD, as reported.
  2. [Console]::OutputEncoding.WebName → ibm437 (InputEncoding too); $OutputEncoding is utf-8.
  3. [Console]::OutputEncoding = [Text.Encoding]::UTF8; 'áéíóú ñÑ ü' → round-trips intact. The assignment is not ignored here, unlike the 7.6.7 no-console case in the original report: the pty path has a console, so the setter takes effect.
  4. chcp 65001 does not persist — each command is a fresh pwsh -NoLogo -NoProfile -NonInteractive -Command ... process.

This supports the prefix approach in #58968 / #60999 on 7.7.0: the assignment fixes output for the whole command, native children included. #60999's en-US IBM437 case matches exactly.

Baker134 avatar Nov 18 '26 13:11 Baker134

Additional data from zh-CN Windows (cp936) — and a refinement of the "assignment silently ignored" diagnosis

Environment: opencode v2.3.2 (npm, latest), Windows 11 (10.0.26200), zh-CN console codepage 936, pwsh 7.7.0 (Store/MSIX), "shell" set to the WindowsApps alias, no plugins. Same U+FFFD behavior as reported.

Three findings that look useful for the prefix PRs (#54511 / #58968 / #60999):

1. The host writer locks its encoding at first write — this, not console absence, explains the "silently ignored" assignment.

[Console]::OutputEncoding = [Text.Encoding]::UTF8; Write-Output "中文"                          # → clean
Write-Output "ascii"; [Console]::OutputEncoding = [Text.Encoding]::UTF8; Write-Output "中文"    # → still mojibake

The original report attributes the ineffective assignment to having no console. Our process has no console either ([Console]::CursorVisible throws, same as reported) — yet the assignment works whenever it precedes all output. The no-console case does honor the assignment; what makes it look ignored is any output (even pure ASCII, even a stray error record) occurring before it. Practical consequence for testing the PRs: the assignment must be prepended before everything, and a test run that emits output first will spuriously look like a failure.

2. Input via argv is intact. Chinese literals inside the -Command string arrive correctly: '你好' -eq (-join [char]0x4F60, [char]0x597D) → True. Only stdout encoding is affected, so the fix only needs to handle output.

3. On zh-CN systems, non-ASCII arrives uninvited. System-generated error text is itself localized (Get-Item C:\missing → 找不到路径), so even ASCII-only commands can emit non-ASCII. A conditional prefix strategy ("only when non-ASCII is expected") will miss these cases — the prefix needs to be unconditional.

Workaround in use here: a global ~/.config/opencode/AGENTS.md instructs every session to prepend the assignment; verified across fresh processes (each shell call spawns a fresh -NoLogo -NoProfile -NonInteractive -Command process).

zzk364277889-eng avatar Nov 21 '26 09:11 zzk364277889-eng

ru-RU data (OEM 866): still present in v2.4.0, and MCP stderr is decoded the same way

Environment: opencode 2.1.0 to 2.3.2 (npm), Windows 11 (10.0.26200), ru-RU, OEM code page 866, pwsh 7.7.0 from the default shell selection (no shell key in the global config).

  1. shell: on this machine 427 of 4,759 shell results this month came back with runs of U+FFFD. PowerShell's own error text, for example (2.3.2):
    Get-ChildItem: �� 㤠���� ���� ���� "<path>", ��᪮��� �� �� �������.
    
  2. v2.4.0 (released today) still starts PowerShell with -NoLogo -NoProfile -NonInteractive -Command <command> (select.ts#L174), and a code search finds no OutputEncoding / UTF8Encoding setup, so #58968 / #60999 are still needed. I checked this by reading the code, not by re-running 2.4.0.
  3. MCP: stdio.ts#L177-L185 reads a local server's stderr with Stream.decodeText() (UTF-8) and appends its tail to the exit error, so the reason a server failed to start is lost too. From opencode.log (2.1.0, 2026-10-27; 41 such lines for three servers on 2.1.0 and 2.2.0, 10-27..10-30):
    message="mcp transport error" server=playwright-extension error="MCP server process exited with code 1: �� 㤠���� �믮����� 㪠������ �ணࠬ��."
    
    That is the Windows message «Не удается выполнить указанную программу.» in cp866. The PowerShell prefix in the PRs does not reach this path. One option: decode stderr as UTF-8 when it is valid, and fall back to the OEM code page when it is not.

igorsaevets avatar Nov 23 '26 09:11 igorsaevets

Fresh data from v2.4.3, Windows 10 19045 (ru-RU, OEM 866), pwsh 7.6.7 — and the "assignment is silently ignored" part looks version-specific: in this build setting [Console]::OutputEncoding inside the command works.

Default (what shell gives the model):

Write-Output "Привет мир — тест"
# → �ਢ�� ��� - ���
[Console]::OutputEncoding.WebName   # cp866
$OutputEncoding.WebName             # utf-8

Same command with the assignment as its first statement:

[Console]::OutputEncoding = [Text.Encoding]::UTF8
Write-Output "Привет мир — тест"
# → Привет мир — тест
[Console]::OutputEncoding.WebName   # utf-8

Notes from the same machine:

  • Native commands (git, gh, node) are fine either way — they emit UTF-8 bytes themselves; only text produced by PowerShell itself (cmdlets, Get-Content, error records) goes through the console encoding and gets mangled.
  • This is not cosmetic: a plain Get-ChildItem C:\nope comes back to the model as ��� 㤠���� ����..., and every agent reading a ru-RU Windows box gets unreadable tool output.
  • We did not need chcp 65001; the assignment alone flipped WebName to utf-8 and the output came back correct.

Minimal fix candidate: when spawning pwsh for shell, prepend [Console]::OutputEncoding = [Text.Encoding]::UTF8; to the command string (or wrap with -Command "[Console]::OutputEncoding=[Text.Encoding]::UTF8; <command>"), leaving the existing UTF-8 decode of stdout as is.

Repro is two commands, no plugins, default config (shell key absent). Happy to test a build if it helps.

ZxcPap14 avatar Dec 01 '26 09:12 ZxcPap14