fix: preserve BOM in text tool round-trips#23797
Merged
Hona merged 6 commits intoanomalyco:devfrom Apr 22, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces BOM-aware read/write helpers and applies them across the write, edit, and apply_patch tools to preserve UTF-8 BOMs on disk while keeping BOMs out of visible diffs/patch previews, along with regression tests.
Changes:
- Add
util/bom.tsto split/join BOMs, read files while preserving BOM state, and re-sync BOM after formatting. - Update
write,edit, andapply_patchflows to compute diffs on BOM-stripped text and to restore BOMs after formatter runs. - Add regression tests covering overwrite/edit/apply-patch scenarios involving BOM-prefixed files.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/opencode/src/util/bom.ts | New shared BOM utilities (split/join/read/sync). |
| packages/opencode/src/tool/write.ts | Preserve BOMs during write + after formatting, and keep BOM out of diff. |
| packages/opencode/src/tool/edit.ts | Preserve BOMs for in-place edits and after formatting (with a couple of ordering/overwrite concerns). |
| packages/opencode/src/tool/apply_patch.ts | Track BOM per file change and restore BOM after formatting so patches don’t invent first-line diffs. |
| packages/opencode/src/patch/index.ts | Carry BOM state through patch-derived updates (but BOM detection depends on read semantics). |
| packages/opencode/test/tool/write.test.ts | Regression test: overwriting existing BOM file preserves BOM. |
| packages/opencode/test/tool/edit.test.ts | Regression test: first visible line edit in BOM file works and diff omits BOM. |
| packages/opencode/test/tool/apply_patch.test.ts | Regression test: patch preview doesn’t show invented first-line diff; BOM preserved after apply. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
erfianugrah
pushed a commit
to erfianugrah/opencode
that referenced
this pull request
Apr 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes THIS:
after
no weird BOM rendering! :)

Summary
write,edit, andapply_patchwithout leaking the BOM into visible diff or edit textTesting