Skip to content

Commit

Permalink
fix(cli): do not panic on Ctrl+C on ios dev (#7240)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jun 17, 2023
1 parent 1542ad1 commit 655c714
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-ios-cli-panic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:bug
"@tauri-apps/cli": patch:bug
---

Fixes panic when exiting the `ios dev` command with Ctrl + C.
16 changes: 10 additions & 6 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use shared_child::SharedChild;
use std::{
env::set_current_dir,
net::{IpAddr, Ipv4Addr},
process::{exit, Command, ExitStatus, Stdio},
process::{exit, Command, Stdio},
sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
Expand Down Expand Up @@ -393,15 +393,19 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {

pub fn wait_dev_process<
C: DevProcess + Send + 'static,
F: Fn(ExitStatus, ExitReason) + Send + Sync + 'static,
F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static,
>(
child: C,
on_exit: F,
) {
std::thread::spawn(move || {
let status = child.wait().expect("failed to wait on app");
let code = child
.wait()
.ok()
.and_then(|status| status.code())
.or(Some(1));
on_exit(
status,
code,
if child.manually_killed_process() {
ExitReason::TriggeredKill
} else {
Expand All @@ -411,15 +415,15 @@ pub fn wait_dev_process<
});
}

pub fn on_app_exit(status: ExitStatus, reason: ExitReason, exit_on_panic: bool, no_watch: bool) {
pub fn on_app_exit(code: Option<i32>, reason: ExitReason, exit_on_panic: bool, no_watch: bool) {
if no_watch
|| (!matches!(reason, ExitReason::TriggeredKill)
&& (exit_on_panic || matches!(reason, ExitReason::NormalExit)))
{
kill_before_dev_process();
#[cfg(not(debug_assertions))]
let _ = check_for_updates();
exit(status.code().unwrap_or(0));
exit(code.unwrap_or(0));
}
}

Expand Down
2 changes: 1 addition & 1 deletion tooling/cli/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub trait Interface: Sized {
fn app_settings(&self) -> &Self::AppSettings;
fn env(&self) -> HashMap<&str, String>;
fn build(&mut self, options: Options) -> crate::Result<()>;
fn dev<F: Fn(ExitStatus, ExitReason) + Send + Sync + 'static>(
fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self,
options: Options,
on_exit: F,
Expand Down
6 changes: 3 additions & 3 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
fs::{File, FileType},
io::{BufRead, Read, Write},
path::{Path, PathBuf},
process::{Command, ExitStatus},
process::Command,
str::FromStr,
sync::{mpsc::sync_channel, Arc, Mutex},
time::{Duration, Instant},
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Interface for Rust {
Ok(())
}

fn dev<F: Fn(ExitStatus, ExitReason) + Send + Sync + 'static>(
fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self,
mut options: Options,
on_exit: F,
Expand Down Expand Up @@ -426,7 +426,7 @@ impl Rust {
shared_options(mobile, args, features, &self.app_settings);
}

fn run_dev<F: Fn(ExitStatus, ExitReason) + Send + Sync + 'static>(
fn run_dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self,
options: Options,
run_args: Vec<String>,
Expand Down
12 changes: 6 additions & 6 deletions tooling/cli/src/interface/rust/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl DevProcess for DevChild {
}
}

pub fn run_dev<F: Fn(ExitStatus, ExitReason) + Send + Sync + 'static>(
pub fn run_dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
options: Options,
run_args: Vec<String>,
available_targets: &mut Option<Vec<Target>>,
Expand All @@ -93,7 +93,7 @@ pub fn run_dev<F: Fn(ExitStatus, ExitReason) + Send + Sync + 'static>(
available_targets,
config_features,
move |status, reason| {
if status.success() {
if status == Some(0) {
let bin_path =
rename_app(target_os, &bin_path, product_name.as_deref()).expect("failed to rename app");
let mut app = Command::new(bin_path);
Expand Down Expand Up @@ -192,7 +192,7 @@ pub fn build(
Ok(())
}

fn build_dev_app<F: FnOnce(ExitStatus, ExitReason) + Send + 'static>(
fn build_dev_app<F: FnOnce(Option<i32>, ExitReason) + Send + 'static>(
options: Options,
available_targets: &mut Option<Vec<Target>>,
config_features: Vec<String>,
Expand Down Expand Up @@ -259,10 +259,10 @@ fn build_dev_app<F: FnOnce(ExitStatus, ExitReason) + Send + 'static>(

let build_child_ = build_child.clone();
std::thread::spawn(move || {
let status = build_child_.wait().expect("failed to wait on build");
let status = build_child_.wait().expect("failed to build app");

if status.success() {
on_exit(status, ExitReason::NormalExit);
on_exit(status.code(), ExitReason::NormalExit);
} else {
let is_cargo_compile_error = stderr_lines
.lock()
Expand All @@ -273,7 +273,7 @@ fn build_dev_app<F: FnOnce(ExitStatus, ExitReason) + Send + 'static>(
stderr_lines.lock().unwrap().clear();

on_exit(
status,
status.code(),
if status.code() == Some(101) && is_cargo_compile_error {
ExitReason::CompilationFailed
} else {
Expand Down

0 comments on commit 655c714

Please sign in to comment.