Skip to content

Commit 5eb036f

Browse files
authored
feat(core): restart() handles binary name change for macOS (#11002)
1 parent 4ba7b94 commit 5eb036f

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:enhance
3+
---
4+
5+
Handle macOS binary name change on the `process::restart` function.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ webkit2gtk = { version = "=2.0.1", features = ["v2_40"] }
103103

104104
[target."cfg(target_os = \"macos\")".dependencies]
105105
embed_plist = "1.2"
106+
plist = "1"
106107
cocoa = "0.26"
107108
objc = "0.2"
108109
window-vibrancy = "0.5"

crates/tauri/src/process.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,59 @@ pub fn restart(env: &Env) -> ! {
7575
use std::process::{exit, Command};
7676

7777
if let Ok(path) = current_binary(env) {
78-
Command::new(path)
79-
// first arg is the binary name, must skip it
78+
// on macOS on updates the binary name might have changed
79+
// so we'll read the Contents/Info.plist file to determine the binary path
80+
#[cfg(target_os = "macos")]
81+
restart_macos_app(&path, env);
82+
83+
if let Err(e) = Command::new(path)
8084
.args(env.args_os.iter().skip(1).collect::<Vec<_>>())
8185
.spawn()
82-
.expect("application failed to start");
86+
{
87+
log::error!("failed to restart app: {e}");
88+
}
8389
}
8490

8591
exit(0);
8692
}
93+
94+
#[cfg(target_os = "macos")]
95+
fn restart_macos_app(current_binary: &PathBuf, env: &Env) {
96+
use std::process::{exit, Command};
97+
98+
if let Some(macos_directory) = current_binary.parent() {
99+
if macos_directory.components().last()
100+
!= Some(std::path::Component::Normal(std::ffi::OsStr::new("MacOS")))
101+
{
102+
return;
103+
}
104+
105+
if let Some(contents_directory) = macos_directory.parent() {
106+
if contents_directory.components().last()
107+
!= Some(std::path::Component::Normal(std::ffi::OsStr::new(
108+
"Contents",
109+
)))
110+
{
111+
return;
112+
}
113+
114+
if let Ok(info_plist) =
115+
plist::from_file::<_, plist::Dictionary>(contents_directory.join("Info.plist"))
116+
{
117+
if let Some(binary_name) = info_plist
118+
.get("CFBundleExecutable")
119+
.and_then(|v| v.as_string())
120+
{
121+
if let Err(e) = Command::new(macos_directory.join(binary_name))
122+
.args(env.args_os.iter().skip(1).collect::<Vec<_>>())
123+
.spawn()
124+
{
125+
log::error!("failed to restart app: {e}");
126+
}
127+
128+
exit(0);
129+
}
130+
}
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)