Skip to content

fix(file_editor): insert at EOF of file without trailing newline concatenates onto last line - #4582

Open
ksk2023 wants to merge 1 commit into
OpenHands:mainfrom
ksk2023:fix-file-editor-insert-no-trailing-newline
Open

fix(file_editor): insert at EOF of file without trailing newline concatenates onto last line#4582
ksk2023 wants to merge 1 commit into
OpenHands:mainfrom
ksk2023:fix-file-editor-insert-no-trailing-newline

Conversation

@ksk2023

@ksk2023 ksk2023 commented Aug 23, 2026

Copy link
Copy Markdown

HUMAN:

I manually reproduced the bug on the current main, applied the fix, and ran the file_editor test suite: my four new regression tests fail on main and all pass with the fix, with no new failures versus the baseline.

AGENT:

Issue Number

Fixes #4583

Why

FileEditor.insert silently corrupts a file when asked to insert after the last line of a file that does not end with a newline: the inserted content is concatenated onto the last line instead of starting on its own line. This is a real data-corruption path for agent-driven edits, and the bug was inherited from the original Anthropic computer-use-demo implementation this editor derives from.

Summary

In _execute_insert(), the file is read line-by-line and retained lines are collected up to insert_line. Python's line iterator omits the trailing \n from the last line when the file has no final newline, so new_lines[-1] is e.g. "c", and the new content is appended directly, producing cX\n.

The fix adds a guard right before appending the inserted lines:

if new_lines and not new_lines[-1].endswith("\n"):
    new_lines[-1] = new_lines[-1] + "\n"

Reproducer:

editor = FileEditor()
# file contains "a\nb\nc" (no trailing newline)
editor(command="insert", path=p, insert_line=3, new_str="X")
# Before: "a\nb\ncX\n"   After: "a\nb\nc\nX\n"

How to Test

Added 4 regression tests to tests/tools/file_editor/test_basic_operations.py:

  • test_insert_at_eof_without_trailing_newline — single-line insert at EOF (fails on main, passes with fix)
  • test_insert_multiple_lines_at_eof_without_trailing_newline — multi-line insert at EOF (fails on main, passes with fix)
  • test_insert_at_eof_with_trailing_newline_unchanged — files already ending with \n keep existing behavior (no extra blank line)
  • test_insert_in_middle_unaffected_by_eof_fix — middle-of-file inserts unchanged

Verified on main: 2 failed, 2 passed → with fix: 4 passed. The full file_editor suite shows no new failures compared to baseline (the 9 pre-existing failures on main under Windows — including test_insert_non_utf8_file, which fails identically without this change — are unrelated environment/encoding issues).

When inserting after the last line (insert_line == num_lines) of a file
that does not end with a newline, the retained last line lacks "\n", so
the inserted content gets concatenated onto the last line instead of
starting on its own line.

Before: insert(insert_line=2, new_str="X") on "a\nb\nc" -> "a\nb\ncX\n"
After:  insert(insert_line=2, new_str="X") on "a\nb\nc" -> "a\nb\nc\nX\n"

This adds a missing-newline guard right before appending the inserted
lines, plus regression tests covering: single/multi-line inserts at EOF
without trailing newline, the unchanged behavior for files that already
end with a newline, and middle-of-file inserts.
Copilot AI lite review requested due to automatic review settings August 23, 2026 01:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

👋 This PR needs a couple of things fixed before OpenHands can review it:

  • the PR description's HUMAN: section needs at least 20 characters describing what you tested, not just the template placeholder

Push an update once this is addressed and this check re-runs automatically.

This is an automated check - no AI was used to generate this comment.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

🚦 CI is currently failing on this PR's latest commit.

Please fix the failing checks before OpenHands reviews it - this is re-checked automatically once you push a new commit. (A maintainer can also request @all-hands-bot as a reviewer to have it reviewed regardless of CI status.)

This is an automated check - no AI was used to generate this comment.

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.

[Bug]: file editor insert concatenates onto last line when file has no trailing newline

3 participants