Skip to content

feat(tool): support non-utf8 file encodings - #49881

Closed
MasEne1 wants to merge 1 commit into
anomalyco:devfrom
MasEne1:file-encoding
Closed

MasEne1 wants to merge 1 commit into
anomalyco:devfrom
MasEne1:file-encoding

Conversation

@MasEne1

@MasEne1 MasEne1 commented Sep 19, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #45924

Type of change

  • New feature
  • Bug fix
  • Refactor / code improvement
  • Documentation

What does this PR do?

edit, write, apply_patch and the patch engine decoded every file as UTF-8 with a non-fatal decoder, so a GBK/Shift-JIS/Big5 file turned into U+FFFD garbage and the corruption was written back permanently.

This adds per-read encoding detection (BOM for UTF-8/UTF-16LE/UTF-16BE, then UTF-8 validity, then a new configurable fallback file_encoding in opencode.json, default utf-8 so default behavior is unchanged) in a shared util/encoding module, wired into edit/write/read/apply_patch and the patch engine. Files are written back in the encoding they were loaded with, and new files are created in the fallback encoding. Non-UTF-8 decoding goes through iconv-lite, lazily imported only when needed.

Related: #45925 rejects non-UTF-8 files in the v2 core tools. This PR is complementary: it makes the v1 tools encoding-aware so legacy files stay editable.

How did you verify your code works?

  • New test/tool/encoding.test.ts (13 tests): GBK edit/write/read round-trips verified at the byte level, UTF-16LE/BE BOM round-trips, UTF-8 BOM preserved, file left byte-identical when an edit does not match.
  • bun test test/tool/ test/patch/ from packages/opencode — no new failures (the 4 Windows path-normalization failures also fail on vanilla dev).
  • bun typecheck passes for packages/opencode and packages/core.

Screenshots / recordings

Not a UI change.

Checklist

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

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Sep 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Sep 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@thdxr

thdxr commented Oct 10, 2026

Copy link
Copy Markdown
Member

This branch has been upgraded to V2. We're no longer taking PRs for V1. If you think this is still relevant, please port it over and open it for V2.

— from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

@thdxr thdxr closed this Oct 10, 2026
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.

edit and apply_patch silently corrupt non-UTF-8 files (U+FFFD replacement)

2 participants