Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file watching for symlinked workspace on Linux #19159

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions crates/fs/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,6 @@ impl Fs for RealFs {
watcher.add(parent).log_err();
}

// Check if path is a symlink and follow the target parent
if let Some(target) = self.read_link(&path).await.ok() {
watcher.add(&target).ok();
if let Some(parent) = target.parent() {
watcher.add(parent).log_err();
}
}

(
Box::pin(rx.filter_map({
let watcher = watcher.clone();
Expand Down
49 changes: 45 additions & 4 deletions crates/fs/src/linux_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl LinuxWatcher {
impl Watcher for LinuxWatcher {
fn add(&self, path: &std::path::Path) -> gpui::Result<()> {
let root_path = path.to_path_buf();
// Canonicalize the root path to handle cases where it's a symlink or below one
let target_path = std::fs::canonicalize(&path).ok();

let is_canonical = target_path == Some(root_path.clone());

let tx = self.tx.clone();
let pending_paths = self.pending_path_events.clone();
Expand All @@ -44,10 +48,47 @@ impl Watcher for LinuxWatcher {
.paths
.iter()
.filter_map(|event_path| {
event_path.starts_with(&root_path).then(|| PathEvent {
path: event_path.clone(),
kind,
})
// we canonicalize the parent and join with file name to handle cases
// where the file doesn't exist anymore
if let Some(parent) = event_path.parent() {
if event_path.clone().starts_with(parent) {
if !is_canonical {
if let Ok(canonical_parent) = std::fs::canonicalize(&parent)
{
if let Some(file_name) = event_path.file_name() {
return Some(PathEvent {
path: canonical_parent.join(file_name),
kind,
});
}
}
} else {
return Some(PathEvent {
path: event_path.clone(),
kind,
});
}
} else {
if let Ok(canonical_parent) = std::fs::canonicalize(&parent) {
if event_path.starts_with(canonical_parent.clone()) {
if !is_canonical {
if let Some(file_name) = event_path.file_name() {
return Some(PathEvent {
path: canonical_parent.join(file_name),
kind,
});
}
} else {
return Some(PathEvent {
path: event_path.clone(),
kind,
});
}
}
}
}
}
None
})
.collect::<Vec<_>>();

Expand Down
Loading