Skip to content

Commit 0f09217

Browse files
committed
Fix file watching for symlinks on Linux
1 parent 181c372 commit 0f09217

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

crates/fs/src/fs.rs

-8
Original file line numberDiff line numberDiff line change
@@ -655,14 +655,6 @@ impl Fs for RealFs {
655655
watcher.add(parent).log_err();
656656
}
657657

658-
// Check if path is a symlink and follow the target parent
659-
if let Some(target) = self.read_link(&path).await.ok() {
660-
watcher.add(&target).ok();
661-
if let Some(parent) = target.parent() {
662-
watcher.add(parent).log_err();
663-
}
664-
}
665-
666658
(
667659
Box::pin(rx.filter_map({
668660
let watcher = watcher.clone();

crates/fs/src/linux_watcher.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ impl LinuxWatcher {
2525
impl Watcher for LinuxWatcher {
2626
fn add(&self, path: &std::path::Path) -> gpui::Result<()> {
2727
let root_path = path.to_path_buf();
28+
// Canonicalize the root path to handle cases where it's a symlink or below one
29+
let target_path = std::fs::canonicalize(&path).ok();
2830

2931
let tx = self.tx.clone();
3032
let pending_paths = self.pending_path_events.clone();
@@ -44,10 +46,32 @@ impl Watcher for LinuxWatcher {
4446
.paths
4547
.iter()
4648
.filter_map(|event_path| {
47-
event_path.starts_with(&root_path).then(|| PathEvent {
48-
path: event_path.clone(),
49-
kind,
50-
})
49+
// we canonicalize the parent and join with file name to handle cases
50+
// where the file doesn't exist anymore
51+
if let Some(parent) = event_path.parent() {
52+
if let Ok(canonical_parent) = std::fs::canonicalize(&parent)
53+
{
54+
if event_path.clone().starts_with(canonical_parent.clone())
55+
|| event_path.starts_with(parent)
56+
{
57+
if target_path != Some(root_path.clone()) {
58+
// there are symlinks above workspace root
59+
if let Some(file_name) = event_path.file_name() {
60+
return Some(PathEvent {
61+
path: canonical_parent.join(file_name),
62+
kind,
63+
});
64+
}
65+
} else {
66+
return Some(PathEvent {
67+
path: event_path.clone(),
68+
kind,
69+
});
70+
}
71+
}
72+
}
73+
}
74+
None
5175
})
5276
.collect::<Vec<_>>();
5377

0 commit comments

Comments
 (0)