fix(cli): do not panic on Ctrl+C on ios dev (#7240)

This commit is contained in:
Lucas Fernandes Nogueira 2023-06-17 10:09:37 -07:00 committed by GitHub
parent 1542ad17d6
commit 655c714e41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 16 deletions

View File

@ -0,0 +1,6 @@
---
"tauri-cli": patch:bug
"@tauri-apps/cli": patch:bug
---
Fixes panic when exiting the `ios dev` command with Ctrl + C.

View File

@ -22,7 +22,7 @@ use shared_child::SharedChild;
use std::{ use std::{
env::set_current_dir, env::set_current_dir,
net::{IpAddr, Ipv4Addr}, net::{IpAddr, Ipv4Addr},
process::{exit, Command, ExitStatus, Stdio}, process::{exit, Command, Stdio},
sync::{ sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
Arc, Mutex, Arc, Mutex,
@ -393,15 +393,19 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
pub fn wait_dev_process< pub fn wait_dev_process<
C: DevProcess + Send + 'static, C: DevProcess + Send + 'static,
F: Fn(ExitStatus, ExitReason) + Send + Sync + 'static, F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static,
>( >(
child: C, child: C,
on_exit: F, on_exit: F,
) { ) {
std::thread::spawn(move || { 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( on_exit(
status, code,
if child.manually_killed_process() { if child.manually_killed_process() {
ExitReason::TriggeredKill ExitReason::TriggeredKill
} else { } else {
@ -411,7 +415,7 @@ 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 if no_watch
|| (!matches!(reason, ExitReason::TriggeredKill) || (!matches!(reason, ExitReason::TriggeredKill)
&& (exit_on_panic || matches!(reason, ExitReason::NormalExit))) && (exit_on_panic || matches!(reason, ExitReason::NormalExit)))
@ -419,7 +423,7 @@ pub fn on_app_exit(status: ExitStatus, reason: ExitReason, exit_on_panic: bool,
kill_before_dev_process(); kill_before_dev_process();
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
let _ = check_for_updates(); let _ = check_for_updates();
exit(status.code().unwrap_or(0)); exit(code.unwrap_or(0));
} }
} }

View File

@ -90,7 +90,7 @@ pub trait Interface: Sized {
fn app_settings(&self) -> &Self::AppSettings; fn app_settings(&self) -> &Self::AppSettings;
fn env(&self) -> HashMap<&str, String>; fn env(&self) -> HashMap<&str, String>;
fn build(&mut self, options: Options) -> crate::Result<()>; 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, &mut self,
options: Options, options: Options,
on_exit: F, on_exit: F,

View File

@ -8,7 +8,7 @@ use std::{
fs::{File, FileType}, fs::{File, FileType},
io::{BufRead, Read, Write}, io::{BufRead, Read, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
process::{Command, ExitStatus}, process::Command,
str::FromStr, str::FromStr,
sync::{mpsc::sync_channel, Arc, Mutex}, sync::{mpsc::sync_channel, Arc, Mutex},
time::{Duration, Instant}, time::{Duration, Instant},
@ -160,7 +160,7 @@ impl Interface for Rust {
Ok(()) Ok(())
} }
fn dev<F: Fn(ExitStatus, ExitReason) + Send + Sync + 'static>( fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self, &mut self,
mut options: Options, mut options: Options,
on_exit: F, on_exit: F,
@ -426,7 +426,7 @@ impl Rust {
shared_options(mobile, args, features, &self.app_settings); 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, &mut self,
options: Options, options: Options,
run_args: Vec<String>, run_args: Vec<String>,

View File

@ -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, options: Options,
run_args: Vec<String>, run_args: Vec<String>,
available_targets: &mut Option<Vec<Target>>, available_targets: &mut Option<Vec<Target>>,
@ -93,7 +93,7 @@ pub fn run_dev<F: Fn(ExitStatus, ExitReason) + Send + Sync + 'static>(
available_targets, available_targets,
config_features, config_features,
move |status, reason| { move |status, reason| {
if status.success() { if status == Some(0) {
let bin_path = let bin_path =
rename_app(target_os, &bin_path, product_name.as_deref()).expect("failed to rename app"); rename_app(target_os, &bin_path, product_name.as_deref()).expect("failed to rename app");
let mut app = Command::new(bin_path); let mut app = Command::new(bin_path);
@ -192,7 +192,7 @@ pub fn build(
Ok(()) Ok(())
} }
fn build_dev_app<F: FnOnce(ExitStatus, ExitReason) + Send + 'static>( fn build_dev_app<F: FnOnce(Option<i32>, ExitReason) + Send + 'static>(
options: Options, options: Options,
available_targets: &mut Option<Vec<Target>>, available_targets: &mut Option<Vec<Target>>,
config_features: Vec<String>, config_features: Vec<String>,
@ -259,10 +259,10 @@ fn build_dev_app<F: FnOnce(ExitStatus, ExitReason) + Send + 'static>(
let build_child_ = build_child.clone(); let build_child_ = build_child.clone();
std::thread::spawn(move || { 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() { if status.success() {
on_exit(status, ExitReason::NormalExit); on_exit(status.code(), ExitReason::NormalExit);
} else { } else {
let is_cargo_compile_error = stderr_lines let is_cargo_compile_error = stderr_lines
.lock() .lock()
@ -273,7 +273,7 @@ fn build_dev_app<F: FnOnce(ExitStatus, ExitReason) + Send + 'static>(
stderr_lines.lock().unwrap().clear(); stderr_lines.lock().unwrap().clear();
on_exit( on_exit(
status, status.code(),
if status.code() == Some(101) && is_cargo_compile_error { if status.code() == Some(101) && is_cargo_compile_error {
ExitReason::CompilationFailed ExitReason::CompilationFailed
} else { } else {