worktree: Don't attempt to watch non-existing global gitignore (#40476)

Lukas Wirth created

Release Notes:

- N/A *or* Added/Fixed/Improved ...

Change summary

crates/project/src/debugger/breakpoint_store.rs |  1 
crates/worktree/src/worktree.rs                 | 37 +++++++++++-------
2 files changed, 22 insertions(+), 16 deletions(-)

Detailed changes

crates/project/src/debugger/breakpoint_store.rs 🔗

@@ -164,7 +164,6 @@ pub struct BreakpointStore {
 
 impl BreakpointStore {
     pub fn init(client: &AnyProtoClient) {
-        log::error!("breakpoint store init");
         client.add_entity_request_handler(Self::handle_toggle_breakpoint);
         client.add_entity_message_handler(Self::handle_breakpoints_for_file);
     }

crates/worktree/src/worktree.rs 🔗

@@ -3614,25 +3614,32 @@ impl BackgroundScanner {
 
         log::trace!("containing git repository: {containing_git_repository:?}");
 
-        let mut global_gitignore_events =
-            if let Some(global_gitignore_path) = &paths::global_gitignore_path() {
-                self.state.lock().await.snapshot.global_gitignore =
-                    if self.fs.is_file(&global_gitignore_path).await {
-                        build_gitignore(global_gitignore_path, self.fs.as_ref())
-                            .await
-                            .ok()
-                            .map(Arc::new)
-                    } else {
-                        None
-                    };
+        let mut global_gitignore_events = if let Some(global_gitignore_path) =
+            &paths::global_gitignore_path()
+        {
+            let is_file = self.fs.is_file(&global_gitignore_path).await;
+            self.state.lock().await.snapshot.global_gitignore = if is_file {
+                build_gitignore(global_gitignore_path, self.fs.as_ref())
+                    .await
+                    .ok()
+                    .map(Arc::new)
+            } else {
+                None
+            };
+            if is_file
+                || matches!(global_gitignore_path.parent(), Some(path) if self.fs.is_dir(path).await)
+            {
                 self.fs
                     .watch(global_gitignore_path, FS_WATCH_LATENCY)
                     .await
                     .0
             } else {
-                self.state.lock().await.snapshot.global_gitignore = None;
-                Box::pin(futures::stream::empty())
-            };
+                Box::pin(futures::stream::pending())
+            }
+        } else {
+            self.state.lock().await.snapshot.global_gitignore = None;
+            Box::pin(futures::stream::pending())
+        };
 
         let (scan_job_tx, scan_job_rx) = channel::unbounded();
         {
@@ -3740,7 +3747,7 @@ impl BackgroundScanner {
                         Some([event, ..]) => {
                             self.update_global_gitignore(&event.path).await;
                         }
-                        _ => {},
+                        _ => (),
                     }
                 }
             }