Fix edit tool failing on files with UTF-8 BOM

Strip BOM before matching (LLM won't include invisible BOM in oldText),
restore on write.

Based on #394 by @prathamdby
This commit is contained in:
Mario Zechner 2026-01-02 01:57:24 +01:00
parent 6e4270a286
commit d9adf659ca
4 changed files with 30 additions and 4 deletions

View file

@ -433,4 +433,18 @@ describe("edit tool CRLF handling", () => {
}),
).rejects.toThrow(/Found 2 occurrences/);
});
it("should preserve UTF-8 BOM after edit", async () => {
const testFile = join(testDir, "bom-test.txt");
writeFileSync(testFile, "\uFEFFfirst\r\nsecond\r\nthird\r\n");
await editTool.execute("test-bom", {
path: testFile,
oldText: "second\n",
newText: "REPLACED\n",
});
const content = readFileSync(testFile, "utf-8");
expect(content).toBe("\uFEFFfirst\r\nREPLACED\r\nthird\r\n");
});
});