From cf33a936409654d98fde63d1ffd34344d3898c82 Mon Sep 17 00:00:00 2001 From: Joe Date: Fri, 6 Feb 2026 14:17:14 -0600 Subject: [PATCH] Refactor StdBackend to improve file watcher reliability - Updated the `StdBackend` to utilize `notify::RecommendedWatcher` instead of a platform-specific watcher type, enhancing cross-platform event delivery. - Simplified the debouncer initialization by removing platform-specific configurations, streamlining the code for better maintainability. - Improved documentation to reflect changes in the watcher implementation and clarify the debouncing mechanism. --- crates/memofs/src/std_backend.rs | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/crates/memofs/src/std_backend.rs b/crates/memofs/src/std_backend.rs index 828b1acf..96cfb590 100644 --- a/crates/memofs/src/std_backend.rs +++ b/crates/memofs/src/std_backend.rs @@ -5,19 +5,11 @@ use std::{collections::HashSet, io}; use crossbeam_channel::{Receiver, Sender}; use notify::RecursiveMode; use notify_debouncer_full::{ - new_debouncer_opt, + new_debouncer, notify::event::{CreateKind, EventKind, ModifyKind, RemoveKind, RenameMode}, - DebounceEventResult, Debouncer, FileIdMap, + DebounceEventResult, Debouncer, RecommendedCache, }; -/// On macOS, FSEvents silently fails to deliver events in `/var/folders/` -/// temp directories (a known platform limitation). Use PollWatcher instead -/// for reliable event delivery on all directory types. -#[cfg(target_os = "macos")] -type PlatformWatcher = notify::PollWatcher; -#[cfg(not(target_os = "macos"))] -type PlatformWatcher = notify::RecommendedWatcher; - use crate::{DirEntry, Metadata, ReadDir, VfsBackend, VfsEvent}; /// Critical errors from the file watcher that indicate watching is no longer reliable. @@ -63,7 +55,7 @@ pub type CriticalErrorHandler = Box bool + Send /// `VfsBackend` that uses `std::fs` and the `notify` crate. pub struct StdBackend { - debouncer: Debouncer, + debouncer: Debouncer, watcher_receiver: Receiver, watches: HashSet, /// Receiver for critical errors from the watcher thread. @@ -106,15 +98,11 @@ impl StdBackend { event_tx: Sender, error_tx: Sender, error_handler: CriticalErrorHandler, - ) -> Debouncer { + ) -> Debouncer { // Use 50ms debounce timeout (same as the old v4 implementation) let debounce_timeout = Duration::from_millis(50); - // On macOS we use PollWatcher which needs an explicit poll interval. - // On other platforms this field is ignored by RecommendedWatcher. - let config = notify::Config::default().with_poll_interval(debounce_timeout); - - new_debouncer_opt::<_, PlatformWatcher, FileIdMap>( + new_debouncer( debounce_timeout, None, // Use default tick rate move |result: DebounceEventResult| { @@ -157,8 +145,6 @@ impl StdBackend { } } }, - FileIdMap::new(), - config, ) .expect("Failed to create file watcher debouncer") }