Skip to content

fix(core): strip BOM from AI-generated content - #381

Closed
moizlatif7 wants to merge 1 commit into
devfrom
fix-bom-strip-ai-content
Closed

fix(core): strip BOM from AI-generated content#381
moizlatif7 wants to merge 1 commit into
devfrom
fix-bom-strip-ai-content

Conversation

@moizlatif7

Copy link
Copy Markdown

Summary

Fixes #368 by stripping UTF-8 BOMs from AI-generated content while preserving BOMs in existing files.

Problem

On Windows systems, OpenCode was adding UTF-8 BOMs (0xEF 0xBB 0xBF) to newly created files when the AI's content contained a BOM character (\uFEFF). This broke Linux scripts because the shebang (#!/bin/bash) must be the very first bytes in the file.

The logic was: desiredBom = source.bom || next.bom

  • source.bom: Does existing file have BOM?
  • next.bom: Does AI content have BOM? ← This was the problem

Solution

Changed BOM handling to: desiredBom = source.bom

This means:

  • ✅ Existing files with BOMs keep their BOMs (preserves round-trips)
  • ✅ New files never get BOMs (fixes Windows issue)
  • ✅ AI-generated BOMs are stripped (fixes BOM in generated File Bug #368)

Changes

Modified Files (6 packages):

  1. packages/core/src/file-mutation.ts - Core V2 write logic
  2. packages/core/src/patch.ts - Patch derivation
  3. packages/core/src/tool/edit.ts - Core edit tool
  4. packages/opencode/src/tool/write.ts - Write tool
  5. packages/opencode/src/tool/edit.ts - Edit tool (2 locations)
  6. packages/opencode/src/patch/index.ts - Patch application

Test Updates:

  • Updated existing test: "preserves BOM from existing files but strips BOM from new content"
  • Added new test: "strips BOM from AI-generated content to fix Issue BOM in generated File Bug #368"

Testing

✅ All existing BOM-related tests pass
✅ New test verifies BOMs are stripped from AI content
✅ Full core test suite: 1075 tests pass
✅ Tool test suite: 335 tests pass
✅ Format/Patch tests: 31 tests pass

Verification

To verify the fix works:

  1. Create a new file with AI-generated content containing BOM
  2. Verify the file does NOT have BOM bytes at the start
  3. Existing files with BOMs should preserve them on edit

Closes #368

Changes the BOM preservation logic to only preserve BOMs from existing
files, never from new AI-generated content. This fixes Issue #368 where
Windows systems were adding UTF-8 BOMs to new files, breaking Linux
scripts with shebangs.

Changes:
- Modified writeTextPreservingBom to strip BOM from new content
- Updated 6 files across core and opencode packages
- Changed logic from `source.bom || next.bom` to `source.bom`
- Added test case to verify BOMs are stripped from AI content
- Updated existing test to reflect new behavior

Behavior:
- Existing files with BOMs: BOM preserved (round-trip safe)
- New files with BOM in content: BOM stripped (fixes #368)
- AI-generated content with BOM: BOM stripped (fixes Windows issue)

Fixes #368
Copilot AI review requested due to automatic review settings July 29, 2026 21:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@moizlatif7

Copy link
Copy Markdown
Author

Closing this PR to redo the fix using proper ANR workflow with ANR change markers. Will reopen with correct branch naming (anr/368/*) and all required ANRCODE_CHANGE markers.

@moizlatif7 moizlatif7 closed this Jul 30, 2026
@moizlatif7
moizlatif7 deleted the fix-bom-strip-ai-content branch July 30, 2026 21:10
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.

BOM in generated File Bug

2 participants