Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Open gitignored subdirectories as their own workspace (#60918)
Mikayla Makie9d6cd2f053a
diff --git a/crates/project/src/project.rs b/crates/project/src/project.rsindex 6a08a291e31..5f104cf3128 100644--- a/crates/project/src/project.rs+++ b/crates/project/src/project.rs@@ -2520,17 +2520,20 @@ impl Project {.filter_map(|worktree| {let worktree = worktree.read(cx);let abs_path = worktree.abs_path();- let relative_path = path_style.strip_prefix(path, abs_path.as_ref());- let is_dir = relative_path- .as_ref()- .and_then(|p| worktree.entry_for_path(p))- .is_some_and(|e| e.is_dir());+ let relative_path = path_style.strip_prefix(path, abs_path.as_ref())?;// Don't exclude the worktree root itself, only actual subdirectories- let is_subdir = relative_path- .as_ref()- .is_some_and(|p| !p.as_ref().as_unix_str().is_empty());- let contains =- relative_path.is_some() && (!exclude_sub_dirs || !is_dir || !is_subdir);+ let is_subpath = !relative_path.is_empty();+ // Gitignored subtrees aren't scanned, so their contents don't+ // meaningfully belong to this project (e.g. nested checkouts+ // in an ignored directory). Treat such paths as not contained+ // so opening them behaves like opening an unrelated path.+ if is_subpath && worktree.is_path_ignored(&relative_path) {+ return None;+ }+ let is_dir = worktree+ .entry_for_path(&relative_path)+ .is_some_and(|e| e.is_dir());+ let contains = !exclude_sub_dirs || !is_dir || !is_subpath;contains.then(|| worktree.is_visible())}).max()@@ -2551,10 +2554,9 @@ impl Project {.filter_map(|worktree| {let worktree = worktree.read(cx);let abs_path = worktree.abs_path();- let relative_path = path_style.strip_prefix(path, abs_path.as_ref());- let is_subpath = relative_path- .as_ref()- .is_some_and(|p| !p.as_ref().as_unix_str().is_empty());+ let relative_path = path_style.strip_prefix(path, abs_path.as_ref())?;+ let is_subpath =+ !relative_path.is_empty() && !worktree.is_path_ignored(&relative_path);is_subpath.then(|| worktree.is_visible())}).max()diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rsindex 2b2ba5dece6..e4a730f1bc1 100644--- a/crates/project/tests/integration/project_tests.rs+++ b/crates/project/tests/integration/project_tests.rs@@ -8636,6 +8636,74 @@ async fn test_search_in_gitignored_dirs(cx: &mut gpui::TestAppContext) {);}+#[gpui::test]+async fn test_visibility_for_paths_in_gitignored_dirs(cx: &mut gpui::TestAppContext) {+ init_test(cx);++ let fs = FakeFs::new(cx.executor());+ fs.insert_tree(+ path!("/dir"),+ json!({+ ".git": {},+ ".gitignore": ".checkouts/\n",+ "src": {+ "main.rs": "fn main() {}"+ },+ ".checkouts": {+ "worktrees": {+ "foo": {+ "README.md": "hello"+ }+ }+ },+ }),+ )+ .await;+ let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;+ cx.run_until_parked();++ project.read_with(cx, |project, cx| {+ let root = PathBuf::from(path!("/dir"));+ let file = PathBuf::from(path!("/dir/src/main.rs"));+ let ignored_dir = PathBuf::from(path!("/dir/.checkouts/worktrees/foo"));+ let file_in_ignored_dir = PathBuf::from(path!("/dir/.checkouts/worktrees/foo/README.md"));++ assert_eq!(+ project.visibility_for_paths(std::slice::from_ref(&root), true, cx),+ Some(true)+ );+ assert_eq!(+ project.visibility_for_paths(std::slice::from_ref(&file), true, cx),+ Some(true)+ );+ assert_eq!(+ project.visibility_for_subpaths(std::slice::from_ref(&file), cx),+ Some(true)+ );++ // Paths inside gitignored directories aren't scanned, so they aren't+ // considered contained by the project; opening one should behave like+ // opening an unrelated path.+ for path in [&ignored_dir, &file_in_ignored_dir] {+ assert_eq!(+ project.visibility_for_paths(std::slice::from_ref(path), true, cx),+ None,+ "{path:?} should not be considered contained"+ );+ assert_eq!(+ project.visibility_for_paths(std::slice::from_ref(path), false, cx),+ None,+ "{path:?} should not be considered contained"+ );+ assert_eq!(+ project.visibility_for_subpaths(std::slice::from_ref(path), cx),+ None,+ "{path:?} should not be considered a contained subpath"+ );+ }+ });+}+#[gpui::test]async fn test_search_with_unicode(cx: &mut gpui::TestAppContext) {init_test(cx);diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rsindex ee04ab6d7bc..ab414f08709 100644--- a/crates/worktree/src/worktree.rs+++ b/crates/worktree/src/worktree.rs@@ -2808,6 +2808,17 @@ impl Snapshot {})}+ /// Whether `path` is gitignored, or lies inside a gitignored directory.+ ///+ /// The contents of ignored directories aren't scanned until explicitly+ /// expanded, so when `path` has no entry this falls back to the ignore+ /// status of its nearest scanned ancestor.+ pub fn is_path_ignored(&self, path: &RelPath) -> bool {+ path.ancestors()+ .find_map(|ancestor| self.entry_for_path(ancestor))+ .is_some_and(|entry| entry.is_ignored)+ }+/// Resolves a path to an executable using the following heuristics:////// 1. If the path starts with `~`, it is expanded to the user's home directory.diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rsindex d4b394429a7..1029c82ef6c 100644--- a/crates/zed/src/zed.rs+++ b/crates/zed/src/zed.rs@@ -6222,6 +6222,95 @@ mod tests {assert_eq!(cx.windows().len(), 1, "Should still have only 1 window");}+ #[gpui::test]+ async fn test_open_paths_in_gitignored_dir_opens_new_workspace(cx: &mut TestAppContext) {+ let app_state = init_test(cx);++ app_state+ .fs+ .as_fake()+ .insert_tree(+ path!("/project"),+ json!({+ ".git": {},+ ".gitignore": ".checkouts/\n",+ "src": {+ "main.rs": "fn main() {}"+ },+ ".checkouts": {+ "worktrees": {+ "foo": {+ "README.md": "hello"+ }+ }+ }+ }),+ )+ .await;++ cx.update(|cx| {+ open_paths(+ &[PathBuf::from(path!("/project"))],+ app_state.clone(),+ workspace::OpenOptions::default(),+ cx,+ )+ })+ .await+ .unwrap();+ cx.run_until_parked();+ assert_eq!(cx.update(|cx| cx.windows().len()), 1);++ // Opening a directory inside a gitignored folder must not be treated+ // as contained by the open project: its contents were never scanned,+ // and it may be an independent checkout (e.g. a git worktree kept in+ // an ignored directory). It should become its own workspace root+ // instead.+ cx.update(|cx| {+ open_paths(+ &[PathBuf::from(path!("/project/.checkouts/worktrees/foo"))],+ app_state.clone(),+ workspace::OpenOptions::default(),+ cx,+ )+ })+ .await+ .unwrap();+ cx.run_until_parked();++ let workspace_roots = cx.update(|cx| {+ cx.windows()+ .into_iter()+ .filter_map(|window| window.downcast::<MultiWorkspace>())+ .flat_map(|window| {+ let mut roots = Vec::new();+ if let Ok(multi_workspace) = window.read(cx) {+ for workspace in multi_workspace.workspaces() {+ roots.push(+ workspace+ .read(cx)+ .worktrees(cx)+ .map(|worktree| worktree.read(cx).abs_path().to_path_buf())+ .collect::<Vec<_>>(),+ );+ }+ }+ roots+ })+ .collect::<Vec<_>>()+ });+ assert!(+ workspace_roots.contains(&vec![PathBuf::from(path!(+ "/project/.checkouts/worktrees/foo"+ ))]),+ "the gitignored directory should be the root of its own workspace, got {workspace_roots:?}"+ );+ assert!(+ workspace_roots.contains(&vec![PathBuf::from(path!("/project"))]),+ "the original project workspace should be unchanged, got {workspace_roots:?}"+ );+ }+#[gpui::test]async fn test_quit_checks_all_workspaces_for_dirty_items(cx: &mut TestAppContext) {let app_state = init_test(cx);