Skip to content

Commit 574a4d4

Browse files
fix(cli): slow dev startup (#13426)
* fix(cli): slow dev startup * Use `recv_timeout` instead * Update crates/tauri-cli/src/interface/rust.rs Co-authored-by: Fabian-Lars <[email protected]> --------- Co-authored-by: Fabian-Lars <[email protected]>
1 parent 1777406 commit 574a4d4

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

.changes/cli-dev-slow-start.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
tauri-cli: "patch:bug"
3+
---
4+
5+
Fix `dev`, `build` and `bundle` commands always take 2 seconds to start

crates/tauri-cli/src/interface/rust.rs

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
use std::{
66
collections::HashMap,
77
ffi::OsStr,
8-
fs::{File, FileType},
9-
io::{BufRead, Read, Write},
8+
fs::FileType,
9+
io::{BufRead, Write},
1010
path::{Path, PathBuf},
1111
process::Command,
1212
str::FromStr,
1313
sync::{mpsc::sync_channel, Arc, Mutex},
14-
time::{Duration, Instant},
14+
time::Duration,
1515
};
1616

1717
use anyhow::Context;
@@ -126,22 +126,16 @@ impl Interface for Rust {
126126
let manifest = {
127127
let (tx, rx) = sync_channel(1);
128128
let mut watcher = new_debouncer(Duration::from_secs(1), None, move |r| {
129-
if let Ok(events) = r {
130-
let _ = tx.send(events);
129+
if let Ok(_events) = r {
130+
let _ = tx.send(());
131131
}
132132
})
133133
.unwrap();
134-
watcher.watch(tauri_dir().join("Cargo.toml"), RecursiveMode::Recursive)?;
135-
let (manifest, _modified) = rewrite_manifest(config)?;
136-
let now = Instant::now();
137-
let timeout = Duration::from_secs(2);
138-
loop {
139-
if now.elapsed() >= timeout {
140-
break;
141-
}
142-
if rx.try_recv().is_ok() {
143-
break;
144-
}
134+
watcher.watch(tauri_dir().join("Cargo.toml"), RecursiveMode::NonRecursive)?;
135+
let (manifest, modified) = rewrite_manifest(config)?;
136+
if modified {
137+
// Wait for the modified event so we don't trigger a re-build later on
138+
let _ = rx.recv_timeout(Duration::from_secs(2));
145139
}
146140
manifest
147141
};
@@ -409,12 +403,10 @@ fn dev_options(
409403

410404
// Copied from https://github.com/rust-lang/cargo/blob/69255bb10de7f74511b5cef900a9d102247b6029/src/cargo/core/workspace.rs#L665
411405
fn expand_member_path(path: &Path) -> crate::Result<Vec<PathBuf>> {
412-
let Some(path) = path.to_str() else {
413-
return Err(anyhow::anyhow!("path is not UTF-8 compatible"));
414-
};
415-
let res = glob(path).with_context(|| format!("could not parse pattern `{}`", &path))?;
406+
let path = path.to_str().context("path is not UTF-8 compatible")?;
407+
let res = glob(path).with_context(|| format!("could not parse pattern `{path}`"))?;
416408
let res = res
417-
.map(|p| p.with_context(|| format!("unable to match path to pattern `{}`", &path)))
409+
.map(|p| p.with_context(|| format!("unable to match path to pattern `{path}`")))
418410
.collect::<Result<Vec<_>, _>>()?;
419411
Ok(res)
420412
}
@@ -614,8 +606,7 @@ impl<T> MaybeWorkspace<T> {
614606
))
615607
}
616608
MaybeWorkspace::Workspace(TomlWorkspaceField { workspace: false }) => Err(anyhow::anyhow!(
617-
"`workspace=false` is unsupported for `package.{}`",
618-
label,
609+
"`workspace=false` is unsupported for `package.{label}`"
619610
)),
620611
}
621612
}
@@ -694,12 +685,9 @@ impl CargoSettings {
694685
/// Try to load a set of CargoSettings from a "Cargo.toml" file in the specified directory.
695686
fn load(dir: &Path) -> crate::Result<Self> {
696687
let toml_path = dir.join("Cargo.toml");
697-
let mut toml_str = String::new();
698-
let mut toml_file = File::open(toml_path).with_context(|| "failed to open Cargo.toml")?;
699-
toml_file
700-
.read_to_string(&mut toml_str)
701-
.with_context(|| "failed to read Cargo.toml")?;
702-
toml::from_str(&toml_str).with_context(|| "failed to parse Cargo.toml")
688+
let toml_str = std::fs::read_to_string(&toml_path)
689+
.with_context(|| format!("Failed to read {}", toml_path.display()))?;
690+
toml::from_str(&toml_str).with_context(|| format!("Failed to parse {}", toml_path.display()))
703691
}
704692
}
705693

@@ -976,10 +964,10 @@ impl AppSettings for RustAppSettings {
976964
.unwrap()
977965
.inner
978966
.as_table()
979-
.get("package")
980-
.and_then(|p| p.as_table())
981-
.and_then(|p| p.get("name"))
982-
.and_then(|n| n.as_str())
967+
.get("package")?
968+
.as_table()?
969+
.get("name")?
970+
.as_str()
983971
.map(|n| n.to_string())
984972
}
985973

@@ -990,19 +978,18 @@ impl AppSettings for RustAppSettings {
990978
.unwrap()
991979
.inner
992980
.as_table()
993-
.get("lib")
994-
.and_then(|p| p.as_table())
995-
.and_then(|p| p.get("name"))
996-
.and_then(|n| n.as_str())
981+
.get("lib")?
982+
.as_table()?
983+
.get("name")?
984+
.as_str()
997985
.map(|n| n.to_string())
998986
}
999987
}
1000988

