Skip to content

Commit aef22c8

Browse files
cliffhallclaude
andcommitted
fix(filesystem): match edit_file oldText that ends with a newline
In edit_file's whitespace-tolerant fallback, a trailing newline on oldText became an empty last "line" that had to equal the file's next line, so whole lines copied with their final newline failed to match whenever only indentation differed. The newline now ends oldText's last line: the match keeps the file's indentation and must be followed by a newline in the file. The newline after the match is kept when newText also ends with one; otherwise newText's last line joins the next line, as an exact replacement would leave it. Exact matching is unchanged. Closes #5431 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 281b167 commit aef22c8

4 files changed

Lines changed: 179 additions & 10 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+
`edit_file`: an `oldText` of whole lines that ends with a newline now matches when only indentation differs, keeping the file's indentation; the trailing newline ends the last line instead of adding an empty one.

‎src/filesystem/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Files inside the allowed directories are also [resources](https://modelcontextpr
189189
- `newText` (string): Text to replace with
190190
- `dryRun` (boolean): Preview changes without applying (default: false)
191191
- Returns detailed diff and match information for dry runs, otherwise applies changes
192-
- Matching: `oldText` must match the file's text exactly, except that line endings never matter and, when there is no exact match, a run of whole lines matches if each line is equal once leading and trailing whitespace is ignored. Whitespace inside a line must match; nothing fuzzier is tried
192+
- Matching: `oldText` must match the file's text exactly, except that line endings never matter and, when there is no exact match, a run of whole lines matches if each line is equal once leading and trailing whitespace is ignored. Whitespace inside a line must match; nothing fuzzier is tried. A newline at the end of `oldText` ends its last line rather than adding an empty one, so whole lines copied with their final newline still match when only indentation differs; the matched lines must then be followed by a newline in the file, and that newline is kept only if `newText` also ends with one
193193
- If any edit has no match, no edits are applied, and the error names the edit, quotes the closest lines of the file with line numbers, and shows the first line that differs (JSON-quoted, so tabs and trailing spaces are visible)
194194
- Best Practice: Always use dryRun first to preview changes before applying them
195195
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// edit_file's whitespace-tolerant match with an oldText that ends with a
2+
// newline (#5431). The fallback splits oldText into lines, and a trailing
3+
// newline used to become an empty last "line" that had to equal the file's
4+
// next line, so whole lines copied with their final newline failed to match
5+
// whenever only indentation differed. A trailing newline now ends oldText's
6+
// last line, as it does in exact matching: the match keeps the file's
7+
// indentation, it must be followed by a newline in the file, and newText's
8+
// trailing newline decides whether that newline survives. These tests drive
9+
// edit_file over the protocol and read back what was written.
10+
11+
import fs from "fs/promises";
12+
import path from "path";
13+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
14+
import {
15+
call,
16+
connect,
17+
makeTempDir,
18+
quietStderr,
19+
textOf,
20+
type Connected,
21+
} from "./helpers.js";
22+
23+
describe("edit_file whitespace-tolerant match with a trailing newline", () => {
24+
let dir: string;
25+
let conn: Connected;
26+
let file: string;
27+
// The issue's fixture: tab indentation, two trailing spaces on line 3.
28+
const tabbed = "def f():\n\tif x:\n\t\treturn 1 \n\treturn 2\n";
29+
30+
beforeEach(async () => {
31+
quietStderr();
32+
dir = await makeTempDir("mcp-fs-edit-trailing-newline-");
33+
conn = await connect([dir]);
34+
file = path.join(dir, "code.py");
35+
});
36+
37+
afterEach(async () => {
38+
vi.restoreAllMocks();
39+
await conn.close();
40+
await fs.rm(dir, { recursive: true, force: true });
41+
});
42+
43+
async function edit(oldText: string, newText: string, dryRun = false) {
44+
return call(conn.client, "edit_file", {
45+
path: file,
46+
edits: [{ oldText, newText }],
47+
dryRun,
48+
});
49+
}
50+
51+
it("matches the issue's repro and keeps the file's indentation", async () => {
52+
await fs.writeFile(file, tabbed);
53+
const result = await edit("if x:\n return 1\n", "if x:\n return 9\n");
54+
expect(result.isError).toBeFalsy();
55+
expect(await fs.readFile(file, "utf-8")).toBe(
56+
"def f():\n\tif x:\n\t\treturn 9\n\treturn 2\n",
57+
);
58+
});
59+
60+
it("previews the same edit as a dry run without writing", async () => {
61+
await fs.writeFile(file, tabbed);
62+
const result = await edit(
63+
"if x:\n return 1\n",
64+
"if x:\n return 9\n",
65+
true,
66+
);
67+
expect(result.isError).toBeFalsy();
68+
expect(textOf(result)).toContain("-\t\treturn 1 \n+\t\treturn 9\n");
69+
expect(await fs.readFile(file, "utf-8")).toBe(tabbed);
70+
});
71+
72+
it("gives the same result as the oldText without its final newline", async () => {
73+
await fs.writeFile(file, tabbed);
74+
await edit("if x:\n return 1", "if x:\n return 9");
75+
const withoutNewline = await fs.readFile(file, "utf-8");
76+
await fs.writeFile(file, tabbed);
77+
await edit("if x:\n return 1\n", "if x:\n return 9\n");
78+
expect(await fs.readFile(file, "utf-8")).toBe(withoutNewline);
79+
});
80+
81+
it("removes whole lines when newText is empty", async () => {
82+
await fs.writeFile(file, tabbed);
83+
const result = await edit(" return 1\n", "");
84+
expect(result.isError).toBeFalsy();
85+
expect(await fs.readFile(file, "utf-8")).toBe(
86+
"def f():\n\tif x:\n\treturn 2\n",
87+
);
88+
});
89+
90+
it("joins newText's last line to the next line when only oldText ends with a newline", async () => {
91+
// What an exact replacement does: the newline belonged to oldText, and
92+
// newText does not put it back.
93+
await fs.writeFile(file, tabbed);
94+
const result = await edit("return 1\n", "return 9; ");
95+
expect(result.isError).toBeFalsy();
96+
expect(await fs.readFile(file, "utf-8")).toBe(
97+
"def f():\n\tif x:\n\t\treturn 9; \treturn 2\n",
98+
);
99+
});
100+
101+
it("matches the file's last line when the file ends with a newline", async () => {
102+
await fs.writeFile(file, tabbed);
103+
const result = await edit(" return 2\n", " return 3\n");
104+
expect(result.isError).toBeFalsy();
105+
expect(await fs.readFile(file, "utf-8")).toBe(
106+
"def f():\n\tif x:\n\t\treturn 1 \n\treturn 3\n",
107+
);
108+
});
109+
110+
it("does not match a last line the file does not end with a newline", async () => {
111+
const unterminated = "def f():\n\treturn 2";
112+
await fs.writeFile(file, unterminated);
113+
const result = await edit(" return 2\n", " return 3\n");
114+
expect(result.isError).toBe(true);
115+
expect(textOf(result)).toContain(
116+
'oldText line 2 ("") runs past the end of the file, which ends at line 2.',
117+
);
118+
expect(await fs.readFile(file, "utf-8")).toBe(unterminated);
119+
});
120+
121+
it("keeps CRLF line endings", async () => {
122+
await fs.writeFile(file, tabbed.replace(/\n/g, "\r\n"));
123+
const result = await edit(
124+
"if x:\r\n return 1\r\n",
125+
"if x:\r\n return 9\r\n",
126+
);
127+
expect(result.isError).toBeFalsy();
128+
expect(await fs.readFile(file, "utf-8")).toBe(
129+
"def f():\r\n\tif x:\r\n\t\treturn 9\r\n\treturn 2\r\n",
130+
);
131+
});
132+
133+
it("leaves exact matching with a trailing newline unchanged", async () => {
134+
await fs.writeFile(file, tabbed);
135+
const result = await edit("\tif x:\n", "\tif y:\n");
136+
expect(result.isError).toBeFalsy();
137+
expect(await fs.readFile(file, "utf-8")).toBe(
138+
"def f():\n\tif y:\n\t\treturn 1 \n\treturn 2\n",
139+
);
140+
});
141+
});

‎src/filesystem/lib.ts‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,12 +1006,30 @@ export async function applyFileEdits(
10061006
continue;
10071007
}
10081008

1009-
// Otherwise, try line-by-line matching with flexibility for whitespace
1010-
const oldLines = normalizedOld.split("\n");
1009+
// Otherwise, try line-by-line matching with flexibility for whitespace.
1010+
// A trailing newline ends oldText's last line rather than adding an
1011+
// empty line after it (#5431), so it is dropped from the lines compared,
1012+
// and the match must instead be followed by a newline in the file, as
1013+
// exact matching would require. newText's trailing newline is handled to
1014+
// match: the newline after the matched lines stays when newText also
1015+
// ends with one; otherwise newText's last line joins the line after the
1016+
// match, as an exact replacement would leave it.
1017+
const oldEndsLine = normalizedOld.endsWith("\n");
1018+
const newEndsLine = normalizedNew.endsWith("\n");
1019+
const oldLines = (
1020+
oldEndsLine ? normalizedOld.slice(0, -1) : normalizedOld
1021+
).split("\n");
1022+
const replacementLines = (
1023+
oldEndsLine && newEndsLine ? normalizedNew.slice(0, -1) : normalizedNew
1024+
).split("\n");
10111025
const contentLines = modifiedContent.split("\n");
1026+
// With a trailing newline, the last matched line needs an element after
1027+
// it in contentLines: that is what shows the file's newline is there.
1028+
const lastStart =
1029+
contentLines.length - oldLines.length - (oldEndsLine ? 1 : 0);
10121030
let matchFound = false;
10131031

1014-
for (let i = 0; i <= contentLines.length - oldLines.length; i++) {
1032+
for (let i = 0; i <= lastStart; i++) {
10151033
const potentialMatch = contentLines.slice(i, i + oldLines.length);
10161034

10171035
// Compare lines with normalized whitespace
@@ -1025,12 +1043,9 @@ export async function applyFileEdits(
10251043
// replaces, shifted by however much the edit indents or dedents it
10261044
// relative to the matching oldText line (#4990). Lines beyond
10271045
// oldText's length are placed relative to its last line.
1028-
const editUnit = indentUnit([
1029-
...oldLines,
1030-
...normalizedNew.split("\n"),
1031-
]);
1046+
const editUnit = indentUnit([...oldLines, ...replacementLines]);
10321047
const fileUnit = indentUnit(contentLines);
1033-
const newLines = normalizedNew.split("\n").map((line, j) => {
1048+
const newLines = replacementLines.map((line, j) => {
10341049
const k = Math.min(j, oldLines.length - 1);
10351050
return reindentLine(
10361051
line,
@@ -1041,7 +1056,15 @@ export async function applyFileEdits(
10411056
);
10421057
});
10431058

1044-
contentLines.splice(i, oldLines.length, ...newLines);
1059+
if (oldEndsLine && !newEndsLine) {
1060+
// oldText took the newline after the matched lines and newText
1061+
// does not put one back, so newText's last line runs into the line
1062+
// that followed the match. An empty newText just removes the lines.
1063+
newLines[newLines.length - 1] += contentLines[i + oldLines.length];
1064+
contentLines.splice(i, oldLines.length + 1, ...newLines);
1065+
} else {
1066+
contentLines.splice(i, oldLines.length, ...newLines);
1067+
}
10451068
modifiedContent = contentLines.join("\n");
10461069
matchFound = true;
10471070
break;

0 commit comments

Comments
 (0)