Skip to content

Commit d95dd2f

Browse files
cliffhallclaude
andcommitted
fix(filesystem): keep line endings and indentation in edit_file
edit_file read the file with its CRLF line endings normalized to LF and wrote it back that way, so any edit to a CRLF file silently rewrote every line ending. It now detects the file's predominant line ending and writes the edited file back with it (modelcontextprotocol#4991). When there is no exact match, the whitespace-tolerant matcher reindented only the first replacement line from the file; later lines whose oldText line or own line had no indentation were inserted as given, and indented ones were anchored at the first line. Each replacement line now takes the indentation of the file line it replaces, shifted by its indentation relative to the matching oldText line (clamped at none); lines beyond oldText's length are placed relative to its last line, and blank lines are kept as given (modelcontextprotocol#4990). The pinning tests now assert the correct behavior and lose their KNOWN BUG markers; the matcher's other reindentation pins are updated to the new rule. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 571e2b3 commit d95dd2f

3 files changed

Lines changed: 139 additions & 39 deletions

File tree

‎src/filesystem/__tests__/lib.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
// Pure utility functions
1515
formatSize,
1616
normalizeLineEndings,
17+
detectLineEnding,
1718
createUnifiedDiff,
1819
// Security & validation functions
1920
validatePath,
@@ -161,6 +162,26 @@ describe("Lib Functions", () => {
161162
});
162163
});
163164

