Skip to content

fix(core): preserve line endings in patch tool - #58324

Closed
rekram1-node wants to merge 1 commit into
devfrom
patch-line-endings
Closed

rekram1-node wants to merge 1 commit into
devfrom
patch-line-endings

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Closes #45926
Closes #54605

Problem

The patch tool rewrote every line it touched with \n, so a CRLF file ended up with mixed endings after any update.

 file:   foo\r\n bar\r\n      patch: foo -> FOO
-before: FOO\n   bar\r\n
+after:  FOO\r\n bar\r\n

Change

line the patch didn't touch        -> kept byte for byte
line marked as context (" x")      -> kept byte for byte
changed or added line              -> file's first line ending (LF if none)
unterminated last line             -> gets the file's first line ending

Uniform files stay uniform, and mixed files stay mixed outside the edited lines.

To keep context lines verbatim, the parser now records which old/new lines are context (contextIndices on each chunk). The ACP preview, which reverses chunks, swaps those indices too.

Out of scope

  • A lone \r is still part of the line text, matching how Read shows the file.
  • New files from *** Add File are still written with LF.

Builds on #45931 and #56128, both authors are credited as co-authors.


Added by the opencode bot (AI-generated).

Public API: adds optional contextIndices to UpdateFileChunk in @opencode/util/patch (backwards compatible)

Co-authored-by: pengzh1 <pengzh1@qq.com>
Co-authored-by: Parker Fawcett <259203091+Parker-Fawcett@users.noreply.github.com>

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. I reproduced #54605 on the base with the simulated model: after patching a CRLF file, the whole hunk came back LF while the untouched line kept CRLF. With this change every line keeps \r\n, and the TUI diff shows only the line that actually changed. The patch tests in packages/core pass, and tsc is clean for packages/util and packages/cli.

Public API: UpdateFileChunk in @opencode/util/patch gets an optional contextIndices field. This is backwards compatible: chunks built without it behave as before, apart from the line-ending fix. No SDK, HTTP, plugin or config surface changes.

No blocking concerns.

Before

After

@EgilSandfeld

Copy link
Copy Markdown

I tested this PR's diff at 9d5a0ff on the OpenCode 2.2.7 release source (5f76acb), Windows x64.

The core/CLI typechecks and all 136 focused patch/edit/write/file-mutation tests passed. I also built a private native host and exercised real patch calls: uniform CRLF with UTF-8 BOM, LF without BOM, multi-file update/rename/delete, and a linked child-session edit worked.

The context tracking is valuable: a mixed file's unchanged CRLF context survives. For:
first\r\nOLD\nlast\r\n
with first/last included as patch context, I get:
first\r\nNEW\r\nlast\r\n

That matches the documented first-ending policy, but my stricter use case needs the replaced line to retain its original LF:
first\r\nNEW\nlast\r\n

Missing final newline is also still normalized. Is retaining an existing replaced line's ending, with the first-ending fallback only for new lines, something this PR should cover, or intentionally separate work?

I haven't treated this as a formatter fix: a real external formatter emitting LF can still override CRLF afterwards. #61416 addresses the separate BOM/stale-result problem after partial formatter failure.

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

apply_patch turns CRLF files into mixed line endings Opencode v2: Tool Patch messes up line endings in Windows

2 participants