|
| 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 | +}); |
0 commit comments