Skip to content

filesystem: read_text_file head/tail mishandle line endings (tail drops a line after a final newline; head keeps \r on CRLF) #5187

Description

@cliffhall

Problem

tailFile in src/filesystem/lib.ts splits the decoded tail on \n and keeps the last N segments. For a file that ends with a newline, the empty segment after the final \n counts as one of the N lines: tail: 1 on "a\nb\nc\n" returns an empty string, and tail: 2 returns only c. The test in src/filesystem/__tests__/server-tools.test.ts that counts the trailing newline as a line pins this today. head does not have the trailing-newline problem, but it has a line-ending one: headFile splits on \n without normalizing CRLF, so on a CRLF file every line it returns still ends in \r, while tail normalizes to LF.

Expected

A final newline ends the last line and is not counted as an extra empty line, for LF and CRLF files and across the chunk boundary tailFile reads in. The pinning test changes with the fix. head returns CRLF files' lines without a trailing \r, normalized to LF the same way tail is, so head and tail agree on line endings.

Scope widened during work on #5316 to cover head's CRLF handling too, since both are read_text_file's line modes.

Prior work

The PR(s) above targeted the pre-refactor code and are closed as part of the backlog triage in #4875; this issue is where the work continues, with the PR(s) as a prototype. Thank you to their authors.

Activity

  1. added
    bugSomething isn't working
    server-filesystemReference implementation for the Filesystem MCP server - src/filesystem
    on Oct 20, 2026
  2. cliffhall commented on Oct 20, 2026

    @cliffhall
    MemberAuthor

    Triage: Priority Medium (total 6)

    Filed from the PR backlog triage (#4877). Board: Servers V2 (#43), Status Incoming, awaiting maintainer review (no milestone yet).

  3. added this to the v2.2.0 milestone on Oct 25, 2026
  4. self-assigned this
    on Oct 27, 2026
  5. changed the title [-]filesystem: read_text_file tail returns one line too few when the file ends with a newline[/-] [+]filesystem: read_text_file head/tail mishandle line endings (tail drops a line after a final newline; head keeps \r on CRLF)[/+] on Oct 27, 2026
  6. cliffhall commented on Oct 27, 2026

    @cliffhall
    MemberAuthor

    Fixed by #5316 (tail) and #5324 (head CRLF), both merged into v2/main.

  7. added 2 commits that reference this issue on Oct 28, 2026
    30afc34
    1002969
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingserver-filesystemReference implementation for the Filesystem MCP server - src/filesystemv2

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions