Skip to content

Commit

Permalink
fix(core): restart cannot handle binary name change on macOS
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lucasfernog committed Sep 13, 2024
1 parent 2e87e85 commit 09e8953
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-restart-macos.md
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 43 additions & 4 deletions core/tauri/src/api/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
})
.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);
Expand Down
14 changes: 7 additions & 7 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 09e8953

Please sign in to comment.