Skip to content

Commit 6507d0b

Browse files
authored
fix(cli): replace dev setup panics with errors and kill beforeDevCommand once (#16124)
1 parent 7bb0a54 commit 6507d0b

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

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+
`tauri dev` now reports an error instead of panicking when the `beforeDevCommand` cannot be spawned or the `devUrl` host cannot be resolved, and no longer risks stopping the `beforeDevCommand` process tree twice when Ctrl+C and the app exit race.

‎crates/tauri-cli/src/dev.rs‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,13 @@ pub fn setup(
203203
command.stdout(os_pipe::dup_stdout().unwrap());
204204
command.stderr(os_pipe::dup_stderr().unwrap());
205205

206-
let child = SharedChild::spawn(&mut command)
207-
.unwrap_or_else(|_| panic!("failed to run `{before_dev}`"));
206+
let child = SharedChild::spawn(&mut command).map_err(|error| Error::CommandFailed {
207+
command: format!(
208+
"`{before_dev}` with `{}`",
209+
if cfg!(windows) { "cmd /S /C" } else { "sh -c" }
210+
),
211+
error,
212+
})?;
208213

209214
let child = BEFORE_DEV.get_or_init(move || child);
210215
std::thread::spawn(move || {
@@ -229,9 +234,6 @@ pub fn setup(
229234
options.runner = config.build.runner.clone();
230235
}
231236

232-
let mut cargo_features = config.build.features.clone().unwrap_or_default();
233-
cargo_features.extend(options.features.clone());
234-
235237
let mut dev_url = config.build.dev_url.clone();
236238
let frontend_dist = config.build.frontend_dist.clone();
237239
if !options.no_dev_server && dev_url.is_none() {
@@ -265,16 +267,20 @@ pub fn setup(
265267

266268
if !options.no_dev_server_wait {
267269
if let Some(url) = dev_url {
268-
let host = url.host().expect("No host name in the URL");
270+
let host = url
271+
.host()
272+
.with_context(|| format!("no host name in the dev URL `{url}`"))?;
269273
let port = url
270274
.port_or_known_default()
271-
.expect("No port number in the URL");
275+
.with_context(|| format!("no port number in the dev URL `{url}`"))?;
272276
let addrs;
273277
let addr;
274278
let addrs = match host {
275279
url::Host::Domain(domain) => {
276280
use std::net::ToSocketAddrs;
277-
addrs = (domain, port).to_socket_addrs().unwrap();
281+
addrs = (domain, port)
282+
.to_socket_addrs()
283+
.with_context(|| format!("failed to resolve the dev URL host `{domain}`"))?;
278284
addrs.as_slice()
279285
}
280286
url::Host::Ipv4(ip) => {
@@ -334,10 +340,10 @@ pub fn on_app_exit(code: Option<i32>, reason: ExitReason, exit_on_panic: bool, n
334340

335341
pub fn kill_before_dev_process() {
336342
if let Some(child) = BEFORE_DEV.get() {
337-
if KILL_BEFORE_DEV_FLAG.load(Ordering::SeqCst) {
343+
// swap so only the first caller (e.g. the Ctrl+C handler or `on_app_exit`) kills the process
344+
if KILL_BEFORE_DEV_FLAG.swap(true, Ordering::SeqCst) {
338345
return;
339346
}
340-
KILL_BEFORE_DEV_FLAG.store(true, Ordering::SeqCst);
341347
#[cfg(windows)]
342348
{
343349
let powershell_path = std::env::var("SYSTEMROOT").map_or_else(

0 commit comments

Comments
 (0)