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
This commit is contained in:
Cole Miller 2024-12-12 01:54:14 -05:00 committed by GitHub
parent cc97e682d5
commit 543a3ef5e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 4 deletions

1
Cargo.lock generated
View file

@ -4787,6 +4787,7 @@ dependencies = [
"git2",
"gpui",
"libc",
"log",
"notify",
"objc",
"parking_lot",

View file

@ -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

View file

@ -695,10 +695,13 @@ impl Fs for RealFs {
let pending_paths: Arc<Mutex<Vec<PathEvent>>> = 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