Skip to content

Commit 1002969

Browse files
cliffhallclaude
andcommitted
fix(filesystem): head returns CRLF lines without their \r
headFile split on "\n" without normalizing CRLF, so every line it returned from a CRLF file still ended in "\r", while tailFile normalizes to LF. Drop the "\r" of each line that ended in "\r\n", including a pair split across the 1 KB read chunk, so head and tail agree on line endings. LF output is unchanged. Part of #5187 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 84b412b commit 1002969

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-filesystem": patch
3+
---
4+
5+
`read_text_file` with `head` no longer leaves a `\r` at the end of each line of a CRLF file. Like `tail`, it returns lines normalized to LF, including a `\r\n` pair split across a read chunk. LF output is unchanged.

‎src/filesystem/__tests__/server-tools.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,35 @@ describe("read_text_file and read_file", () => {
365365
expect(textOf(result)).toBe("three\nfour");
366366
});
367367

368+
describe("head line endings (#5187)", () => {
369+
async function head(content: string, n: number): Promise<string> {
370+
const target = path.join(dir, "head.txt");
371+
await fs.writeFile(target, content);
372+
return textOf(
373+
await call(client, "read_text_file", { path: target, head: n }),
374+
);
375+
}
376+
377+
it("returns the first lines of an LF file unchanged", async () => {
378+
expect(await head("a\nb\nc\n", 2)).toBe("a\nb");
379+
});
380+
381+
it("returns the first lines of a CRLF file without their \\r", async () => {
382+
expect(await head("a\r\nb\r\nc\r\n", 2)).toBe("a\nb");
383+
});
384+
385+
it("returns a CRLF file with no final newline normalized to LF", async () => {
386+
expect(await head("a\r\nb\r\nc", 5)).toBe("a\nb\nc");
387+
});
388+
389+
// headFile reads 1 KB chunks from the start. Here the first chunk ends
390+
// on the "\r" of a "\r\n" pair whose "\n" opens the second chunk.
391+
it("joins a CRLF pair split across a chunk boundary", async () => {
392+
const content = "C".repeat(1023) + "\r\nnext\r\n";
393+
expect(await head(content, 2)).toBe("C".repeat(1023) + "\nnext");
394+
});
395+
});
396+
368397
describe("tail at the end of the file (#5187)", () => {
369398
async function tail(content: string, n: number): Promise<string> {
370399
const target = path.join(dir, "tail.txt");

‎src/filesystem/lib.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,11 @@ export async function headFile(
765765
const completeLines = buffer.slice(0, newLineIndex).split("\n");
766766
buffer = buffer.slice(newLineIndex + 1);
767767
for (const line of completeLines) {
768-
lines.push(line);
768+
// Each of these lines ended in "\n"; drop the "\r" of a CRLF ending
769+
// so head normalizes line endings the way tailFile does. A "\r"
770+
// whose "\n" is still in the next chunk stays in the buffer until
771+
// that "\n" arrives, so a pair split across chunks is handled too.
772+
lines.push(line.endsWith("\r") ? line.slice(0, -1) : line);
769773
if (lines.length >= numLines) break;
770774
}
771775
}

0 commit comments

Comments
 (0)