From 08d5d32dfccc4dc38c2aa95c81229bf031d3ac7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Czerniawski?= <33061335+lczerniawski@users.noreply.github.com> Date: Wed, 27 Mar 2024 23:47:46 +0100 Subject: feat: add `--watch-exclude` flag (#21935) This PR introduces the ability to exclude certain paths from the file watcher in Deno. This is particularly useful when running scripts in watch mode, as it allows developers to prevent unnecessary restarts when changes are made to files that do not affect the running script, or when executing scripts that generate new files which results in an infinite restart loop. --------- Co-authored-by: David Sherret --- cli/util/file_watcher.rs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'cli/util/file_watcher.rs') diff --git a/cli/util/file_watcher.rs b/cli/util/file_watcher.rs index 33b764bbf..50ae7c233 100644 --- a/cli/util/file_watcher.rs +++ b/cli/util/file_watcher.rs @@ -4,6 +4,7 @@ use crate::args::Flags; use crate::colors; use crate::util::fs::canonicalize_path; +use deno_config::glob::PathOrPatternSet; use deno_core::error::AnyError; use deno_core::error::JsError; use deno_core::futures::Future; @@ -244,6 +245,7 @@ where ) -> Result, F: Future>, { + let exclude_set = flags.resolve_watch_exclude_set()?; let (paths_to_watch_tx, mut paths_to_watch_rx) = tokio::sync::mpsc::unbounded_channel(); let (restart_tx, mut restart_rx) = tokio::sync::mpsc::unbounded_channel(); @@ -297,12 +299,12 @@ where } let mut watcher = new_watcher(watcher_sender.clone())?; - consume_paths_to_watch(&mut watcher, &mut paths_to_watch_rx); + consume_paths_to_watch(&mut watcher, &mut paths_to_watch_rx, &exclude_set); let receiver_future = async { loop { let maybe_paths = paths_to_watch_rx.recv().await; - add_paths_to_watcher(&mut watcher, &maybe_paths.unwrap()); + add_paths_to_watcher(&mut watcher, &maybe_paths.unwrap(), &exclude_set); } }; let operation_future = error_handler(operation( @@ -321,7 +323,7 @@ where continue; }, success = operation_future => { - consume_paths_to_watch(&mut watcher, &mut paths_to_watch_rx); + consume_paths_to_watch(&mut watcher, &mut paths_to_watch_rx, &exclude_set); // TODO(bartlomieju): print exit code here? info!( "{} {} {}. Restarting on file change...", @@ -334,11 +336,11 @@ where } ); }, - }; + } let receiver_future = async { loop { let maybe_paths = paths_to_watch_rx.recv().await; - add_paths_to_watcher(&mut watcher, &maybe_paths.unwrap()); + add_paths_to_watcher(&mut watcher, &maybe_paths.unwrap(), &exclude_set); } }; @@ -351,7 +353,7 @@ where print_after_restart(); continue; }, - }; + } } } @@ -376,28 +378,41 @@ fn new_watcher( .iter() .filter_map(|path| canonicalize_path(path).ok()) .collect(); + sender.send(paths).unwrap(); }, Default::default(), )?) } -fn add_paths_to_watcher(watcher: &mut RecommendedWatcher, paths: &[PathBuf]) { +fn add_paths_to_watcher( + watcher: &mut RecommendedWatcher, + paths: &[PathBuf], + paths_to_exclude: &PathOrPatternSet, +) { // Ignore any error e.g. `PathNotFound` + let mut watched_paths = Vec::new(); + for path in paths { + if paths_to_exclude.matches_path(path) { + continue; + } + + watched_paths.push(path.clone()); let _ = watcher.watch(path, RecursiveMode::Recursive); } - log::debug!("Watching paths: {:?}", paths); + log::debug!("Watching paths: {:?}", watched_paths); } fn consume_paths_to_watch( watcher: &mut RecommendedWatcher, receiver: &mut UnboundedReceiver>, + exclude_set: &PathOrPatternSet, ) { loop { match receiver.try_recv() { Ok(paths) => { - add_paths_to_watcher(watcher, &paths); + add_paths_to_watcher(watcher, &paths, exclude_set); } Err(e) => match e { mpsc::error::TryRecvError::Empty => { -- cgit v1.2.3