Skip to content

Commit 30afc34

Browse files
cliffhallclaude
andcommitted
fix(filesystem): tail no longer counts a final newline as a line
tailFile split the decoded tail on "\n" and kept the last N segments, so the empty segment after a file's final newline took one of the N slots. Its read loop stopped on N newlines including that final one, so a file longer than one 1 KB chunk could also cut the first wanted line short. Drop one trailing newline before splitting, and have the read loop look for one more line break when the file ends with a newline. The result carries no trailing newline, as headFile's does. Closes modelcontextprotocol#5187 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 66e3dec commit 30afc34

3 files changed

Lines changed: 77 additions & 6 deletions

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 `tail` no longer returns one line too few when the file ends with a newline: the final newline ends the last line instead of counting as an empty one, for LF and CRLF files and for files longer than one read chunk. Like `head`, the result carries no trailing newline.

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

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,67 @@ describe("read_text_file and read_file", () => {
357357
expect(textOf(result)).toBe("one\ntwo");
358358
});
359359

360-
it("returns the last N lines with tail, counting the trailing newline as a line", async () => {
360+
it("returns the last N lines with tail, not counting the trailing newline as a line (#5187)", async () => {
361361
const result = await call(client, "read_text_file", {
362362
path: file,
363363
tail: 2,
364364
});
365-
expect(textOf(result)).toBe("four\n");
365+
expect(textOf(result)).toBe("three\nfour");
366+
});
367+
368+
describe("tail at the end of the file (#5187)", () => {
369+
async function tail(content: string, n: number): Promise<string> {
370+
const target = path.join(dir, "tail.txt");
371+
await fs.writeFile(target, content);
372+
return textOf(
373+
await call(client, "read_text_file", { path: target, tail: n }),
374+
);
375+
}
376+
377+
it("returns the last line of an LF file that ends with a newline", async () => {
378+
expect(await tail("a\nb\nc\n", 1)).toBe("c");
379+
});
380+
381+
it("returns the last lines of a CRLF file that ends with a newline", async () => {
382+
expect(await tail("a\r\nb\r\nc\r\n", 2)).toBe("b\nc");
383+
});
384+
385+
it("returns the last lines of a file with no final newline unchanged", async () => {
386+
expect(await tail("a\nb\nc", 2)).toBe("b\nc");
387+
});
388+
389+
it("counts a blank last line as a line", async () => {
390+
expect(await tail("a\nb\n\n", 2)).toBe("b\n");
391+
});
392+
393+
it("returns the whole file when tail exceeds its line count", async () => {
394+
expect(await tail("a\nb\n", 5)).toBe("a\nb");
395+
});
396+
397+
// tailFile reads 1 KB chunks from the end. Here the last chunk holds both
398+
// remaining newlines but starts partway through the first wanted line, so
399+
// the read loop must not count the final newline as a line break it needs.
400+
it("reads back past a chunk boundary for the first wanted line (LF)", async () => {
401+
const content =
402+
"head\n" + "A".repeat(611) + "\n" + "B".repeat(511) + "\n";
403+
expect(await tail(content, 2)).toBe(
404+
"A".repeat(611) + "\n" + "B".repeat(511),
405+
);
406+
});
407+
408+
it("reads back past a chunk boundary for the first wanted line (CRLF)", async () => {
409+
const content =
410+
"head\r\n" + "A".repeat(611) + "\r\n" + "B".repeat(510) + "\r\n";
411+
expect(await tail(content, 2)).toBe(
412+
"A".repeat(611) + "\n" + "B".repeat(510),
413+
);
414+
});
415+
416+
it("joins a CRLF pair split across a chunk boundary", async () => {
417+
// The last 1024 bytes start with the "\n" of the "\r\n" after "x".
418+
const content = "x\r\n" + "C".repeat(1021) + "\r\n";
419+
expect(await tail(content, 2)).toBe("x\n" + "C".repeat(1021));
420+
});
366421
});
367422

368423
it("treats head: 0 as absent and returns the whole file", async () => {

‎src/filesystem/lib.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,23 +593,34 @@ export async function tailFile(
593593
let position = fileSize;
594594
const chunk = Buffer.alloc(CHUNK_SIZE);
595595
let newlinesFound = 0;
596-
597-
// Read chunks from the end of the file until we have enough lines
598-
while (position > 0 && newlinesFound < numLines) {
596+
// A final newline ends the last line rather than starting an empty one,
597+
// so it is not one of the N line breaks the loop needs to see. Set from
598+
// the file's last byte on the first read (CRLF ends in 0x0a too).
599+
let newlinesNeeded = numLines;
600+
601+
// Read chunks from the end of the file until the start of the first of
602+
// the last N lines is in hand
603+
while (position > 0 && newlinesFound < newlinesNeeded) {
599604
const size = Math.min(CHUNK_SIZE, position);
605+
const atEnd = position === fileSize;
600606
position -= size;
601607

602608
const { bytesRead } = await fileHandle.read(chunk, 0, size, position);
603609
if (!bytesRead) break;
604610

605611
const readData = Buffer.from(chunk.subarray(0, bytesRead));
612+
if (atEnd && readData[bytesRead - 1] === 0x0a) newlinesNeeded++;
606613
chunks.unshift(readData);
607614
for (const byte of readData) {
608615
if (byte === 0x0a) newlinesFound++;
609616
}
610617
}
611618

612-
const text = normalizeLineEndings(Buffer.concat(chunks).toString("utf-8"));
619+
// Drop one trailing newline so it does not count as an empty last line,
620+
// and so the result ends the way headFile's does: without one.
621+
const text = normalizeLineEndings(
622+
Buffer.concat(chunks).toString("utf-8"),
623+
).replace(/\n$/, "");
613624
return text.split("\n").slice(-numLines).join("\n");
614625
} finally {
615626
await fileHandle.close();

0 commit comments

Comments
 (0)