165+
describe("detectLineEnding", () => {
166+
it("detects CRLF", () => {
167+
expect(detectLineEnding("line1\r\nline2\r\n")).toBe("\r\n");
168+
});
169+
170+
it("detects LF", () => {
171+
expect(detectLineEnding("line1\nline2\n")).toBe("\n");
172+
});
173+
174+
it("picks the predominant ending of a mixed file", () => {
175+
expect(detectLineEnding("a\r\nb\r\nc\n")).toBe("\r\n");
176+
expect(detectLineEnding("a\r\nb\nc\n")).toBe("\n");
177+
});
178+
179+
it("defaults to LF for a file with no line breaks", () => {
180+
expect(detectLineEnding("")).toBe("\n");
181+
expect(detectLineEnding("line1")).toBe("\n");
182+
});
183+
});
184+
164185
describe("createUnifiedDiff", () => {
165186
it("creates diff for simple changes", () => {
166187
const original = "line1\nline2\nline3";
@@ -804,7 +825,7 @@ describe("Lib Functions", () => {
804825

805826
expect(mockFs.writeFile).toHaveBeenCalledWith(
806827
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
807-
"line1\nmodified line2\nline3\n",
828+
"line1\r\nmodified line2\r\nline3\r\n",
808829
"utf-8",
809830
);
810831
expect(mockFs.rename).toHaveBeenCalledWith(

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

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -691,22 +691,35 @@ describe("edit_file", () => {
691691
expect(text.endsWith("`````\n\n")).toBe(true);
692692
});
693693

694-
// KNOWN BUG #4991: any edit silently converts a CRLF file's line endings to LF; the fix changes this assertion.
695-
// The CRLF in the file is normalized away by any edit, dry run aside.
696-
it("rewrites a CRLF file with LF line endings", async () => {
694+
// #4991: an edit keeps the file's line endings, whatever oldText and
695+
// newText use.
696+
it("keeps a CRLF file's line endings", async () => {
697697
await fs.writeFile(file, "a\r\nb\r\n");
698698
await call(client, "edit_file", {
699699
path: file,
700-
edits: [{ oldText: "a\r\n", newText: "c\r\n" }],
700+
edits: [
701+
{ oldText: "a\r\n", newText: "c\r\n" },
702+
{ oldText: "b\n", newText: "d\ne\n" },
703+
],
704+
});
705+
expect(await fs.readFile(file, "utf-8")).toBe("c\r\nd\r\ne\r\n");
706+
});
707+
708+
it("keeps an LF file's line endings when newText uses CRLF", async () => {
709+
await fs.writeFile(file, "a\nb\n");
710+
await call(client, "edit_file", {
711+
path: file,
712+
edits: [{ oldText: "a", newText: "c\r\nd" }],
701713
});
702-
expect(await fs.readFile(file, "utf-8")).toBe("c\nb\n");
714+
expect(await fs.readFile(file, "utf-8")).toBe("c\nd\nb\n");
703715
});
704716

705717
// #2034: when there is no exact substring, the matcher compares line by line
706718
// with leading and trailing whitespace trimmed, and reindents the
707-
// replacement. These pin its current reindentation rules.
719+
// replacement (#4990): each replacement line takes the indentation of the
720+
// file line it replaces, shifted by its indent relative to the matching
721+
// oldText line.
708722
describe("whitespace-tolerant matcher (#2034)", () => {
709-
// KNOWN BUG #4990: lines after the first lose the file's indentation when oldText's line has none; the fix changes this assertion.
710723
it("matches lines whose indentation differs from oldText", async () => {
711724
await call(client, "edit_file", {
712725
path: file,
@@ -717,14 +730,12 @@ describe("edit_file", () => {
717730
},
718731
],
719732
});
720-
// First line takes the file's indentation; later lines without
721-
// indentation of their own are inserted as given.
733+
// Every line takes the indentation of the file line it replaces.
722734
expect(await fs.readFile(file, "utf-8")).toBe(
723-
"function greet() {\n console.log('bye');\nreturn 0;\n}\n",
735+
"function greet() {\n console.log('bye');\n return 0;\n}\n",
724736
);
725737
});
726738

727-
// KNOWN BUG #4990: a replacement line whose oldText line has no indent ignores the file's indentation; the fix changes this assertion.
728739
it("keeps relative indentation when both old and new lines are indented", async () => {
729740
await fs.writeFile(file, " if (a) {\n b();\n }\n");
730741
await call(client, "edit_file", {
@@ -736,30 +747,64 @@ describe("edit_file", () => {
736747
},
737748
],
738749
});
739-
// Line 2: new indent 6 - old indent 2 = 4 extra spaces on top of the
740-
// first line's 4. Line 3: old line "}" has no indent, so the new
741-
// line is inserted as given.
750+
// Line 2: the file's 8, plus new indent 6 - old indent 2 = 4 more.
751+
// Line 3: the file's 4, plus new indent 2 - old indent 0 = 2 more.
752+
expect(await fs.readFile(file, "utf-8")).toBe(
753+
" if (a) {\n c();\n }\n",
754+
);
755+
});
756+
757+
it("keeps the file's indentation when oldText indents with a different width", async () => {
758+
await fs.writeFile(file, " if (a) {\n b();\n }\n");
759+
await call(client, "edit_file", {
760+
path: file,
761+
edits: [
762+
{
763+
oldText: "if (a) {\n b();\n}",
764+
newText: "if (a) {\n c();\n}",
765+
},
766+
],
767+
});
742768
expect(await fs.readFile(file, "utf-8")).toBe(
743-
" if (a) {\n c();\n }\n",
769+
" if (a) {\n c();\n }\n",
744770
);
745771
});
746772

747-
it("clamps a negative relative indent to the first line's indentation", async () => {
773+
it("dedents relative to the file line it replaces", async () => {
748774
await fs.writeFile(file, " x\n y\n");
749775
await call(client, "edit_file", {
750776
path: file,
751777
edits: [{ oldText: "x\n y", newText: "x\n z" }],
752778
});
753-
expect(await fs.readFile(file, "utf-8")).toBe(" x\n z\n");
779+
// Line 2: the file's 6, minus old indent 4 - new indent 1 = 3.
780+
expect(await fs.readFile(file, "utf-8")).toBe(" x\n z\n");
781+
});
782+
783+
it("clamps a dedent at no indentation", async () => {
784+
await fs.writeFile(file, "x\n y\n");
785+
await call(client, "edit_file", {
786+
path: file,
787+
edits: [{ oldText: "x\n y", newText: "x\nz" }],
788+
});
789+
expect(await fs.readFile(file, "utf-8")).toBe("x\nz\n");
790+
});
791+
792+
it("indents replacement lines beyond oldText's length relative to its last line", async () => {
793+
await fs.writeFile(file, " a\nb\n");
794+
await call(client, "edit_file", {
795+
path: file,
796+
edits: [{ oldText: "a ", newText: "a\n added" }],
797+
});
798+
expect(await fs.readFile(file, "utf-8")).toBe(" a\n added\nb\n");
754799
});
755800

756-
it("inserts extra replacement lines beyond oldText's length as given", async () => {
757-
await fs.writeFile(file, "a\nb\n");
801+
it("keeps a blank replacement line as given", async () => {
802+
await fs.writeFile(file, " a\n b\n");
758803
await call(client, "edit_file", {
759804
path: file,
760-
edits: [{ oldText: " a ", newText: "a\n added" }],
805+
edits: [{ oldText: "a\nb", newText: "a\n\nb" }],
761806
});
762-
expect(await fs.readFile(file, "utf-8")).toBe("a\n added\nb\n");
807+
expect(await fs.readFile(file, "utf-8")).toBe(" a\n\n b\n");
763808
});
764809

765810
it("reports no match when a line differs beyond whitespace", async () => {

‎src/filesystem/lib.ts‎

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,51 @@ interface FileEdit {
311311
newText: string;
312312
}
313313

314+
/**
315+
* The line ending a file predominantly uses: CRLF when more of its line breaks
316+
* are CRLF than bare LF, otherwise LF. A file with mixed endings is written
317+
* back with its predominant one.
318+
*/
319+
export function detectLineEnding(text: string): "\r\n" | "\n" {
320+
const crlf = text.match(/\r\n/g)?.length ?? 0;
321+
const lf = (text.match(/\n/g)?.length ?? 0) - crlf;
322+
return crlf > lf ? "\r\n" : "\n";
323+
}
324+
325+
function leadingWhitespace(line: string): string {
326+
return line.slice(0, line.length - line.trimStart().length);
327+
}
328+
329+
/**
330+
* Reindents one replacement line for the whitespace-tolerant matcher: it gets
331+
* the indentation of `fileLine` (the file line it replaces), plus or minus the
332+
* difference between its own indentation and `oldLine`'s (the oldText line
333+
* that matched `fileLine`). A dedent never goes below no indentation. A blank
334+
* line is kept as given.
335+
*/
336+
function reindentLine(line: string, fileLine: string, oldLine: string): string {
337+
const body = line.trimStart();
338+
if (body === "") return line;
339+
const fileIndent = leadingWhitespace(fileLine);
340+
const newIndent = leadingWhitespace(line);
341+
const delta = newIndent.length - leadingWhitespace(oldLine).length;
342+
const indent =
343+
delta >= 0
344+
? fileIndent + newIndent.slice(newIndent.length - delta)
345+
: fileIndent.slice(0, Math.max(0, fileIndent.length + delta));
346+
return indent + body;
347+
}
348+
314349
export async function applyFileEdits(
315350
filePath: string,
316351
edits: FileEdit[],
317352
dryRun: boolean = false,
318353
): Promise<string> {
319-
// Read file content and normalize line endings
320-
const content = normalizeLineEndings(await fs.readFile(filePath, "utf-8"));
354+
// Read file content and normalize line endings, remembering which ending
355+
// the file uses so the edited file is written back with it (#4991).
356+
const rawContent = await fs.readFile(filePath, "utf-8");
357+
const eol = detectLineEnding(rawContent);
358+
const content = normalizeLineEndings(rawContent);
321359

322360
// Apply edits sequentially
323361
let modifiedContent = content;
@@ -349,22 +387,13 @@ export async function applyFileEdits(
349387
});
350388

351389
if (isMatch) {
352-
// Preserve original indentation of first line
353-
const originalIndent = contentLines[i].match(/^\s*/)?.[0] || "";
390+
// Each replacement line takes the indentation of the file line it
391+
// replaces, shifted by however much the edit indents or dedents it
392+
// relative to the matching oldText line (#4990). Lines beyond
393+
// oldText's length are placed relative to its last line.
354394
const newLines = normalizedNew.split("\n").map((line, j) => {
355-
if (j === 0) return originalIndent + line.trimStart();
356-
// For subsequent lines, try to preserve relative indentation
357-
const oldIndent = oldLines[j]?.match(/^\s*/)?.[0] || "";
358-
const newIndent = line.match(/^\s*/)?.[0] || "";
359-
if (oldIndent && newIndent) {
360-
const relativeIndent = newIndent.length - oldIndent.length;
361-
return (
362-
originalIndent +
363-
" ".repeat(Math.max(0, relativeIndent)) +
364-
line.trimStart()
365-
);
366-
}
367-
return line;
395+
const k = Math.min(j, oldLines.length - 1);
396+
return reindentLine(line, potentialMatch[k], oldLines[k]);
368397
});
369398

370399
contentLines.splice(i, oldLines.length, ...newLines);
@@ -382,6 +411,11 @@ export async function applyFileEdits(
382411
// Create unified diff
383412
const diff = createUnifiedDiff(content, modifiedContent, filePath);
384413

414+
// Write the file back with the line ending it was read with (#4991).
415+
if (eol === "\r\n") {
416+
modifiedContent = modifiedContent.replace(/\n/g, "\r\n");
417+
}
418+
385419
// Format diff with appropriate number of backticks
386420
let numBackticks = 3;
387421
while (diff.includes("`".repeat(numBackticks))) {

0 commit comments

Comments
 (0)