diff --git a/.changes/fix-copy-new-resource-files.md b/.changes/fix-copy-new-resource-files.md new file mode 100644 index 000000000000..757b5aeec942 --- /dev/null +++ b/.changes/fix-copy-new-resource-files.md @@ -0,0 +1,6 @@ +--- +'tauri-build': 'patch:bug' +'tauri-utils': 'patch:enhance' +--- + +Emit a `cargo:rerun-if-changed` for each resource directory (and glob base directory), so that adding or removing a file inside a resource directory re-runs the build script and copies the new files. Previously only the individual files present at build time were watched, so newly added files were silently ignored until an unrelated rebuild. diff --git a/crates/tauri-build/src/lib.rs b/crates/tauri-build/src/lib.rs index db62722ef275..e8dd24d41c79 100644 --- a/crates/tauri-build/src/lib.rs +++ b/crates/tauri-build/src/lib.rs @@ -87,11 +87,10 @@ fn copy_binaries( /// Copies resources to a path. fn copy_resources(resources: ResourcePaths<'_>, path: &Path) -> Result<()> { let path = path.canonicalize()?; - for resource in resources.iter() { + let mut resources = resources.iter(); + for resource in resources.by_ref() { let resource = resource?; - println!("cargo:rerun-if-changed={}", resource.path().display()); - // avoid copying the resource if target is the same as source let src = resource.path().canonicalize()?; let target = path.join(resource.target()); @@ -99,6 +98,11 @@ fn copy_resources(resources: ResourcePaths<'_>, path: &Path) -> Result<()> { copy_file(src, target)?; } } + + for path in resources.rerun_if_changed() { + println!("cargo:rerun-if-changed={}", path.display()); + } + Ok(()) } diff --git a/crates/tauri-utils/src/resources.rs b/crates/tauri-utils/src/resources.rs index 6c30b89de486..6b2c410cddb8 100644 --- a/crates/tauri-utils/src/resources.rs +++ b/crates/tauri-utils/src/resources.rs @@ -99,6 +99,7 @@ impl<'a> ResourcePaths<'a> { allow_walk, current_dest: None, current_iter: None, + rerun_if_changed: Vec::new(), }, } } @@ -111,6 +112,7 @@ impl<'a> ResourcePaths<'a> { allow_walk, current_dest: None, current_iter: None, + rerun_if_changed: Vec::new(), }, } } @@ -136,6 +138,10 @@ pub struct ResourcePathsIter<'a> { /// The iter for the current pattern. The cycle goes like this: /// [`ResourcePaths::next`] -> [`Self::next`] -> [`Self::pattern_iter::next`] -> [`Self::current_iter::next`] current_iter: Option, + /// Paths that were walked or globbed while iterating. Build scripts + /// should emit a `rerun-if-changed` for each so that adding or removing a + /// file inside a resource directory re-triggers the resource copy. + rerun_if_changed: Vec, } #[derive(Debug)] @@ -168,6 +174,17 @@ impl Iterator for ResourcePathsInnerIter { } impl ResourcePathsIter<'_> { + /// Paths that were walked or globbed while iterating. + /// + /// A build script should emit a `cargo:rerun-if-changed` for each of these + /// after iterating, so that adding or removing a file inside a resource + /// directory re-runs the script and copies the new files. Only meaningful + /// once iteration has produced the entries (i.e. after the iterator is + /// exhausted). + pub fn rerun_if_changed(&self) -> &[PathBuf] { + &self.rerun_if_changed + } + fn next_current_iter(&mut self) -> Option> { let current_iter = self.current_iter.as_mut().unwrap(); let entry = current_iter.next()?; @@ -247,6 +264,20 @@ impl ResourcePathsIter<'_> { }; if pattern.contains('*') { + // Watch the fixed directory prefix of the glob (everything before the + // first wildcard component) so new files matching the glob are noticed. + let mut base = PathBuf::new(); + for component in Path::new(pattern).components() { + if component.as_os_str().to_string_lossy().contains('*') { + break; + } + base.push(component); + } + if base.as_os_str().is_empty() { + base.push("."); + } + self.rerun_if_changed.push(base); + self.current_iter = match glob::glob(pattern) { Ok(glob) => Some(ResourcePathsInnerIter::Glob { iter: glob }), Err(error) => return Some(Err(error.into())), @@ -260,6 +291,7 @@ impl ResourcePathsIter<'_> { } } else { let path = normalize(Path::new(pattern)); + self.rerun_if_changed.push(path.clone()); if path.is_dir() { if !self.allow_walk { return Some(Err(crate::Error::NotAllowedToWalkDir(path))); @@ -453,6 +485,37 @@ mod tests { } } + #[test] + #[serial_test::serial(resources)] + fn resource_paths_iter_rerun_if_changed() { + setup_test_dirs(); + + let dir = std::env::current_dir().unwrap().join("src-tauri"); + let _ = std::env::set_current_dir(dir); + + let patterns = [ + "../src/script.js".into(), + "../src/assets".into(), + "../src/textures/**/*".into(), + "*.toml".into(), + ]; + let mut resources = ResourcePaths::new(&patterns, true).iter(); + + for resource in resources.by_ref() { + resource.unwrap(); + } + + assert_eq!( + resources.rerun_if_changed(), + &[ + normalize(Path::new("../src/script.js")), + normalize(Path::new("../src/assets")), + normalize(Path::new("../src/textures")), + PathBuf::from("."), + ] + ); + } + #[test] #[serial_test::serial(resources)] fn resource_paths_iter_slice_no_walk() {