From 543a3ef5e45397e2a2546fc068d574b2256cc500 Mon Sep 17 00:00:00 2001 From: Cole Miller Date: Thu, 12 Dec 2024 01:54:14 -0500 Subject: [PATCH] linux: Don't watch parent directory when target path already exists (#21854) The Linux watcher was unconditionally watching the parent directory of every watched path. This is needed in the case of config files that may not exist when the watch is set up, but not in other cases. Scoping the parent watch more narrowly cuts down on the amount of error logging from irrelevant file change notifications being sent to Zed (in my case it was picking up changes to a random file in `$HOME`). Release Notes: - N/A --- Cargo.lock | 1 + crates/fs/Cargo.toml | 1 + crates/fs/src/fs.rs | 11 +++++++---- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d8950b856c5cf2759f28932fc1f53a6212b31904..def04c674e5f7ba6cbfb2e8db21c7374cbafdaa0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4787,6 +4787,7 @@ dependencies = [ "git2", "gpui", "libc", + "log", "notify", "objc", "parking_lot", diff --git a/crates/fs/Cargo.toml b/crates/fs/Cargo.toml index 7a1cfaeaa59099872dafa473fb5a4c44bc574bba..fe145a6be589f746c612153d1ef806df8b398739 100644 --- a/crates/fs/Cargo.toml +++ b/crates/fs/Cargo.toml @@ -21,6 +21,7 @@ git.workspace = true git2.workspace = true gpui.workspace = true libc.workspace = true +log.workspace = true parking_lot.workspace = true paths.workspace = true rope.workspace = true diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs index 17571de76b775cfc9773f46af1f9fb59c46cda9a..0706c0e41cc98563df35d0a7eb3e3917cace5603 100644 --- a/crates/fs/src/fs.rs +++ b/crates/fs/src/fs.rs @@ -695,10 +695,13 @@ impl Fs for RealFs { let pending_paths: Arc>> = Default::default(); let watcher = Arc::new(linux_watcher::LinuxWatcher::new(tx, pending_paths.clone())); - watcher.add(&path).ok(); // Ignore "file doesn't exist error" and rely on parent watcher. - if let Some(parent) = path.parent() { - // watch the parent dir so we can tell when settings.json is created - watcher.add(parent).log_err(); + if watcher.add(path).is_err() { + // If the path doesn't exist yet (e.g. settings.json), watch the parent dir to learn when it's created. + if let Some(parent) = path.parent() { + if let Err(e) = watcher.add(parent) { + log::warn!("Failed to watch: {e}"); + } + } } // Check if path is a symlink and follow the target parent