Skip to content

Commit 48a0924

Browse files
authored
Merge pull request #5316 from modelcontextprotocol/v2/fix/5187-filesystem-tail-trailing-newline
fix(filesystem): tail no longer counts a final newline as a line
2 parents bc99d61 + 30afc34 commit 48a0924

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
@@ -702,23 +702,34 @@ export async function tailFile(
702702
let position = fileSize;
703703
const chunk = Buffer.alloc(CHUNK_SIZE);
704704
let newlinesFound = 0;
705-
706-
// Read chunks from the end of the file until we have enough lines
707-
while (position > 0 && newlinesFound < numLines) {
705+
// A final newline ends the last line rather than starting an empty one,
706+
// so it is not one of the N line breaks the loop needs to see. Set from
707+
// the file's last byte on the first read (CRLF ends in 0x0a too).
708+
let newlinesNeeded = numLines;
709+
710+
// Read chunks from the end of the file until the start of the first of
711+
// the last N lines is in hand
712+
while (position > 0 && newlinesFound < newlinesNeeded) {
708713
const size = Math.min(CHUNK_SIZE, position);
714+
const atEnd = position === fileSize;
709715
position -= size;
710716

711717
const { bytesRead } = await fileHandle.read(chunk, 0, size, position);
712718
if (!bytesRead) break;
713719

714720
const readData = Buffer.from(chunk.subarray(0, bytesRead));
721+
if (atEnd && readData[bytesRead - 1] === 0x0a) newlinesNeeded++;
715722
chunks.unshift(readData);
716723
for (const byte of readData) {
717724
if (byte === 0x0a) newlinesFound++;
718725
}
719726
}
720727

721-
const text = normalizeLineEndings(Buffer.concat(chunks).toString("utf-8"));
728+
// Drop one trailing newline so it does not count as an empty last line,
729+
// and so the result ends the way headFile's does: without one.
730+
const text = normalizeLineEndings(
731+
Buffer.concat(chunks).toString("utf-8"),
732+
).replace(/\n$/, "");
722733
return text.split("\n").slice(-numLines).join("\n");
723734
} finally {
724735
await fileHandle.close();

0 commit comments

Comments
 (0)