zed icon indicating copy to clipboard operation
zed copied to clipboard

Agent Panel: edit_file tool failing to create new files, consistently

Open subtleGradient opened this issue 8 months ago • 0 comments

[!IMPORTANT] TL;DR -- the bug is:

  1. checking the operation mode (Create or Overwrite vs Edit) -- GOOD
  2. after this path validation step -- BAD

Summary

Agent Panel: edit_file tool failing to create new files

The edit_file tool incorrectly reports that paths don't exist in the project when attempting to create new files in existing directories.

Description

Steps to trigger the problem:

  1. Open Zed with a project that has an existing directory (e.g., "TODO")
  2. Ask the agent to create a new file in that directory (e.g., "TODO/new-file.md")
  3. The agent tries to use the edit_file tool with create_or_overwrite: true
  4. The tool fails with error "Path not found in project" even though the directory exists

Actual Behavior: When the agent attempts to create a new file in an existing directory using the edit_file tool, it fails with the error message: "Path TODO/persist-navigation-state-hot-reload-expo-router.md not found in project" even though the TODO directory exists.

Expected Behavior: The edit_file tool should create the new file in the existing directory when create_or_overwrite is set to true, as long as the parent directory exists in the project.

Zed Version and System Specs

Output of "zed: Copy System Specs Into Clipboard"

Zed: v0.186.8 (Zed) OS: macOS 15.3.1 Memory: 16 GiB Architecture: aarch64

also happened in Zed: v0.186.7 (Zed)


example 1

User

ok, now create the TODO file according to the rules

Agent

Use Tool: edit_file (call_SQ1eNzNIHfLSO17Z16tq6rIG)

{
  "create_or_overwrite": true,
  "display_description": "Create TODO for persisting navigation state across hot reloads in Expo Router (mo app) with research findings, implementation plan, and evaluation instructions.",
  "path": "TODO/persist-navigation-state-hot-reload-expo-router.md"
}

Tool Results: call_SQ1eNzNIHfLSO17Z16tq6rIG (Error)

Path TODO/persist-navigation-state-hot-reload-expo-router.md not found in project

Agent

It appears there is a technical issue preventing me from creating new files in the TODO directory, even though I can read and list files there. This may be a system-level restriction or a temporary glitch.

Would you like to try creating the file manually and then I can populate it with the content, or would you like to troubleshoot the file creation permissions? If you create an empty file at TODO/persist-navigation-state-hot-reload-expo-router.md, I can immediately fill it in with the correct content and structure according to your project rules.

example 2

User

ok, now create the TODO file according to the rules

Agent

Use Tool: edit_file (call_CfIiwHXTdZmYuJoOa6aBBLX6)

{
  "display_description": "Create TODO for persisting navigation state across hot reloads in Expo Router (mo app) with research findings and implementation plan.",
  "mode": "create",
  "path": "TODO/persist-navigation-state-hot-reload-expo-router.md"
}

Tool Results: call_CfIiwHXTdZmYuJoOa6aBBLX6 (Error)

Path TODO/persist-navigation-state-hot-reload-expo-router.md not found in project

Agent

It appears that I'm unable to create a new file in the TODO directory due to a technical limitation. This is unexpected, as I can normally create files in other directories.

Would you like me to try creating the file in a different directory (such as the project root), or would you prefer to troubleshoot the file creation issue in the TODO directory first? If you have any specific instructions or a workaround, let me know and I'll follow your lead.


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:

https://github.com/zed-industries/zed/blob/c80aaca0c56cdeb7dfe6cd196c649385848bdfa8/crates/assistant_tools/src/edit_file_tool.rs#L174-L180

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:

https://github.com/zed-industries/zed/blob/c80aaca0c56cdeb7dfe6cd196c649385848bdfa8/crates/assistant_tools/src/edit_file_tool.rs#L208-L214

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.

subtleGradient avatar May 14 '25 16:05 subtleGradient