codex icon indicating copy to clipboard operation
codex copied to clipboard

fix(apply-patch): preserve CRLF line endings on Windows when updating files

Open cnaples79 opened this issue 3 months ago • 8 comments

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

cnaples79 avatar Sep 21 '25 21:09 cnaples79

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

github-actions[bot] avatar Sep 21 '25 21:09 github-actions[bot]

I have read the CLA Document and I hereby sign the CLA

cnaples79 avatar Sep 21 '25 21:09 cnaples79

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 avatar Nov 06 '25 21:11 etraut-openai

@etraut-openai Ill resolve the merge conflicts and comment here once finished! Yes, still interested in this PR! That sounds good. Thank you.

cnaples79 avatar Nov 07 '25 00:11 cnaples79

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 avatar Nov 12 '25 02:11 etraut-openai

@etraut-openai I'd love to see this fixed 😄

sharwell avatar Nov 26 '25 15:11 sharwell

@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:

image

Here's an example of a patch that didn't change line endings:

image

Do you know if this pull request also fixes that issue?

sharwell avatar Nov 26 '25 16:11 sharwell

@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!

dylan-hurd-oai avatar Dec 04 '25 18:12 dylan-hurd-oai

@cnaples79 thanks again for the PR.

etraut-openai avatar Dec 09 '25 19:12 etraut-openai