Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/fix-copy-new-resource-files.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 7 additions & 3 deletions crates/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,22 @@ 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());
if src != target {
copy_file(src, target)?;
}
}

for path in resources.rerun_if_changed() {
println!("cargo:rerun-if-changed={}", path.display());
}

Ok(())
}

Expand Down
63 changes: 63 additions & 0 deletions crates/tauri-utils/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl<'a> ResourcePaths<'a> {
allow_walk,
current_dest: None,
current_iter: None,
rerun_if_changed: Vec::new(),
},
}
}
Expand All @@ -111,6 +112,7 @@ impl<'a> ResourcePaths<'a> {
allow_walk,
current_dest: None,
current_iter: None,
rerun_if_changed: Vec::new(),
},
}
}
Expand All @@ -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<ResourcePathsInnerIter>,
/// 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<PathBuf>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -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<crate::Result<Resource>> {
let current_iter = self.current_iter.as_mut().unwrap();
let entry = current_iter.next()?;
Expand Down Expand Up @@ -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())),
Expand All @@ -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)));
Expand Down Expand Up @@ -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() {
Expand Down
Loading