Repository navigation
fix(core): preserve line endings in patch tool - #58324
rekram1-node wants to merge 1 commit into
Conversation
Co-authored-by: pengzh1 <pengzh1@qq.com> Co-authored-by: Parker Fawcett <259203091+Parker-Fawcett@users.noreply.github.com>
There was a problem hiding this comment.
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.
|
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: That matches the documented first-ending policy, but my stricter use case needs the replaced line to retain its original LF: 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. |
|
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:
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. |


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.Change
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 (
contextIndiceson each chunk). The ACP preview, which reverses chunks, swaps those indices too.Out of scope
\ris still part of the line text, matching how Read shows the file.*** Add Fileare 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
contextIndicestoUpdateFileChunkin @opencode/util/patch (backwards compatible)