fix: preserve CRLF line endings and BOM on Windows file writes#20217
Open
konglingdi1 wants to merge 1 commit intoanomalyco:devfrom
Open
fix: preserve CRLF line endings and BOM on Windows file writes#20217konglingdi1 wants to merge 1 commit intoanomalyco:devfrom
konglingdi1 wants to merge 1 commit intoanomalyco:devfrom
Conversation
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
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #18616
Type of change
What does this PR do?
On Windows, the
write,edit, andapply_patchtools 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: ExportnormalizeLineEndings,detectLineEnding,convertToLineEndingfor 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?
test/tool/write.test.tscovering: CRLF preservation, BOM preservation, combined CRLF+BOM, new file (no spurious CRLF/BOM added), and empty file edge case..batfiles 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