diff --git a/objdiff-cli/src/cmd/diff.rs b/objdiff-cli/src/cmd/diff.rs index 7cf4edc8..6dcec1c3 100644 --- a/objdiff-cli/src/cmd/diff.rs +++ b/objdiff-cli/src/cmd/diff.rs @@ -24,7 +24,7 @@ use objdiff_core::{ bindings::diff::DiffResult, build::{ BuildConfig, BuildStatus, - watcher::{Watcher, create_watcher}, + watcher::{AnyDebouncer, create_watcher}, }, config::{ ProjectConfig, ProjectObject, ProjectObjectMetadata, ProjectOptions, apply_project_options, @@ -271,7 +271,7 @@ pub struct AppState { pub prev_obj: Option<(Object, ObjectDiff)>, pub reload_time: Option, pub time_format: Vec>, - pub watcher: Option, + pub watcher: Option, pub modified: Arc, pub diff_obj_config: DiffObjConfig, pub mapping_config: MappingConfig, diff --git a/objdiff-core/src/build/watcher.rs b/objdiff-core/src/build/watcher.rs index f9f8cf9f..8cb1c928 100644 --- a/objdiff-core/src/build/watcher.rs +++ b/objdiff-core/src/build/watcher.rs @@ -1,6 +1,7 @@ use std::{ + error::Error, fs, - path::{Path, PathBuf}, + path::{Component, Path, PathBuf, Prefix}, sync::{ Arc, atomic::{AtomicBool, Ordering}, @@ -13,10 +14,26 @@ use globset::GlobSet; use notify::RecursiveMode; use notify_debouncer_full::{DebounceEventResult, new_debouncer_opt}; -pub type Watcher = notify_debouncer_full::Debouncer< - notify::RecommendedWatcher, - notify_debouncer_full::RecommendedCache, ->; +pub enum AnyDebouncer { + NativeDebouncer( + notify_debouncer_full::Debouncer< + notify::RecommendedWatcher, + notify_debouncer_full::RecommendedCache, + >, + ), + NetworkFSDebouncer( + notify_debouncer_full::Debouncer, + ), +} + +impl AnyDebouncer { + pub fn watch(&mut self, path: impl AsRef, mode: RecursiveMode) -> notify::Result<()> { + match self { + Self::NativeDebouncer(d) => d.watch(path, mode), + Self::NetworkFSDebouncer(d) => d.watch(path, mode), + } + } +} pub struct WatcherState { pub config_path: Option, @@ -25,52 +42,109 @@ pub struct WatcherState { pub patterns: GlobSet, } +pub fn is_win_networkfs_path(path: &Path) -> Result> { + let mut components = path.components(); + let Some(first_component) = components.next() else { + return Err("Path should not be empty")?; + }; + match first_component { + Component::Prefix(prefix) => Ok(matches!(prefix.kind(), Prefix::VerbatimUNC(..))), + _ => Err("Path should have a prefix (use canonicalize)")?, + } +} + pub fn create_watcher( modified: Arc, project_dir: &Path, patterns: GlobSet, ignore_patterns: GlobSet, waker: Waker, -) -> notify::Result { +) -> notify::Result { let base_dir = fs::canonicalize(project_dir)?; let base_dir_clone = base_dir.clone(); let timeout = Duration::from_millis(200); let config = notify::Config::default().with_poll_interval(Duration::from_secs(2)); - let mut debouncer = new_debouncer_opt( - timeout, - None, - move |result: DebounceEventResult| match result { - Ok(events) => { - let mut any_match = false; - for event in events.iter() { - if !matches!( - event.kind, - notify::EventKind::Modify(..) - | notify::EventKind::Create(..) - | notify::EventKind::Remove(..) - ) { + let handler = move |result: DebounceEventResult| match result { + Ok(events) => { + let mut any_match = false; + for event in events.iter() { + if !matches!( + event.kind, + notify::EventKind::Modify(..) + | notify::EventKind::Create(..) + | notify::EventKind::Remove(..) + ) { + continue; + } + for path in &event.paths { + let Ok(path) = path.strip_prefix(&base_dir_clone) else { continue; + }; + if patterns.is_match(path) && !ignore_patterns.is_match(path) { + log::debug!("File modified: {}", path.display()); + any_match = true; } - for path in &event.paths { - let Ok(path) = path.strip_prefix(&base_dir_clone) else { - continue; - }; - if patterns.is_match(path) && !ignore_patterns.is_match(path) { - log::debug!("File modified: {}", path.display()); - any_match = true; - } - } - } - if any_match { - modified.store(true, Ordering::Relaxed); - waker.wake_by_ref(); } } - Err(errors) => errors.iter().for_each(|e| log::error!("Watch error: {e:?}")), - }, - notify_debouncer_full::RecommendedCache::new(), - config, - )?; + if any_match { + modified.store(true, Ordering::Relaxed); + waker.wake_by_ref(); + } + } + Err(errors) => errors.iter().for_each(|e| { + let path_is_ignored = e.paths.iter().all(|absolute_path| { + let Ok(path) = absolute_path.strip_prefix(&base_dir_clone) else { + return false; + }; + ignore_patterns.is_match(path) + }); + // Don't directly report watch errors for path that are ignored + if path_is_ignored { + log::debug!("Watch error (ignored path) : {e:?}"); + return; + } + log::error!("Watch error: {e:?}"); + }), + }; + + let mut debouncer; + // On Windows (only), check if base_dir is inside a network fs, if so switch to polling watcher + #[cfg(windows)] + { + let use_polling = is_win_networkfs_path(base_dir.as_path()).unwrap_or_default(); + if use_polling { + // Use more frequent polling to compensate the slow polls + let poll_config = config.with_poll_interval(Duration::from_secs(1)); + log::debug!("Using NetworkFS debouncer"); + debouncer = AnyDebouncer::NetworkFSDebouncer(new_debouncer_opt( + timeout, + None, + handler, + notify_debouncer_full::NoCache::new(), + poll_config, + )?); + } else { + log::debug!("Using Native debouncer"); + debouncer = AnyDebouncer::NativeDebouncer(new_debouncer_opt( + timeout, + None, + handler, + notify_debouncer_full::RecommendedCache::new(), + config, + )?); + } + } + #[cfg(not(windows))] + { + debouncer = AnyDebouncer::NativeDebouncer(new_debouncer_opt( + timeout, + None, + handler, + notify_debouncer_full::RecommendedCache::new(), + config, + )?); + } + debouncer.watch(base_dir, RecursiveMode::Recursive)?; Ok(debouncer) } diff --git a/objdiff-gui/src/app.rs b/objdiff-gui/src/app.rs index 0c2e061d..9f94ffa7 100644 --- a/objdiff-gui/src/app.rs +++ b/objdiff-gui/src/app.rs @@ -15,7 +15,7 @@ use egui::text::LayoutJob; use filetime::FileTime; use globset::Glob; use objdiff_core::{ - build::watcher::{Watcher, create_watcher}, + build::watcher::{AnyDebouncer, create_watcher}, config::{ ProjectConfig, ProjectConfigInfo, ProjectObject, ScratchConfig, apply_project_options, build_globset, default_ignore_patterns, default_watch_patterns, @@ -461,7 +461,7 @@ pub struct App { view_state: ViewState, state: AppStateRef, modified: Arc, - watcher: Option, + watcher: Option, app_path: Option, relaunch_path: Rc>>, should_relaunch: bool,