Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

agent: Fix tool paths preferring files in subdirectories named after the project root (#56230)

procr13379fe2931297c3
diff --git a/crates/agent/src/tools/read_file_tool.rs b/crates/agent/src/tools/read_file_tool.rs
index 6b626566b9a..95301ee1523 100644
--- a/crates/agent/src/tools/read_file_tool.rs
+++ b/crates/agent/src/tools/read_file_tool.rs
@@ -610,6 +610,47 @@ mod test {
);
}
+ // When a worktree is named "foo" and contains a subdirectory also named "foo",
+ // read_file({"path": "foo/test.txt"}) should return the file at the worktree
+ // root (as the tool schema promises), not the one inside the foo/ subdirectory.
+ #[gpui::test]
+ async fn test_read_file_worktree_root_not_shadowed_by_subdir(cx: &mut TestAppContext) {
+ init_test(cx);
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ path!("/foo"),
+ json!({
+ "test.txt": "root content",
+ "foo": {
+ "test.txt": "subdir content"
+ }
+ }),
+ )
+ .await;
+ let project = Project::test(fs.clone(), [path!("/foo").as_ref()], cx).await;
+ let action_log = cx.new(|_| ActionLog::new(project.clone()));
+ let tool = Arc::new(ReadFileTool::new(project, action_log, true));
+
+ // The tool schema says the first component must be the worktree root name,
+ // so "foo/test.txt" means test.txt at the root of the "foo" worktree.
+ let result = cx
+ .update(|cx| {
+ let input = ReadFileToolInput {
+ path: "foo/test.txt".into(),
+ start_line: None,
+ end_line: None,
+ };
+ tool.run(
+ ToolInput::resolved(input),
+ ToolCallEventStream::test().0,
+ cx,
+ )
+ })
+ .await;
+ assert_eq!(result.unwrap(), "root content".into());
+ }
+
#[gpui::test]
async fn test_read_file_with_line_range(cx: &mut TestAppContext) {
init_test(cx);
diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs
index 9e47bd60644..9408b69a29d 100644
--- a/crates/project/src/project.rs
+++ b/crates/project/src/project.rs
@@ -4977,18 +4977,33 @@ impl Project {
}
}
} else {
+ // First pass: for each worktree, try two interpretations of the path and
+ // return whichever finds an existing entry first:
+ // (a) Strip the worktree root name as a prefix.
+ // (b) Treat the path as a literal worktree-relative path.
for worktree in worktree_store.visible_worktrees(cx) {
let worktree = worktree.read(cx);
- if let Ok(rel_path) = RelPath::new(path, path_style) {
- if let Some(entry) = worktree.entry_for_path(&rel_path) {
- return Some(ProjectPath {
- worktree_id: worktree.id(),
- path: entry.path.clone(),
- });
- }
+ if let Ok(relative_path) = path.strip_prefix(worktree.root_name().as_std_path())
+ && let Ok(rel_path) = RelPath::new(relative_path, path_style)
+ && let Some(entry) = worktree.entry_for_path(&rel_path)
+ {
+ return Some(ProjectPath {
+ worktree_id: worktree.id(),
+ path: entry.path.clone(),
+ });
+ }
+ if let Ok(rel_path) = RelPath::new(path, path_style)
+ && let Some(entry) = worktree.entry_for_path(&rel_path)
+ {
+ return Some(ProjectPath {
+ worktree_id: worktree.id(),
+ path: entry.path.clone(),
+ });
}
}
+ // Second pass: strip the worktree root name prefix without requiring the
+ // entry to exist, to allow resolving paths that don't exist yet.
for worktree in worktree_store.visible_worktrees(cx) {
let worktree_root_name = worktree.read(cx).root_name();
if let Ok(relative_path) = path.strip_prefix(worktree_root_name.as_std_path())