Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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