Skip to content

Kimi code内置Edit工具剥掉BOM问题 #3825

Closed

Description

@skywolf

你运行的 Kimi Code 版本是?

0.43.1

你使用的是哪个开放平台/订阅?

Kimi Code (kimi.com/code)

你使用的是哪个模型?

K2.7 Code Highspeed

你的电脑平台是?

Microsoft Windows NT 10.0.19045.0 x64

你遇到了什么问题?

用 Edit(或 Write)修改一个带 BOM 的 UTF-8 文件后,保存出来的文件 BOM 丢失,变成无 BOM UTF-8。每次编辑都发生,不是偶发。

影响(Windows 生态下是真实事故,不是洁癖)

  1. PowerShell 5.1 对 .ps1 的编码判定规则是"有 BOM 按 BOM,无 BOM 按 ANSI(GBK)"。
    BOM 被剥掉后,脚本里的中文注释/字符串被当 GBK 误读,直接报一堆引号/括号解析错误。
    我们项目已多次踩坑,现在被迫给 agent 立规矩"每次 Edit 后手动 sed 补 BOM 并 xxd 验证"。

  2. 项目 .editorconfig 约定 cpp/h 用 UTF-8 BOM(VS/MSVC 中文场景事实标准),
    agent 一次普通编辑就悄悄违反约定,git diff 里还看不出来。

  3. 同类问题还有换行符:CRLF 文件经编辑后行尾风格是否保留也应明确。

复现步骤?

  1. 创建带 BOM 的 UTF-8 文件

      printf '\xef\xbb\xbfhello\n中文\n' > /c/Users/zjj/temp_bom_test/t.txt

    初始状态:

      --- initial bytes ---
      00000000: efbb bf68 656c                           ...hel
      --- file info ---
      .../t.txt: Unicode text, UTF-8 (with BOM) text
    

    前三个字节是 ef bb bf,确认带 BOM。

    1. 用 Edit 工具修改

    把第一行 hello 改成 hello2。

    1. 再次检查文件头
      --- bytes after Edit ---
      00000000: 6865 6c6c 6f32                           hello2
      --- file info ---
      .../t.txt: Unicode text, UTF-8 text
    

    BOM 已消失:前三个字节从 ef bb bf 变成了 68 65 6c(即 hel),file 命令也从 UTF-8 (with BOM) text 变成了 UTF-8 text。

    结论

    当前版本 Kimi Code 的 Edit 工具仍然会剥掉 UTF-8 BOM。复现文件我留在:

期望的行为是什么?

  • Edit/Write 写回文件时检测并保留原文件的 BOM(UTF-8/UTF-16LE/BE)与换行风格;新文件再按 .editorconfig 或配置决定。
  • 如果短期不改,请在工具描述里明确写出"会丢 BOM",免得各项目各自踩坑后才发现。

补充信息

No response

Contribution

  • 我愿意自己提交修复此 bug 的 PR(请先等待维护者在本 issue 中批准)

