From d3ce82e82cae8935220ba6bed4478a951fed44ff Mon Sep 17 00:00:00 2001
From: Kirill Bulatov <kirill@zed.dev>
Date: Wed, 15 Nov 2023 18:37:15 +0200
Subject: [PATCH] Fix the new test

---
 crates/project/src/worktree.rs       | 47 +++++++++++++++++++---------
 crates/project/src/worktree_tests.rs |  7 ++---
 2 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs
index 49ba8c93c4..6508a8635c 100644
--- a/crates/project/src/worktree.rs
+++ b/crates/project/src/worktree.rs
@@ -320,7 +320,7 @@ impl Worktree {
                     if new_scan_exclude_files != this.snapshot.scan_exclude_files {
                         this.snapshot.scan_exclude_files = new_scan_exclude_files;
                         log::info!(
-                            "Re-scanning due to new scan exclude files: {:?}",
+                            "Re-scanning directories, new scan exclude files: {:?}",
                             this.snapshot
                                 .scan_exclude_files
                                 .iter()
@@ -343,7 +343,6 @@ impl Worktree {
                             cx,
                         );
                         this.is_scanning = watch::channel_with(true);
-                        // TODO kb change more state? will this even work now?
                     }
                 }
             });
@@ -3489,18 +3488,26 @@ impl BackgroundScanner {
     }
 
     async fn scan_dir(&self, job: &ScanJob) -> Result<()> {
-        log::debug!("scan directory {:?}", job.path);
-
-        let mut ignore_stack = job.ignore_stack.clone();
-        let mut new_ignore = None;
-        let (root_abs_path, root_char_bag, next_entry_id) = {
-            let snapshot = &self.state.lock().snapshot;
-            (
-                snapshot.abs_path().clone(),
-                snapshot.root_char_bag,
-                self.next_entry_id.clone(),
-            )
-        };
+        let root_abs_path;
+        let mut ignore_stack;
+        let mut new_ignore;
+        let root_char_bag;
+        let next_entry_id;
+        {
+            let state = self.state.lock();
+            let snapshot = &state.snapshot;
+            root_abs_path = snapshot.abs_path().clone();
+            if snapshot.is_abs_path_excluded(&job.abs_path) {
+                log::error!("skipping excluded directory {:?}", job.path);
+                return Ok(());
+            }
+            log::debug!("scanning directory {:?}", job.path);
+            ignore_stack = job.ignore_stack.clone();
+            new_ignore = None;
+            root_char_bag = snapshot.root_char_bag;
+            next_entry_id = self.next_entry_id.clone();
+            drop(state);
+        }
 
         let mut dotgit_path = None;
         let mut root_canonical_path = None;
@@ -3515,8 +3522,18 @@ impl BackgroundScanner {
                     continue;
                 }
             };
-
             let child_name = child_abs_path.file_name().unwrap();
+            {
+                let mut state = self.state.lock();
+                if state.snapshot.is_abs_path_excluded(&child_abs_path) {
+                    let relative_path = job.path.join(child_name);
+                    log::debug!("skipping excluded child entry {relative_path:?}");
+                    state.remove_path(&relative_path);
+                    continue;
+                }
+                drop(state);
+            }
+
             let child_path: Arc<Path> = job.path.join(child_name).into();
             let child_metadata = match self.fs.metadata(&child_abs_path).await {
                 Ok(Some(metadata)) => metadata,
diff --git a/crates/project/src/worktree_tests.rs b/crates/project/src/worktree_tests.rs
index 4c0df4dd7d..bd548bb9cb 100644
--- a/crates/project/src/worktree_tests.rs
+++ b/crates/project/src/worktree_tests.rs
@@ -935,10 +935,9 @@ async fn test_ignore_exclusions(cx: &mut TestAppContext) {
             &[
                 "src/foo/foo.rs",
                 "src/foo/another.rs",
-                // TODO kb
-                // "node_modules/.DS_Store",
-                // "src/.DS_Store",
-                // ".DS_Store",
+                "node_modules/.DS_Store",
+                "src/.DS_Store",
+                ".DS_Store",
             ],
             &["target/index", "node_modules/prettier/package.json"],
             &["src/lib.rs", "src/bar/bar.rs", ".gitignore"],