Skip to content

apply_patch turns CRLF files into mixed line endings #45926

Description

@skyzhao1223

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

Activity

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions