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:
parent
3906331b1b
commit
0d390688e3
1 changed files with 14 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue