fix: create parent directories before checking file existence in write_to_file tool

Fixes ENOENT errors when attempting to create files in non-existent subdirectories.
The tool now ensures parent directories are created with recursive mkdir before
checking if a file exists, preventing "No such file or directory" errors.

Fixes #9634
This commit is contained in:
Roo Code 2025-11-26 23:55:44 +00:00
parent ba09228015
commit 7d7979ac38

View file

@ -75,6 +75,14 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
fileExists = task.diffViewProvider.editType === "modify"
} else {
const absolutePath = path.resolve(task.cwd, relPath)
// Ensure parent directories exist before checking file existence
const dirPath = path.dirname(absolutePath)
try {
await fs.mkdir(dirPath, { recursive: true })
} catch (dirError) {
// Ignore errors - directory might already exist or other issues
// The important thing is we tried to create it
}
fileExists = await fileExistsAtPath(absolutePath)
task.diffViewProvider.editType = fileExists ? "modify" : "create"
}
@ -311,6 +319,14 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
fileExists = task.diffViewProvider.editType === "modify"
} else {
const absolutePath = path.resolve(task.cwd, relPath)
// Ensure parent directories exist before checking file existence
const dirPath = path.dirname(absolutePath)
try {
await fs.mkdir(dirPath, { recursive: true })
} catch (dirError) {
// Ignore errors - directory might already exist or other issues
// The important thing is we tried to create it
}
fileExists = await fileExistsAtPath(absolutePath)
task.diffViewProvider.editType = fileExists ? "modify" : "create"
}