Skip to content

fix: preserve CRLF line endings and BOM on Windows file writes#20217

Open
konglingdi1 wants to merge 1 commit intoanomalyco:devfrom
konglingdi1:fix/windows-crlf-line-endings
Open

fix: preserve CRLF line endings and BOM on Windows file writes#20217
konglingdi1 wants to merge 1 commit intoanomalyco:devfrom
konglingdi1:fix/windows-crlf-line-endings

Conversation

@konglingdi1
Copy link
Copy Markdown

@konglingdi1 konglingdi1 commented Mar 31, 2026

Issue for this PR

Closes #18616

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

On Windows, the write, edit, and apply_patch tools silently convert CRLF (\r\n) line endings to LF (\n) when creating or overwriting files. This causes batch files and other Windows-specific files to fail when executed in CMD/PowerShell, especially when outputting non-English text.

The fix detects the original file's line ending style and BOM before writing, then preserves them in the output:

  • src/tool/edit.ts: Export normalizeLineEndings, detectLineEnding, convertToLineEnding for reuse by other tools.
  • src/tool/write.ts: When overwriting an existing file, detect and preserve its line ending style and BOM.
  • src/tool/apply_patch.ts: Preserve BOM after applying patches.
  • src/patch/index.ts: Normalize CRLF to LF during patch processing, then rejoin with the original line ending style.

How did you verify your code works?

  • Added 5 unit tests in test/tool/write.test.ts covering: CRLF preservation, BOM preservation, combined CRLF+BOM, new file (no spurious CRLF/BOM added), and empty file edge case.
  • Manually tested on Windows 11 by creating .bat files with CRLF endings, editing them via OpenCode, and confirming they retained CRLF format and executed correctly in CMD/PowerShell.

Screenshots / recordings

N/A - no UI changes.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

When overwriting existing files on Windows, the write, edit, and apply_patch tools now detect the original file's line ending style (CRLF vs LF) and BOM marker, preserving them in the output. This prevents batch files and other Windows-specific files from being silently converted to Unix (LF) format, which caused execution failures in CMD/PowerShell.

Fixes anomalyco#18616
@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Mar 31, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

The issue of being unable to create files in Windows (CR LF) format

1 participant