zed icon indicating copy to clipboard operation
zed copied to clipboard

assistant_tools: Allow edit_file tool to create new files in existing directories

Open subtleGradient opened this issue 8 months ago • 1 comments

maybe fixes #30706 (probably, I haven't actually tested the fix yet 😅)

Bug Analysis: edit_file Tool Failing to Create New Files

The Problem

The edit_file tool is failing to create new files within existing directories. When attempting to create a new file like TODO/persist-navigation-state-hot-reload-expo-router.md in an existing TODO folder, the tool returns an error: "Path TODO/persist-navigation-state-hot-reload-expo-router.md not found in project".

Root Cause

After analyzing the code, I've identified that the issue is in the run method of the EditFileTool struct in edit_file_tool.rs:

let Some(project_path) = project.read(cx).find_project_path(&input.path, cx) else {
    return Task::ready(Err(anyhow!(
        "Path {} not found in project",
        input.path.display()
    )))
    .into();
};

The problem is that find_project_path is being called unconditionally before checking the operation mode. This function likely checks if the exact path exists in the project. This works fine for existing files, but fails for new files that don't exist yet.

Importantly, the code checks the operation mode (Create or Overwrite vs Edit) after this path validation step:

let create_or_overwrite = match input.mode {
    EditFileMode::Create | EditFileMode::Overwrite => true,
    _ => false,
};
if !create_or_overwrite && !exists {
    return Err(anyhow!("{} not found", input.path.display()));
}

Why It's Failing

The tool is correctly handling non-existent files by checking the operation mode, but it never reaches this code because the path validation at the beginning already fails.

When find_project_path is called with a non-existent file path, it returns None, which immediately triggers the error.

The Solution

The fix would involve modifying the path validation logic to:

  1. For Edit mode: Check that the exact file path exists
  2. For Create/Overwrite modes: Only check that the parent directory exists

This way, when creating new files, the tool would validate that the parent directory exists rather than requiring the exact file path to exist.

Specifically, the code would need to check the operation mode first, then apply the appropriate path validation logic based on that mode.


This pull request modifies the EditFileTool implementation to improve handling of file creation and editing modes. The changes ensure proper validation of parent directories for file creation and provide clearer error handling.

Improvements to file handling logic:

  • Added logic to check if the EditFileMode is Create or Overwrite, and validated the existence of the parent directory when in these modes. If the parent directory does not exist, an appropriate error is returned.
  • Updated the logic for EditFileMode::Edit to ensure the exact path must exist in the project, maintaining stricter validation for editing operations.

subtleGradient avatar May 14 '25 16:05 subtleGradient

Warnings
:warning:

This PR is missing release notes.

Please add a "Release Notes" section that describes the change:

Release Notes:

- Added/Fixed/Improved ...

If your change is not user-facing, you can use "N/A" for the entry:

Release Notes:

- N/A

Generated by :no_entry_sign: dangerJS against a8252846cb8d904d86519565eca56afa6b881535

zed-industries-bot avatar May 14 '25 16:05 zed-industries-bot

Thanks for reporting and opening this PR! I went with a slightly different solution in #30909, which addresses the same issue.

osyvokon avatar May 19 '25 09:05 osyvokon