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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions