Skip to content

fix(edit): keep a file's own line endings - #60

Closed
goranai wants to merge 1 commit into
he-yufeng:mainfrom
goranai:fix/edit-file-line-endings
Closed

goranai wants to merge 1 commit into
he-yufeng:mainfrom
goranai:fix/edit-file-line-endings

Conversation

@goranai

@goranai goranai commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Closes #59

edit_file read and wrote with read_text/write_text. Those translate line endings by platform, so one edit rewrote every line ending in the file.

Now it reads with newline="". It matches on LF text, the same text as before, which is also what read_file shows.

  • A one-line edit (no line break in old_string or new_string) is applied to the file as read. Every other byte stays as it was, mixed endings included.
  • An edit that adds or removes line breaks writes the file with its most common line ending (LF, CRLF or CR). A file with no line break at all keeps today's behavior (os.linesep).
  • \r\n in old_string or new_string is read as \n. A CRLF old_string now matches, and a CRLF new_string is no longer written as \r\r\n on Windows.

Behavior changes, all of them:

  • LF files stay LF on Windows. CRLF files stay CRLF on Linux and macOS. CR-only files stay CR.
  • A multi-line edit to a mixed-ending file writes the whole file with its most common ending. Before, it was written with the platform ending.
  • old_string with \r\n used to give "not found". Now it matches the text as LF. The uniqueness check is unchanged.
  • A lone stray CR inside an LF file counts as a line break in a multi-line edit, the same as before.
  • Matching, the "not found" preview and the returned diff use the same LF text as before.
  • README.md, README_CN.md, article/00-index*.md and pyproject.toml: engine 1,496 lines, 3,292 physical, 2,716 net, edit.py 115 lines, 356 offline tests.

Tests:

  • Three new tests in tests/test_tools.py: CRLF kept with LF and CRLF old_string, LF kept with a multi-line new_string, and a one-line edit leaving mixed endings alone. On main on Windows all three fail. With the fix they pass.
  • Full suite on Windows, Python 3.14: main 353 passed, 1 skipped. This branch 356 passed, 1 skipped.
  • ruff check corecoder tests passes.
  • Two Hypothesis runs of 3000 generated files each (LF, CRLF, CR and mixed, with and without a trailing newline, unicode) compared main with this branch. Every edit main applied, this branch also applies. The text is the same, except where new_string held \r\n and main wrote \r\r\n. A file with one line ending style keeps it. A one-line edit changes no byte outside the replaced text. 58 and 66 edits that main refused now apply. In the second run I checked them: each had \r\n in old_string.
  • I have not run it on Linux or macOS, and I have not seen the repo CI run on this change.

Validated with Claude Opus 5.5.

read_text/write_text translate line endings by platform, so one edit
turned an LF file into CRLF on Windows and a CRLF file into LF on
Linux. Read with newline="", match on LF text as before, and write
the file's own ending back. A one-line edit leaves every other byte
as it was.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@he-yufeng

Copy link
Copy Markdown
Owner

Carried forward in #66. The original commits and validation are preserved, current main is merged, and the full CI matrix is running there. Closing this PR in favor of that updated branch.

@he-yufeng he-yufeng closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

edit_file rewrites every line ending of the file it edits (LF to CRLF on Windows, CRLF to LF on Linux/macOS)

2 participants