Skip to content

Commit

Permalink
fix(core): restart cannot handle binary name change on macOS (#10991)
Browse files Browse the repository at this point in the history
* 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.

* fix: use Info.plist to determine binary name

* use log instead of eprintln

* move log
  • Loading branch information
lucasfernog committed Sep 14, 2024
1 parent 8a0e93b commit 26d243f
Show file tree
Hide file tree
Showing 5 changed files with 68 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.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ sys-locale = { version = "0.3", optional = true }
tracing = { version = "0.1", optional = true }
indexmap = { version = "1", features = [ "std", "serde" ], optional = true }
getrandom = { version = "0.2", features = [ "std" ] }
log = "0.4"

[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
rfd = { version = "0.10", optional = true, features = [ "gtk3", "common-controls-v6" ] } # 0.11 raised gtk version
Expand All @@ -114,6 +115,7 @@ nix = { version = "0.26.0", default-features = false, features = [ "user", "sock

[target."cfg(target_os = \"macos\")".dependencies]
embed_plist = "1.2"
plist = "1"
cocoa = "0.24" # wry still uses 0.24
objc = "0.2" # Do not update without consensus

Expand Down
53 changes: 49 additions & 4 deletions core/tauri/src/api/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,56 @@ 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/Info.plist file to determine the binary path
#[cfg(target_os = "macos")]
restart_macos_app(&path, env);

if let Err(e) = Command::new(path).args(&env.args).spawn() {
log::error!("failed to restart app: {e}");
}
}

exit(0);
}

#[cfg(target_os = "macos")]
fn restart_macos_app(current_binary: &PathBuf, env: &Env) {
use std::process::{exit, Command};

if let Some(macos_directory) = current_binary.parent() {
if macos_directory.components().last()
!= Some(std::path::Component::Normal(std::ffi::OsStr::new("MacOS")))
{
return;
}

if let Some(contents_directory) = macos_directory.parent() {
if contents_directory.components().last()
!= Some(std::path::Component::Normal(std::ffi::OsStr::new(
"Contents",
)))
{
return;
}

if let Ok(info_plist) =
plist::from_file::<_, plist::Dictionary>(contents_directory.join("Info.plist"))
{
if let Some(binary_name) = info_plist
.get("CFBundleExecutable")
.and_then(|v| v.as_string())
{
if let Err(e) = Command::new(macos_directory.join(binary_name))
.args(&env.args)
.spawn()
{
log::error!("failed to restart app: {e}");
}

exit(0);
}
}
}
}
}
16 changes: 9 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 26d243f

Please sign in to comment.