Skip to content

Commit d7d03c7

Browse files
authored
fix(cli): dev watcher infinite loop on mobile (#9017)
1 parent e4463f0 commit d7d03c7

File tree

7 files changed

+69
-66
lines changed

7 files changed

+69
-66
lines changed

.changes/mobile-watcher.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/cli": patch:bug
3+
"tauri-cli": patch:bug
4+
---
5+
6+
Fixes dev watcher on mobile dev.

tooling/cli/src/interface/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub trait DevProcess {
2121
fn try_wait(&self) -> std::io::Result<Option<ExitStatus>>;
2222
fn wait(&self) -> std::io::Result<ExitStatus>;
2323
fn manually_killed_process(&self) -> bool;
24-
fn is_building_app(&self) -> bool;
2524
}
2625

2726
pub trait AppSettings {

tooling/cli/src/interface/rust.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Interface for Rust {
121121
watcher
122122
.watcher()
123123
.watch(&tauri_dir().join("Cargo.toml"), RecursiveMode::Recursive)?;
124-
let manifest = rewrite_manifest(config)?;
124+
let (manifest, _modified) = rewrite_manifest(config)?;
125125
let now = Instant::now();
126126
let timeout = Duration::from_secs(2);
127127
loop {
@@ -535,38 +535,34 @@ impl Rust {
535535

536536
if !ignore_matcher.is_ignore(&event_path, event_path.is_dir()) {
537537
if is_configuration_file(self.app_settings.target, &event_path) {
538-
match reload_config(config.as_ref()) {
539-
Ok(config) => {
540-
info!("Tauri configuration changed. Rewriting manifest...");
541-
*self.app_settings.manifest.lock().unwrap() =
542-
rewrite_manifest(config.lock().unwrap().as_ref().unwrap())?
543-
}
544-
Err(err) => {
545-
let p = process.lock().unwrap();
546-
if p.is_building_app() {
547-
p.kill().with_context(|| "failed to kill app process")?;
548-
}
549-
error!("{}", err);
538+
if let Ok(config) = reload_config(config.as_ref()) {
539+
let (manifest, modified) =
540+
rewrite_manifest(config.lock().unwrap().as_ref().unwrap())?;
541+
if modified {
542+
*self.app_settings.manifest.lock().unwrap() = manifest;
543+
// no need to run the watcher logic, the manifest was modified
544+
// and it will trigger the watcher again
545+
continue;
550546
}
551547
}
552-
} else {
553-
info!(
554-
"File {} changed. Rebuilding application...",
555-
display_path(event_path.strip_prefix(app_path).unwrap_or(&event_path))
556-
);
557-
// When tauri.conf.json is changed, rewrite_manifest will be called
558-
// which will trigger the watcher again
559-
// So the app should only be started when a file other than tauri.conf.json is changed
560-
let mut p = process.lock().unwrap();
561-
p.kill().with_context(|| "failed to kill app process")?;
562-
// wait for the process to exit
563-
loop {
564-
if let Ok(Some(_)) = p.try_wait() {
565-
break;
566-
}
548+
}
549+
550+
log::info!(
551+
"File {} changed. Rebuilding application...",
552+
display_path(event_path.strip_prefix(app_path).unwrap_or(&event_path))
553+
);
554+
555+
let mut p = process.lock().unwrap();
556+
p.kill().with_context(|| "failed to kill app process")?;
557+
558+
// wait for the process to exit
559+
// note that on mobile, kill() already waits for the process to exit (duct implementation)
560+
loop {
561+
if !matches!(p.try_wait(), Ok(None)) {
562+
break;
567563
}
568-
*p = run(self)?;
569564
}
565+
*p = run(self)?;
570566
}
571567
}
572568
}

tooling/cli/src/interface/rust/desktop.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ impl DevProcess for DevChild {
6262
fn manually_killed_process(&self) -> bool {
6363
self.manually_killed_app.load(Ordering::Relaxed)
6464
}
65-
66-
fn is_building_app(&self) -> bool {
67-
self.app_child.lock().unwrap().is_none()
68-
}
6965
}
7066

7167
pub fn run_dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(

tooling/cli/src/interface/rust/manifest.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn get_enabled_features(list: &HashMap<String, Vec<String>>, feature: &str) -> V
8484
f
8585
}
8686

87-
pub fn read_manifest(manifest_path: &Path) -> crate::Result<Document> {
87+
pub fn read_manifest(manifest_path: &Path) -> crate::Result<(Document, String)> {
8888
let mut manifest_str = String::new();
8989

9090
let mut manifest_file = File::open(manifest_path)
@@ -95,7 +95,7 @@ pub fn read_manifest(manifest_path: &Path) -> crate::Result<Document> {
9595
.parse::<Document>()
9696
.with_context(|| "failed to parse Cargo.toml")?;
9797

98-
Ok(manifest)
98+
Ok((manifest, manifest_str))
9999
}
100100

101101
pub fn toml_array(features: &HashSet<String>) -> Array {
@@ -265,9 +265,9 @@ fn inject_features(
265265
Ok(persist)
266266
}
267267

268-
pub fn rewrite_manifest(config: &Config) -> crate::Result<Manifest> {
268+
pub fn rewrite_manifest(config: &Config) -> crate::Result<(Manifest, bool)> {
269269
let manifest_path = tauri_dir().join("Cargo.toml");
270-
let mut manifest = read_manifest(&manifest_path)?;
270+
let (mut manifest, original_manifest_str) = read_manifest(&manifest_path)?;
271271

272272
let mut dependencies = Vec::new();
273273

@@ -303,31 +303,36 @@ pub fn rewrite_manifest(config: &Config) -> crate::Result<Manifest> {
303303
.unwrap()
304304
.features;
305305

306-
if persist {
306+
let new_manifest_str = manifest
307+
.to_string()
308+
// apply some formatting fixes
309+
.replace(r#"" ,features =["#, r#"", features = ["#)
310+
.replace(r#"" , features"#, r#"", features"#)
311+
.replace("]}", "] }")
312+
.replace("={", "= {")
313+
.replace("=[", "= [")
314+
.replace(r#"",""#, r#"", ""#);
315+
316+
if persist && original_manifest_str != new_manifest_str {
307317
let mut manifest_file =
308318
File::create(&manifest_path).with_context(|| "failed to open Cargo.toml for rewrite")?;
309-
manifest_file.write_all(
310-
manifest
311-
.to_string()
312-
// apply some formatting fixes
313-
.replace(r#"" ,features =["#, r#"", features = ["#)
314-
.replace(r#"" , features"#, r#"", features"#)
315-
.replace("]}", "] }")
316-
.replace("={", "= {")
317-
.replace("=[", "= [")
318-
.replace(r#"",""#, r#"", ""#)
319-
.as_bytes(),
320-
)?;
319+
manifest_file.write_all(new_manifest_str.as_bytes())?;
321320
manifest_file.flush()?;
322-
Ok(Manifest {
323-
inner: manifest,
324-
tauri_features,
325-
})
321+
Ok((
322+
Manifest {
323+
inner: manifest,
324+
tauri_features,
325+
},
326+
true,
327+
))
326328
} else {
327-
Ok(Manifest {
328-
inner: manifest,
329-
tauri_features,
330-
})
329+
Ok((
330+
Manifest {
331+
inner: manifest,
332+
tauri_features,
333+
},
334+
false,
335+
))
331336
}
332337
}
333338

tooling/cli/src/migrate/manifest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const CRATE_TYPES: [&str; 3] = ["lib", "staticlib", "cdylib"];
1515

1616
pub fn migrate(tauri_dir: &Path) -> Result<()> {
1717
let manifest_path = tauri_dir.join("Cargo.toml");
18-
let mut manifest = read_manifest(&manifest_path)?;
18+
let (mut manifest, _) = read_manifest(&manifest_path)?;
1919
migrate_manifest(&mut manifest)?;
2020

2121
let mut manifest_file =

tooling/cli/src/mobile/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ impl DevChild {
6969

7070
impl DevProcess for DevChild {
7171
fn kill(&self) -> std::io::Result<()> {
72-
self.child.kill()?;
7372
self.manually_killed_process.store(true, Ordering::Relaxed);
74-
Ok(())
73+
match self.child.kill() {
74+
Ok(_) => Ok(()),
75+
Err(e) => {
76+
self.manually_killed_process.store(false, Ordering::Relaxed);
77+
Err(e)
78+
}
79+
}
7580
}
7681

7782
fn try_wait(&self) -> std::io::Result<Option<ExitStatus>> {
@@ -85,10 +90,6 @@ impl DevProcess for DevChild {
8590
fn manually_killed_process(&self) -> bool {
8691
self.manually_killed_process.load(Ordering::Relaxed)
8792
}
88-
89-
fn is_building_app(&self) -> bool {
90-
false
91-
}
9293
}
9394

9495
#[derive(PartialEq, Eq, Copy, Clone)]

0 commit comments

Comments
 (0)