Skip to content

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

Closed as not planned

Description

@jianbinJava

Description

在 Window 环境下 AI 使用 PowerShell 修改文件添加中文内容会有乱码出现, 原因是 Windows下的 PowerShell 默认编码为 GBK 导致。每次都要提醒,挺麻烦的, 望紧急修复。

Plugins

No response

OpenCode version

No response

Steps to reproduce

  1. 输入提示词,叫修改功能,
  2. AI 自动修改完代码后, 把原来的文内容替换后,就变成乱码出现了。

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Activity

  1. github-actions commented on Jun 1, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    Please review those issues and consider adding your details there if they cover the same problem.

  2. realsharpswing commented on Jun 1, 2026

    @realsharpswing

    同样遇到这个问题。我让opencode写了个 OpenCode 插件自动检测 GBK 文件并转换为 UTF-8,实测可行,分享出来:
    https://github.com/realsharpswing/opencode-encoding-guard
    原理是拦截 read 和 edit 工具的 hook,编码检测后自动修正,对 LLM 透明。希望对你有帮助。
    PowerShell对话过程中也会显示乱码, 让opencode自己查找方案在agents.md中添加规则也解决了. 你可以试试让opencode自己分析提供方法.

  3. Astro-Han commented on Jun 3, 2026

    @Astro-Han

    The related issues here are all about garbled output (reading a child process's stdout back through a CP936 decoder). What you're hitting is different: the file on disk is actually written with the wrong encoding, so chcp 65001 and [Console]::OutputEncoding don't help.

    The cause is PowerShell's default write encoding, not the console code page:

    • Out-File, >, Set-Content, and Add-Content in Windows PowerShell 5.1 default to UTF-16LE or the legacy ANSI page (CP936 on zh-CN), so Chinese text gets mangled the moment it's written.
    • PowerShell 7 defaults these to UTF-8 without BOM, which is usually what you want.
    • Set-Content -Encoding utf8 looks like a fix but on 5.1 it writes a BOM, which then breaks some toolchains; on 7 the same flag writes no BOM.

    Two reliable ways to pin it down when the agent shells out:

    $PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
    $PSDefaultParameterValues['Set-Content:Encoding'] = 'utf8'
    $OutputEncoding = [System.Text.UTF8Encoding]::new($false)

    Or just have the agent write files through the editing tool / Node fs rather than redirecting PowerShell output, which sidesteps the per-cmdlet encoding entirely. The underlying trigger is still the ACP being 936, but for the file-content case the knob is the cmdlet's -Encoding, not the console.
    In PawWork we sidestepped this entirely by routing all file writes through Node fs with explicit UTF-8, never through shell redirection — one less platform-dependent knob to worry about.

  4. github-actions commented on Aug 4, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions