OpenHands / OpenHands/software-agent-sdk

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

Open Beginner friendly
#4,583 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority:medium ready-for-dev release-note-required tools
Dominant language
Python
Stars
1.1k
Forks
539
Avg merge
1d 19h
Merged PRs (30d)
137

Description

Bug Description

The insert command of the file editor tool concatenates the inserted content onto the last line instead of starting a new line, whenever the target file does not end with a trailing newline and insert_line equals the number of lines in the file.

This was inherited from the original Anthropic computer-use-demo implementation that the editor is derived from.

Steps to Reproduce
  1. Create a file t.txt with content a\nb\nc (no trailing newline, 3 lines).
  2. Run the reproduction script below with python.
from openhands.tools.file_editor.editor import FileEditor

import tempfile, os
d = tempfile.mkdtemp()
p = os.path.join(d, "t.txt")
with open(p, "w") as f:
    f.write("a\nb\nc")  # no trailing newline, 3 lines

editor = FileEditor()
editor(command="insert", path=p, insert_line=3, new_str="X")

with open(p) as f:
    print(repr(f.read()))
Expected Behavior

'a\nb\nc\nX\n' — the inserted line X starts on its own line.

Actual Behavior

'a\nb\ncX\n' — running the script with python shows c and X merged into a single line, because Python's line iterator returns the last line without \n when the file has no final newline, and _execute_insert() appends the new lines directly onto it.

Acceptance Criteria
  • Inserting at insert_line == num_lines in a file without a trailing newline starts the new content on its own line
  • Inserting into a file that already ends with \n does not introduce an extra blank line
  • Middle-of-file inserts are unchanged
Additional Context

Found while auditing the file editor tool. Same class of missing-trailing-newline bug that other editors in the ecosystem have fixed. Fix proposed in PR (guard: if the last retained line does not end with \n, append one before inserting).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in openhands/tools/file_editor/editor.py at FileEditor and _execute_insert(), then run the Python reproduction script from the issue against a file without a trailing newline. Verify that an end-of-file insert starts on a new line, while inserts into newline-terminated and middle-of-file content remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.