Skip to content

Bug: write_to_file tool throws ENOENT error when creating files in non-existent subdirectories #9634

@ivanenev

Description

@ivanenev

Bug Report: write_to_file Tool ENOENT Error

Problem Description

The write_to_file tool in the Roo-Cline VS Code extension (version 3.34.5) throws ENOENT (No such file or directory) errors when attempting to create files in non-existent subdirectories. The issue occurs in the handlePartial method of the DBr class.

Root Cause

The handlePartial method attempts to check if a file exists using ci(A) (file existence check) before ensuring parent directories exist. When creating files in new subdirectories, this check fails with ENOENT because the parent directories don't exist yet.

Problematic Code (line ~5966):

c=await ci(A),e.diffViewProvider.editType=c?"modify":"create"

Solution Applied

Wrapped the file existence check in a try-catch block that creates parent directories recursively when the check fails.

Fixed Code:

try{c=await ci(A)}catch{let D=gAe.default.dirname(A);try{await HLe.default.mkdir(D,{recursive:!0})}catch{}c=!1}e.diffViewProvider.editType=c?"modify":"create"

Fix Details

  • File Modified: /home/mts/.vscode-oss/extensions/rooveterinaryinc.roo-cline-3.34.5-universal/dist/extension.js
  • Method Fixed: DBr.handlePartial
  • Line Number: ~5966

Testing Results

The fix was successfully tested with multiple scenarios:

  1. ✅ Creating files in simple subdirectories (test_subdir/test_file.txt)
  2. ✅ Creating files in deeply nested directories (deep/nested/subdirectory/test1.txt)
  3. ✅ Creating files in directories with special characters (test-dir_with-special.chars/test2.txt)
  4. ✅ Creating files in directories with spaces (level 1/level 2/level 3/test3.txt)

All test cases completed successfully without ENOENT errors.

Technical Implementation

The fix uses:

  • Try-catch wrapper around file existence check
  • Recursive directory creation with mkdir(D, {recursive:!0})
  • Proper error handling for directory creation failures
  • Maintains existing edit type logic for existing vs new files

Impact

This fix resolves a critical usability issue where AI assistants could not create files in new subdirectories, significantly improving the tool's reliability for complex file operations.

Labels

bug, enhancement, tools, file-system

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue/PR - TriageNew issue. Needs quick review to confirm validity and assign labels.bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions