Skip to content

fix: preserve UTF-8 BOM when reading/writing files #35071

Description

@Ivaloz

Description

When editing files with UTF-8 BOM (Byte Order Mark), the BOM is stripped after save. This corrupts files that require BOM encoding (e.g., some Windows batch files, PowerShell scripts, or files from certain editors).

Root Cause

Effect's FileSystem.readFileString uses TextDecoder which strips BOM by default. When the file is written back, the BOM is lost because the content string no longer contains it.

Proposed Fix

In packages/shared/src/filesystem.ts, wrap
eadFileString and writeFileString to detect and preserve BOM:

  1. On read: Check if raw bytes start with \xEF\BB\BF, if so prepend BOM character to returned string
  2. On write: Check if original file had BOM or content starts with BOM, ensure BOM is preserved

MiMo Code Reference

This fix has been implemented in MiMo Code PR #1520: XiaomiMiMo/MiMo-Code#1520

Environment

  • OpenCode version: 1.17.8
  • OS: Windows 11
  • Terminal: PowerShell

Steps to Reproduce

  1. Create a UTF-8 file with BOM: \�cho -ne '\xEF\xBB\xBFHello' > test.txt\
  2. Open the file in OpenCode and make any edit
  3. Save the file
  4. Verify BOM is lost: \xxd test.txt | head -1\ shows no BOM prefix

Activity

yxl-lab commented on Aug 10, 2026

@yxl-lab

Re-tested this on current dev at 550d1ffd.

The BOM round-trip behavior is covered by merged PR #23797. I ran:

bun test test/tool/edit.test.ts test/tool/write.test.ts test/tool/apply_patch.test.ts -t BOM

All 4 focused BOM tests pass, covering write, edit, apply_patch, and formatter behavior. This looks resolved on current dev. Can this issue be closed?

neriousy commented on Oct 1, 2026

@neriousy
Member

In OpenCode v2, the edit and write tools preserve a UTF-8 BOM. A file that starts with a BOM keeps it after the agent edits or rewrites it.

Fixed in #30632 (write) and #39564 (edit).

Closing as completed. If something doesn't work on v2, please open a new issue.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions