Skip to content

fix: force UTF-8 output encoding for PowerShell on Windows - #49124

Open
ysmla wants to merge 1 commit into
anomalyco:devfrom
ysmla:fix/windows-powershell-utf8
Open

ysmla wants to merge 1 commit into
anomalyco:devfrom
ysmla:fix/windows-powershell-utf8

Conversation

@ysmla

@ysmla ysmla commented Sep 15, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #30205

Related: #30055, #23636

Type of change

  • Bug fix

What does this PR do?

解决Windows下,opencode中模型使用powershell输出中文时会出现乱码的问腿

How did you verify your code works?

在中文 Windows 11 + PowerShell 5.1 环境下,用与工具相同的方式启动 PowerShell(管道输出 stdout),会得到 GBK 编码字节(b2e2 cad4 ...),证实了乱码的原因是编码不匹配,修复后,用重新构建的二进制做端到端测试,让模型执行 echo 测试中文:你好世界——打补丁前输出乱码,打补丁后能正确显示中文。
修复前后对比如下:
origin
fixed

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate PRs Found:

  1. PR fix: set default UTF-8 encoding for spawned subprocess on Windows #31658 - fix: set default UTF-8 encoding for spawned subprocess on Windows
    fix: set default UTF-8 encoding for spawned subprocess on Windows #31658

    • Directly related: addresses the same Windows encoding issue for subprocesses spawned by opencode
  2. PR fix(core): force UTF-8 for Windows shells #36608 - fix(core): force UTF-8 for Windows shells
    fix(core): force UTF-8 for Windows shells #36608

    • Related: appears to be another attempt to fix UTF-8 encoding on Windows shells

These PRs likely cover the same root cause (encoding mismatch when PowerShell outputs on Windows). You should verify if PRs #31658 and #36608 already resolve this issue or if they only partially address it.

Windows PowerShell 5.1 encodes its stdout with the legacy OEM code page
(e.g. CP936 on zh-CN systems) when output is redirected, while opencode
decodes process output as UTF-8, producing mojibake for any non-ASCII
shell output.

Prepend a [Console]::OutputEncoding/$OutputEncoding UTF-8 preamble to
PowerShell commands spawned by the shell tool and bang commands on
Windows. Permission scanning and command titles still use the original
command text since the preamble is injected at spawn time.

Corresponds to upstream discussion in anomalyco#23636.
@ysmla
ysmla force-pushed the fix/windows-powershell-utf8 branch from 042190d to 8d5aa07 Compare September 15, 2026 05:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

在 Window 环境下 AI 使用 PowerShell 修改文件添加中文内容会有乱码出现

1 participant