fix: prevent move operation from silently overwriting existing files (#219) (#223)

Check if the destination file exists before performing a move in
hashline-edit. If it does, return an error instead of silently
overwriting the file.

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
TÂCHES 2026-03-13 12:27:39 -06:00 committed by GitHub
parent 3906331b1b
commit 0d390688e3

View file

@ -259,6 +259,20 @@ export function createHashlineEditTool(cwd: string, options?: HashlineEditToolOp
// Write result
const finalContent = bom + restoreLineEndings(result.lines, originalEnding);
const writePath = move ? resolveToCwd(move, cwd) : absolutePath;
// Prevent silent overwrite when moving to an existing file
if (move && writePath !== absolutePath) {
try {
await ops.access(writePath);
// If access succeeds, the file exists — refuse the move
throw new Error(`Destination file already exists: ${writePath}. Use a different path or delete the existing file first.`);
} catch (err: any) {
// Re-throw our own error; swallow only "file not found"
if (err.message?.startsWith("Destination file already exists:")) throw err;
// File doesn't exist — safe to proceed
}
}
await ops.writeFile(writePath, finalContent);
// If moved, delete original