Activity

  1. added
    bug
    on Sep 16, 2026
  2. dvd233 commented on Sep 16, 2026

    @dvd233

    我已定位根因并准备好了修复,想确认一下方向后再提交 PR。

    根因:FileEditService.edit 读文件走 fs.readText(path, { errors: 'strict' }) → decodeTextWithErrors 里 TextDecoder 的 ignoreBOM 默认 false → BOM 被剥掉;写回时 fs.writeText 直接用解码后的内容,BOM 不会加回。所以编辑带 BOM 的 UTF-8 文件后 BOM 丢失。Windows 下 PowerShell 5.1 / MSVC 依赖 BOM 判定 UTF-8,剥掉后中文会被按 GBK 误读。

    修复(本地已完成红绿测试):fileEditService.ts 在读前先 fs.readBytes(path, 3) 检测 BOM,写回时若原文件有 BOM 则补回 `` 前缀;无 BOM 的文件不受影响。已在 test/app/edit/tools/edit.test.ts 加了两个回归用例(带 BOM 保留 / 无 BOM 不新增)。

    按仓库政策(外部 PR 需维护者 /approve 的 issue),想先确认这个方向是否可接受,以及是否只覆盖 Edit(Write 的 overwrite 语义不同,我暂未动)。如果方向 OK 我就开 PR。

    (声明:我使用 AI 编码助手辅助,以上根因定位与测试均在本人 Windows 机器本地执行验证。)

  3. skywolf commented on Sep 18, 2026

    @skywolf
    Author

    我已定位根因并准备好了修复,想确认一下方向后再提交 PR。

    能解决一半问题也好了。最好也能覆盖Wirte,agent 经常也会用 Write 重写整个脚本。
    突然醒悟,有AI可以自己修改源码了,我按你的方案试试。

  4. added a commit that references this issue on Sep 18, 2026
    1efd6ad
  5. skywolf commented on Sep 18, 2026

    @skywolf
    Author

    @dvd233 我把Editor也修了,先自己用上。 1efd6ad

  6. 11 remaining items

  7. NoX-programer commented on Oct 24, 2026

    @NoX-programer

    补充一下最新情况(2026-10-24):

    1. 最新版 @moonshot-ai/[email protected](2026-10-23 发布)中该问题依然存在。
      本机复现:对带 BOM 的 UTF-8 文件执行任意一次 Edit,BOM 即被剥离,
      其余内容(包括文件末尾无换行的情况)均保持不变。0.42.0 上同样可复现。

    2. 核对了 2.3.1 的 dist/main.mjs 与 main 分支源码:FileEditService.edit
      仍然是 readText({ errors: 'strict' }) → 替换 → writeText 的链路,
      decodeTextWithErrors 中 TextDecoder 的 ignoreBOM 仍为默认值 false,
      读写两侧都没有 BOM 的保留/恢复逻辑。

    3. 之前提到的修复提交 1efd6ad(preserve BOM and line endings when
      Edit/Write modify existing files)目前不在 main 分支上
      (compare 结果为 diverged),也没有关联到任何 PR,未进入任何已发布版本。

    所以想麻烦维护者:能否 review 一下 1efd6ad 并合入,或者基于它开一个正式
    PR?如果需要按仓库规范拆分、补测试或调整实现,我可以配合。

    环境:Windows,@moonshot-ai/[email protected](0.42.0 同样复现)。

    更新:我已按仓库规范开了 PR #4371(含 changeset 与回归测试)。该修复不仅覆盖 UTF-8 BOM,还覆盖全部 5 种带 BOM 的 Unicode 编码(UTF-8/UTF-16LE/UTF-16BE/UTF-32LE/UTF-32BE):Edit 保留原文件的 BOM 与编码;无 BOM 的文件走原有代码路径,行为不变。

  8. AngieOdin commented on Oct 24, 2026

    @AngieOdin

    Confirmed still reproducing on Windows + Kimi Code 2.3.1 (2026-10-24).

    Environment: Windows 11 x64, Kimi Code CLI 2.3.1 (native installer, up-to-date per kimi upgrade).

    Repro (same steps as OP):

    1. printf '\xef\xbb\xbf# 中文註釋測試 BOM\nWrite-Host "hello"\n' > bom-test.ps1 → xxd shows efbb bf23 (BOM present)
    2. Edit tool: change hello → hello2
    3. xxd after Edit: file now starts with 2320 (# ) — BOM stripped

    So the bug persists in 2.3.1. Impact for us matches OP exactly: PowerShell 5.1 reads BOM-less .ps1 as ANSI/GBK → Chinese comments mojibake → parse errors. We currently mitigate with a team rule (agents forbidden from editing BOM files directly; BOM re-added + xxd-verified after any edit), but a proper fix (preserve BOM and line-ending style on write-back) would be much appreciated.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions