Describe the bug
apply_patch has no CRLF handling. Patch.derive splits the file on \n, so a CRLF file's lines keep their trailing \r. The model supplies LF context lines (it only ever sees LF from read), so exact matching fails and seek falls back to rstrip (trimEnd()), which matches by stripping the \r. The hunk is then spliced back using the model's LF lines, replacing the CRLF line endings only on the touched lines.
Result: a consistently-CRLF file becomes a mixed-ending file after any update.
Repro: derive("f", [{ oldLines: ["foo"], newLines: ["FOO"] }], "foo\r\nbar\r\n") → "FOO\nbar\r\n".
This is the apply_patch counterpart of the edit mixed-line-ending issue (#45880); the edit fix (#45888) did not cover the patch/apply_patch path.
Suggested fix
Detect the file's dominant line ending and preserve per-line endings when splicing the hunk (or normalize the touched region consistently), mirroring the edit tool.
Environment
- opencode version: latest dev
Describe the bug
apply_patchhas no CRLF handling.Patch.derivesplits the file on\n, so a CRLF file's lines keep their trailing\r. The model supplies LF context lines (it only ever sees LF from read), so exact matching fails andseekfalls back torstrip(trimEnd()), which matches by stripping the\r. The hunk is then spliced back using the model's LF lines, replacing the CRLF line endings only on the touched lines.Result: a consistently-CRLF file becomes a mixed-ending file after any update.
Repro:
derive("f", [{ oldLines: ["foo"], newLines: ["FOO"] }], "foo\r\nbar\r\n")→"FOO\nbar\r\n".This is the
apply_patchcounterpart of the edit mixed-line-ending issue (#45880); the edit fix (#45888) did not cover the patch/apply_patch path.Suggested fix
Detect the file's dominant line ending and preserve per-line endings when splicing the hunk (or normalize the touched region consistently), mirroring the edit tool.
Environment