From 7d7979ac382c081920ea9ad8b649d77d1dfde710 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Wed, 26 Nov 2025 23:55:44 +0000 Subject: [PATCH] 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 --- src/core/tools/WriteToFileTool.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/core/tools/WriteToFileTool.ts b/src/core/tools/WriteToFileTool.ts index 475be66c2c..3d42d60924 100644 --- a/src/core/tools/WriteToFileTool.ts +++ b/src/core/tools/WriteToFileTool.ts @@ -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" }