diff --git a/.changes/fix-taurignore-root.md b/.changes/fix-taurignore-root.md new file mode 100644 index 000000000000..52b0fb77c77c --- /dev/null +++ b/.changes/fix-taurignore-root.md @@ -0,0 +1,6 @@ +--- +"tauri-cli": "patch:bug" +"@tauri-apps/cli": "patch:bug" +--- + +Fix `tauri dev` not watching the app at all when its `.taurignore` contains a pattern like `/*` that matches every entry at the top level, even if other entries are re-included with `!` negations. Watch folders are now always watched; ignore rules apply to their entries and to change events. diff --git a/crates/tauri-cli/src/interface/rust.rs b/crates/tauri-cli/src/interface/rust.rs index b7def14b99f0..b13ed39eba3e 100644 --- a/crates/tauri-cli/src/interface/rust.rs +++ b/crates/tauri-cli/src/interface/rust.rs @@ -402,6 +402,26 @@ fn lookup(dir: &Path, mut f: F) { } } +/// The paths the dev watcher watches: the entries of every watch folder that the ignore files +/// don't exclude. Every folder is walked, even when a pattern such as `/*` matches the folder +/// itself; ignore rules apply to its entries here and to every change event. +fn watch_targets(watch_folders: &[PathBuf]) -> Vec<(PathBuf, RecursiveMode)> { + let mut targets = Vec::new(); + for path in watch_folders { + lookup(path, |file_type, p| { + if p != *path { + let mode = if file_type.is_dir() { + RecursiveMode::Recursive + } else { + RecursiveMode::NonRecursive + }; + targets.push((p, mode)); + } + }); + } + targets +} + fn dev_options( mobile: bool, args: &mut Vec, @@ -533,23 +553,12 @@ impl Rust { } }) .unwrap(); - for path in watch_folders { - if !ignore_matcher.is_ignore(&path, true) { - log::info!("Watching {} for changes...", display_path(&path)); - lookup(&path, |file_type, p| { - if p != path { - log::debug!("Watching {} for changes...", display_path(&p)); - let _ = watcher.watch( - &p, - if file_type.is_dir() { - RecursiveMode::Recursive - } else { - RecursiveMode::NonRecursive - }, - ); - } - }); - } + for path in &watch_folders { + log::info!("Watching {} for changes...", display_path(path)); + } + for (path, mode) in watch_targets(&watch_folders) { + log::debug!("Watching {} for changes...", display_path(&path)); + let _ = watcher.watch(&path, mode); } while let Ok(events) = rx.recv() { @@ -1792,6 +1801,49 @@ mod tests { ); } + fn taurignore_root_fixture() -> tempfile::TempDir { + let temp_dir = tempfile::tempdir().unwrap(); + let dir = temp_dir.path(); + fs::create_dir_all(dir.join("src")).unwrap(); + fs::create_dir_all(dir.join("assets")).unwrap(); + fs::write(dir.join("src/main.rs"), "").unwrap(); + fs::write(dir.join("Cargo.toml"), "").unwrap(); + fs::write(dir.join("README.md"), "").unwrap(); + fs::write(dir.join(".taurignore"), "/*\n!/*.toml\n!/*.rs\n!/src\n").unwrap(); + temp_dir + } + + #[test] + fn watch_targets_watch_a_folder_whose_taurignore_matches_it() { + let temp_dir = taurignore_root_fixture(); + let dir = temp_dir.path(); + + let mut watched: Vec = watch_targets(&[dir.to_path_buf()]) + .into_iter() + .map(|(path, _)| path.strip_prefix(dir).unwrap().to_path_buf()) + .collect(); + watched.sort(); + + assert_eq!( + watched, + vec![PathBuf::from("Cargo.toml"), PathBuf::from("src")] + ); + } + + #[test] + fn ignore_matcher_filters_events_under_a_root_wide_pattern() { + let temp_dir = taurignore_root_fixture(); + let dir = temp_dir.path(); + + let ignore_matcher = build_ignore_matcher(dir); + + assert!(!ignore_matcher.is_ignore(&dir.join("src"), true)); + assert!(!ignore_matcher.is_ignore(&dir.join("src/main.rs"), false)); + assert!(!ignore_matcher.is_ignore(&dir.join("Cargo.toml"), false)); + assert!(ignore_matcher.is_ignore(&dir.join("README.md"), false)); + assert!(ignore_matcher.is_ignore(&dir.join("assets"), true)); + } + #[test] fn parse_cargo_option() { let args = [