Fix FS-related issues that were causing a test failure on linux (#13072)

This fixes `project_tests::rescan_and_remote_updates` .

That test was actually correctly failing, revealing two bugs on Linux.

Release Notes:

- Fixed an issue where file renames were not detected on Linux.
- Fixed performance problems caused by excessive file system events on
Linux.

---------

Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
Max Brunsfeld 2024-06-14 16:03:34 -07:00 committed by GitHub
parent fab4b01655
commit af45db6d1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 24 deletions

View file

@ -135,8 +135,6 @@ pub struct RealFs {
}
pub struct RealWatcher {
#[cfg(target_os = "linux")]
root_path: PathBuf,
#[cfg(target_os = "linux")]
fs_watcher: parking_lot::Mutex<notify::INotifyWatcher>,
}
@ -452,25 +450,38 @@ impl Fs for RealFs {
async fn watch(
&self,
path: &Path,
_latency: Duration,
latency: Duration,
) -> (
Pin<Box<dyn Send + Stream<Item = Vec<PathBuf>>>>,
Arc<dyn Watcher>,
) {
use parking_lot::Mutex;
let (tx, rx) = smol::channel::unbounded();
let pending_paths: Arc<Mutex<Vec<PathBuf>>> = Default::default();
let root_path = path.to_path_buf();
let file_watcher = notify::recommended_watcher({
let tx = tx.clone();
let pending_paths = pending_paths.clone();
move |event: Result<notify::Event, _>| {
if let Some(event) = event.log_err() {
tx.try_send(event.paths).ok();
let mut paths = event.paths;
paths.retain(|path| path.starts_with(&root_path));
if !paths.is_empty() {
paths.sort();
let mut pending_paths = pending_paths.lock();
if pending_paths.is_empty() {
tx.try_send(()).ok();
}
util::extend_sorted(&mut *pending_paths, paths, usize::MAX, PathBuf::cmp);
}
}
}
})
.expect("Could not start file watcher");
let watcher = Arc::new(RealWatcher {
root_path: path.to_path_buf(),
fs_watcher: parking_lot::Mutex::new(file_watcher),
});
@ -484,14 +495,13 @@ impl Fs for RealFs {
(
Box::pin(rx.filter_map({
let watcher = watcher.clone();
move |mut paths| {
paths.retain(|path| path.starts_with(&watcher.root_path));
move |_| {
let _ = watcher.clone();
let pending_paths = pending_paths.clone();
async move {
if paths.is_empty() {
None
} else {
Some(paths)
}
smol::Timer::after(latency).await;
let paths = std::mem::take(&mut *pending_paths.lock());
(!paths.is_empty()).then_some(paths)
}
}
})),