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

Open Beginner friendly
#35,071 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
Half a day
Newbie friendliness
72/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
typescript
Domain
tooling

Research direction

Start in packages/shared/src/filesystem.ts by reading readFileString and writeFileString, then reproduce the issue with a UTF-8 BOM file as described. Done means reading retains the BOM and saving the file preserves it, including when the content already starts with a BOM.

Written by the indexing model from the issue text.

Description

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: https://github.com/XiaomiMiMo/MiMo-Code/pull/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
Dominant language
TypeScript
Stars
209k
Forks
27.5k
Avg merge
7h 20m
Merged PRs (30d)
358

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from anomalyco/opencode

All issues in anomalyco/opencode

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.