|
| 1 | +// The edit_file no-match error (#2034). An edit whose oldText matches nowhere |
| 2 | +// used to fail with only "Could not find exact match", leaving a model to |
| 3 | +// guess at an invisible whitespace difference. The error now names the edit, |
| 4 | +// states what the matcher tolerates, and quotes the closest lines of the file |
| 5 | +// with line numbers and the first differing line JSON-quoted. These tests pin |
| 6 | +// that report, and that it stays a report: a near match is never applied. |
| 7 | + |
| 8 | +import fs from "fs/promises"; |
| 9 | +import path from "path"; |
| 10 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 11 | +import { describeEditMismatch } from "../lib.js"; |
| 12 | +import { |
| 13 | + call, |
| 14 | + connect, |
| 15 | + makeTempDir, |
| 16 | + quietStderr, |
| 17 | + textOf, |
| 18 | + type Connected, |
| 19 | +} from "./helpers.js"; |
| 20 | + |
| 21 | +describe("edit_file no-match error, over the protocol", () => { |
| 22 | + let dir: string; |
| 23 | + let conn: Connected; |
| 24 | + let file: string; |
| 25 | + const original = |
| 26 | + "function greet() {\n" + |
| 27 | + " const name = 'world';\n" + |
| 28 | + " console.log('hello', name);\n" + |
| 29 | + " return name;\n" + |
| 30 | + "}\n"; |
| 31 | + |
| 32 | + beforeEach(async () => { |
| 33 | + quietStderr(); |
| 34 | + dir = await makeTempDir("mcp-fs-edit-mismatch-"); |
| 35 | + conn = await connect([dir]); |
| 36 | + file = path.join(dir, "greet.ts"); |
| 37 | + await fs.writeFile(file, original); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(async () => { |
| 41 | + vi.restoreAllMocks(); |
| 42 | + await conn.close(); |
| 43 | + await fs.rm(dir, { recursive: true, force: true }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("names the failed edit, quotes the closest lines and writes nothing", async () => { |
| 47 | + const result = await call(conn.client, "edit_file", { |
| 48 | + path: file, |
| 49 | + edits: [ |
| 50 | + { oldText: "return name;", newText: "return name.length;" }, |
| 51 | + // A tab where the file has a space, inside the line: not tolerated. |
| 52 | + { |
| 53 | + oldText: " const name = 'world';\n console.log('hello',\tname);", |
| 54 | + newText: " console.log('bye');", |
| 55 | + }, |
| 56 | + ], |
| 57 | + }); |
| 58 | + expect(result.isError).toBe(true); |
| 59 | + expect(textOf(result)).toBe( |
| 60 | + "Could not find exact match for edit 2 of 2. oldText must match the " + |
| 61 | + "file's text exactly; only line endings, and leading and trailing " + |
| 62 | + "whitespace on whole lines, may differ. No edits were applied.\n\n" + |
| 63 | + "oldText:\n const name = 'world';\n console.log('hello',\tname);\n\n" + |
| 64 | + "Closest text in the file (lines 1-5 of 5, after the earlier edits " + |
| 65 | + "in this call):\n" + |
| 66 | + "1: function greet() {\n" + |
| 67 | + "2: const name = 'world';\n" + |
| 68 | + "3: console.log('hello', name);\n" + |
| 69 | + "4: return name.length;\n" + |
| 70 | + "5: }\n\n" + |
| 71 | + "First difference, oldText line 2 against file line 3:\n" + |
| 72 | + " oldText: \" console.log('hello',\\tname);\"\n" + |
| 73 | + " file: \" console.log('hello', name);\"\n\n" + |
| 74 | + "Retry with oldText copied exactly from the file, without the " + |
| 75 | + "line-number prefixes.", |
| 76 | + ); |
| 77 | + // The first edit matched, but a failed call applies none of them. |
| 78 | + expect(await fs.readFile(file, "utf-8")).toBe(original); |
| 79 | + }); |
| 80 | + |
| 81 | + it("still applies an edit that differs only in indentation", async () => { |
| 82 | + await call(conn.client, "edit_file", { |
| 83 | + path: file, |
| 84 | + edits: [{ oldText: " return name;\n}", newText: " return 0;\n}" }], |
| 85 | + }); |
| 86 | + const edited = await fs.readFile(file, "utf-8"); |
| 87 | + expect(edited).toContain("\n return 0;\n}"); |
| 88 | + expect(edited).not.toContain(" return 0;"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("states the matching rules in the tool description", async () => { |
| 92 | + const { tools } = await conn.client.listTools(); |
| 93 | + const description = tools.find((t) => t.name === "edit_file")?.description; |
| 94 | + expect(description).toContain("Whitespace inside a line must match."); |
| 95 | + expect(description).toContain("line endings (LF or CRLF) never matter"); |
| 96 | + expect(description).toContain("nothing is written"); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe("describeEditMismatch", () => { |
| 101 | + const lines = (n: number, prefix = "line") => |
| 102 | + Array.from({ length: n }, (_, i) => `${prefix} ${i + 1}`); |
| 103 | + |
| 104 | + it("omits the earlier-edits note for the first edit", () => { |
| 105 | + const message = describeEditMismatch("alpha\nbeta\n", "betta", 0, 1); |
| 106 | + expect(message).toContain("edit 1 of 1"); |
| 107 | + expect(message).toContain("(lines 1-2 of 2):\n1: alpha\n2: beta"); |
| 108 | + expect(message).toContain(' oldText: "betta"\n file: "beta"'); |
| 109 | + }); |
| 110 | + |
| 111 | + it("picks the most similar run of lines, the earliest on a tie", () => { |
| 112 | + const content = [...lines(10), "target x", ...lines(10), "target x"].join( |
| 113 | + "\n", |
| 114 | + ); |
| 115 | + const message = describeEditMismatch(content, "target x", 0, 1); |
| 116 | + expect(message).toContain("(lines 9-13 of 22):"); |
| 117 | + expect(message).toContain("11: target x"); |
| 118 | + expect(message).toContain("oldText line 1 against file line 11"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("normalizes CRLF in oldText before comparing", () => { |
| 122 | + const message = describeEditMismatch("a\nb\nc\n", "a\r\nB\r\n", 0, 1); |
| 123 | + expect(message).toContain("oldText line 2 against file line 2"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("reports oldText that runs past the end of the file", () => { |
| 127 | + const message = describeEditMismatch("a\nb\n", "a\nb\nc", 0, 1); |
| 128 | + expect(message).toContain("(lines 1-2 of 2):"); |
| 129 | + expect(message).toContain( |
| 130 | + 'oldText line 3 ("c") runs past the end of the file, which ends at line 2.', |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it("says so when no line resembles oldText", () => { |
| 135 | + const message = describeEditMismatch("abc\ndef\n", "xyz", 0, 1); |
| 136 | + expect(message).toContain("No line of the file resembles oldText."); |
| 137 | + expect(message).not.toContain("Closest text"); |
| 138 | + }); |
| 139 | + |
| 140 | + it("handles an empty file", () => { |
| 141 | + const message = describeEditMismatch("", "x", 0, 1); |
| 142 | + expect(message).toContain("No line of the file resembles oldText."); |
| 143 | + }); |
| 144 | + |
| 145 | + it("caps the quoted region at 30 lines", () => { |
| 146 | + const content = lines(50).join("\n"); |
| 147 | + const oldText = [...lines(39), "line 40 changed"].join("\n"); |
| 148 | + const message = describeEditMismatch(content, oldText, 0, 1); |
| 149 | + expect(message).toContain("(lines 1-30 of 50):"); |
| 150 | + expect(message).toContain("\n30: line 30\n"); |
| 151 | + expect(message).not.toContain("\n31: "); |
| 152 | + expect(message).toContain("oldText line 40 against file line 40"); |
| 153 | + }); |
| 154 | + |
| 155 | + it("skips the search on a file too large to compare", () => { |
| 156 | + const content = lines(2000).join("\n"); |
| 157 | + const oldText = lines(1000, "other").join("\n"); |
| 158 | + const message = describeEditMismatch(content, oldText, 1, 3); |
| 159 | + expect(message).toContain("edit 2 of 3"); |
| 160 | + expect(message).toContain( |
| 161 | + "The file has 2000 lines, too many to search for the closest text.", |
| 162 | + ); |
| 163 | + expect(message).not.toContain("Closest text"); |
| 164 | + }); |
| 165 | +}); |
0 commit comments