Skip to content

Commit 67d7877

Browse files
authored
fix(cli): Watch workspace members if tauri dir is workspace root (#8520)
* fix(cli): Watch workspace members if tauri dir is ws root See title. This PR also includes a fix/workaround for paths with funny characters that may not make the glob expansion panic. Fixes #8509 * extract into function * cleanup
1 parent 8991129 commit 67d7877

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

.changes/cli-watch-ws-members.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
The cli now also watches cargo workspace members if the tauri folder is the workspace root.

tooling/cli/src/interface/rust.rs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,40 @@ fn expand_member_path(path: &Path) -> crate::Result<Vec<PathBuf>> {
347347
Ok(res)
348348
}
349349

350+
fn get_watch_folders() -> crate::Result<Vec<PathBuf>> {
351+
let tauri_path = tauri_dir();
352+
let workspace_path = get_workspace_dir()?;
353+
354+
// We always want to watch the main tauri folder.
355+
let mut watch_folders = vec![tauri_path.to_path_buf()];
356+
357+
// We also try to watch workspace members, no matter if the tauri cargo project is the workspace root or a workspace member
358+
let cargo_settings = CargoSettings::load(&workspace_path)?;
359+
if let Some(members) = cargo_settings.workspace.and_then(|w| w.members) {
360+
for p in members {
361+
let p = workspace_path.join(p);
362+
match expand_member_path(&p) {
363+
// Sometimes expand_member_path returns an empty vec, for example if the path contains `[]` as in `C:/[abc]/project/`.
364+
// Cargo won't complain unless theres a workspace.members config with glob patterns so we should support it too.
365+
Ok(expanded_paths) => {
366+
if expanded_paths.is_empty() {
367+
watch_folders.push(p);
368+
} else {
369+
watch_folders.extend(expanded_paths);
370+
}
371+
}
372+
Err(err) => {
373+
// If this fails cargo itself should fail too. But we still try to keep going with the unexpanded path.
374+
error!("Error watching {}: {}", p.display(), err.to_string());
375+
watch_folders.push(p);
376+
}
377+
};
378+
}
379+
}
380+
381+
Ok(watch_folders)
382+
}
383+
350384
impl Rust {
351385
fn run_dev<F: Fn(ExitStatus, ExitReason) + Send + Sync + 'static>(
352386
&mut self,
@@ -412,43 +446,11 @@ impl Rust {
412446
let process = Arc::new(Mutex::new(child));
413447
let (tx, rx) = sync_channel(1);
414448
let app_path = app_dir();
415-
let tauri_path = tauri_dir();
416-
let workspace_path = get_workspace_dir()?;
417-
418-
let watch_folders = if tauri_path == workspace_path {
419-
vec![tauri_path]
420-
} else {
421-
let cargo_settings = CargoSettings::load(&workspace_path)?;
422-
cargo_settings
423-
.workspace
424-
.as_ref()
425-
.map(|w| {
426-
w.members
427-
.clone()
428-
.unwrap_or_default()
429-
.into_iter()
430-
.map(|p| workspace_path.join(p))
431-
.collect()
432-
})
433-
.unwrap_or_else(|| vec![tauri_path])
434-
};
435449

436-
let watch_folders = watch_folders
437-
.into_iter()
438-
.flat_map(|p| {
439-
match expand_member_path(&p) {
440-
Ok(p) => p,
441-
Err(err) => {
442-
// If this fails cargo itself should fail too. But we still try to keep going with the unexpanded path.
443-
error!("Error watching {}: {}", p.display(), err.to_string());
444-
vec![p]
445-
}
446-
}
447-
})
448-
.collect::<Vec<_>>();
449-
let watch_folders = watch_folders.iter().map(Path::new).collect::<Vec<_>>();
450+
let watch_folders = get_watch_folders()?;
450451

451-
let common_ancestor = common_path::common_path_all(watch_folders.clone()).unwrap();
452+
let common_ancestor = common_path::common_path_all(watch_folders.iter().map(Path::new))
453+
.expect("watch_folders should not be empty");
452454
let ignore_matcher = build_ignore_matcher(&common_ancestor);
453455

454456
let mut watcher = new_debouncer(Duration::from_secs(1), move |r| {
@@ -458,9 +460,9 @@ impl Rust {
458460
})
459461
.unwrap();
460462
for path in watch_folders {
461-
if !ignore_matcher.is_ignore(path, true) {
462-
info!("Watching {} for changes...", display_path(path));
463-
lookup(path, |file_type, p| {
463+
if !ignore_matcher.is_ignore(&path, true) {
464+
info!("Watching {} for changes...", display_path(&path));
465+
lookup(&path, |file_type, p| {
464466
if p != path {
465467
debug!("Watching {} for changes...", display_path(&p));
466468
let _ = watcher.watcher().watch(

0 commit comments

Comments
 (0)