Skip to content

fix(functions): preserve line endings when applying text edits - #725

Open
DZH166 wants to merge 1 commit into
OpenBMB:mainfrom
DZH166:fix/edit-preserve-line-endings
Open

DZH166 wants to merge 1 commit into
OpenBMB:mainfrom
DZH166:fix/edit-preserve-line-endings

Conversation

@DZH166

@DZH166 DZH166 commented Nov 14, 2026

Copy link
Copy Markdown

Problem

apply_text_edits() writes the edited text with target.write_text(rendered, encoding=used_encoding). Text mode (newline=None) translates every \n in rendered to os.linesep, and the matching read path (path.read_text(...)) already translated the file on the way in, so _detect_newline never sees the real style:

  • a CRLF file is rewritten as \r\r\n (or LF on POSIX);
  • an LF file becomes CRLF on Windows;
  • a CR-only file becomes CRLF;

…while the tool still answers newline: newline_style, claiming the style was preserved. One single-line edit therefore rewrites the line endings of the whole file.

before: LF  b'alpha\nbeta\ngamma\n'      -> b'alpha\r\nBETA\r\ngamma\r\n'
        CRLF reported newline '\n' although the file holds '\r\n'
        CR-only -> CRLF
after : all three edits keep the original bytes; the reported newline matches the file

Fix

Write with newline="", and read the exact bytes in the explicit-encoding branch so the detected newline reflects the file.

Tests

tests/test_file_tool_line_endings.py (4 tests): LF, CRLF and CR files keep their endings through an edit, and the reported newline matches.

pytest tests/test_file_tool_line_endings.py -q    # before: 4 failed   after: 4 passed

apply_text_edits wrote with write_text() in universal-newline mode, so every '\\n' became os.linesep: CRLF files became CR CR LF and LF files became CRLF, while the tool reported newline: preserve. Read and write the exact bytes/newlines.
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.

1 participant