From 09e89537b38c74732a6f63ef154705496e5dc1fb Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 13 Sep 2024 17:56:40 -0300 Subject: [PATCH] fix(core): `restart` cannot handle binary name change on macOS Tauri v2 changed the default binary name of the bundled apps (no longer follows the productName, just uses the default name from Cargo instead). This breaks the `restart` function, which expects the current binary path to match the new one. Due to this change, the restart() function for macOS is broken - the .app is correctly replaced even if the productName changed, but the restart() function cannot handle the new binary path. This change adds a simple check on macOS to read the `Contents/MacOS` folder and if it only contains a single binary, we use it to restart instead. This inference cannot be used if there's sidecars, so in this case we just let the existing implementation run and if it fails to restart, we do not panic but only warn and exit instead. AppImage updates are not affected by this, and the Windows installer is responsible for restarting, so this change is only applied to macOS binaries. --- .changes/fix-restart-macos.md | 6 ++++ core/tauri/src/api/process.rs | 47 ++++++++++++++++++++++++++++--- examples/api/src-tauri/Cargo.lock | 14 ++++----- 3 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 .changes/fix-restart-macos.md diff --git a/.changes/fix-restart-macos.md b/.changes/fix-restart-macos.md new file mode 100644 index 000000000000..b7db3b536469 --- /dev/null +++ b/.changes/fix-restart-macos.md @@ -0,0 +1,6 @@ +--- +"tauri": patch:bug +--- + +Fixes the restart() function not being compatible with the v2 binary name change. +Additionally, do not panic if we somehow failed to restart, and only exit instead. diff --git a/core/tauri/src/api/process.rs b/core/tauri/src/api/process.rs index 1cb4c0e08853..03b8547883fa 100644 --- a/core/tauri/src/api/process.rs +++ b/core/tauri/src/api/process.rs @@ -82,10 +82,49 @@ pub fn restart(env: &Env) { use std::process::{exit, Command}; if let Ok(path) = current_binary(env) { - Command::new(path) - .args(&env.args) - .spawn() - .expect("application failed to start"); + // on macOS on updates the binary name might have changed + // so we'll read the Contents/MacOS folder instead to infer the actual binary path + #[cfg(target_os = "macos")] + if let Some(parent) = path.parent() { + if parent.components().last() + == Some(std::path::Component::Normal(std::ffi::OsStr::new("MacOS"))) + { + let macos_binaries = std::fs::read_dir(parent) + .map(|dir| { + dir + .into_iter() + .flatten() + .map(|entry| entry.path()) + .collect::>() + }) + .unwrap_or_default(); + match macos_binaries.len() { + 0 => { + // should never happen, but let's not panic here since it's a crucial feature for updates + exit(1); + } + 1 => { + // we have one binary (no sidecar) so we should use it to restart + if let Err(e) = Command::new(macos_binaries.first().unwrap()) + .args(&env.args) + .spawn() + { + eprintln!("failed to restart app: {e}"); + } + + exit(0); + } + _ => { + // in case of sidecars we don't have enough information here to decide what's the right binary name + // so let's hope the binary name didn't change by running the Command::spawn below + } + } + } + } + + if let Err(e) = Command::new(path).args(&env.args).spawn() { + eprintln!("failed to restart app: {e}"); + } } exit(0); diff --git a/examples/api/src-tauri/Cargo.lock b/examples/api/src-tauri/Cargo.lock index 65e4049085b6..af3f065ec5f1 100644 --- a/examples/api/src-tauri/Cargo.lock +++ b/examples/api/src-tauri/Cargo.lock @@ -4027,7 +4027,7 @@ checksum = "69758bda2e78f098e4ccb393021a0963bb3442eac05f135c30f61b7370bbafae" [[package]] name = "tauri" -version = "1.7.1" +version = "1.7.2" dependencies = [ "anyhow", "base64 0.22.1", @@ -4093,7 +4093,7 @@ dependencies = [ [[package]] name = "tauri-build" -version = "1.5.3" +version = "1.5.4" dependencies = [ "anyhow", "cargo_toml", @@ -4112,7 +4112,7 @@ dependencies = [ [[package]] name = "tauri-codegen" -version = "1.4.4" +version = "1.4.5" dependencies = [ "base64 0.21.7", "brotli", @@ -4136,7 +4136,7 @@ dependencies = [ [[package]] name = "tauri-macros" -version = "1.4.5" +version = "1.4.6" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -4148,7 +4148,7 @@ dependencies = [ [[package]] name = "tauri-runtime" -version = "0.14.4" +version = "0.14.5" dependencies = [ "gtk", "http", @@ -4167,7 +4167,7 @@ dependencies = [ [[package]] name = "tauri-runtime-wry" -version = "0.14.9" +version = "0.14.10" dependencies = [ "arboard", "cocoa 0.24.1", @@ -4186,7 +4186,7 @@ dependencies = [ [[package]] name = "tauri-utils" -version = "1.6.0" +version = "1.6.1" dependencies = [ "aes-gcm", "brotli",