1001989
impl RustAppSettings {
1002990
pub fn new(config: &Config, manifest: Manifest, target: Option<String>) -> crate::Result<Self> {
1003991
let tauri_dir = tauri_dir();
1004-
let cargo_settings =
1005-
CargoSettings::load(tauri_dir).with_context(|| "failed to load cargo settings")?;
992+
let cargo_settings = CargoSettings::load(tauri_dir).context("failed to load cargo settings")?;
1006993
let cargo_package_settings = match &cargo_settings.package {
1007994
Some(package_info) => package_info.clone(),
1008995
None => {
@@ -1013,7 +1000,7 @@ impl RustAppSettings {
10131000
};
10141001

10151002
let ws_package_settings = CargoSettings::load(&get_workspace_dir()?)
1016-
.with_context(|| "failed to load cargo settings from workspace root")?
1003+
.context("failed to load cargo settings from workspace root")?
10171004
.workspace
10181005
.and_then(|v| v.package);
10191006

@@ -1194,7 +1181,7 @@ fn get_cargo_option<'a>(args: &'a [String], option: &'a str) -> Option<&'a str>
11941181
pub fn get_workspace_dir() -> crate::Result<PathBuf> {
11951182
Ok(
11961183
get_cargo_metadata()
1197-
.with_context(|| "failed to get cargo metadata")?
1184+
.context("failed to get cargo metadata")?
11981185
.workspace_root,
11991186
)
12001187
}

crates/tauri-cli/src/interface/rust/manifest.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use toml_edit::{Array, DocumentMut, InlineTable, Item, TableLike, Value};
1414
use std::{
1515
collections::{HashMap, HashSet},
1616
fs::File,
17-
io::{Read, Write},
17+
io::Write,
1818
path::Path,
1919
};
2020

@@ -83,15 +83,12 @@ fn get_enabled_features(list: &HashMap<String, Vec<String>>, feature: &str) -> V
8383
}
8484

8585
pub fn read_manifest(manifest_path: &Path) -> crate::Result<(DocumentMut, String)> {
86-
let mut manifest_str = String::new();
87-
88-
let mut manifest_file = File::open(manifest_path)
89-
.with_context(|| format!("failed to open `{manifest_path:?}` file"))?;
90-
manifest_file.read_to_string(&mut manifest_str)?;
86+
let manifest_str = std::fs::read_to_string(manifest_path)
87+
.with_context(|| format!("Failed to read `{manifest_path:?}` file"))?;
9188

9289
let manifest: DocumentMut = manifest_str
9390
.parse::<DocumentMut>()
94-
.with_context(|| "failed to parse Cargo.toml")?;
91+
.with_context(|| "Failed to parse Cargo.toml")?;
9592

9693
Ok((manifest, manifest_str))
9794
}

0 commit comments

Comments
 (0)