fix(apply-patch): preserve CRLF line endings on Windows when updating files
Summary
Patched files on Windows could end up with mixed CRLF/LF endings when applying Update File hunks. This change detects CRLF in the original file, normalizes lines for matching, and re-emits updated content using the original EOL style.
Rationale
Issue #4003 reports mixed EOLs after patch application on Windows. split/join used unconditionally, which produced LF for newly inserted lines even when the original file used CRLF.
Changes
- Detect CRLF by checking for in original contents.
- Strip trailing per line for diffing/matching.
- Join updated lines with if original used CRLF; otherwise join with .
- Add a test to ensure CRLF preservation on update.
Testing
- New unit test writes a CRLF file, applies an update, and asserts CRLF-only output.
- Existing tests pass locally in similar code paths; CI will validate full suite.
Fixes #4003
All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.
I have read the CLA Document and I hereby sign the CLA
Thanks for the contribution, and apologies for the slow reply. We received many PRs, and we're just now catching up on the backlog.
It looks like the PR has gone stale. If you're still interested in pursuing the PR, could you please resolve the merge conflicts? Thanks!
I haven't reviewed the change yet, but once it's in a good state, either I or one of my colleagues will review it.
@etraut-openai Ill resolve the merge conflicts and comment here once finished! Yes, still interested in this PR! That sounds good. Thank you.
We received another PR that addresses this issue, so I'm going to close this one. Thanks again for taking the time to submit this PR.
@etraut-openai I'd love to see this fixed 😄
@cnaples79 In addition to mixed line endings, there is an interesting anomaly that occurs when using the VS Code extension, where a patch involving multiple files fails to show its content. Here's an example of a patch that involved a line ending change:
Here's an example of a patch that didn't change line endings:
Do you know if this pull request also fixes that issue?
@cnaples79 as an FYI, I'm iterating on this PR in #7515 so that we can introduce this fix in a controlled manner. We appreciate your contribution here, and welcome thoughts on the other PR. You'll be credited for your work in the Release Notes once it is merged!
@cnaples79 thanks again for the PR.