Skip to content

Commit

Permalink
fix: fix main_binary_name includes .exe regression on Windows (#1…
Browse files Browse the repository at this point in the history
…1011)

* fix: fix `main_binary_name` includes `.exe` regression on Windows

* Update crates/tauri-bundler/src/bundle/settings.rs

* Update .changes/main_binary_name-exe.md

---------

Co-authored-by: Lucas Fernandes Nogueira <[email protected]>
  • Loading branch information
amrbashir and lucasfernog committed Sep 15, 2024
1 parent 5a0e922 commit 94e9d47
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .changes/main_binary_name-exe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri-bundler": "patch:bug"
"tauri-cli": "patch:bug"
"@tauri-apps/cli": "patch:bug"
---

Fix `main_binary_name` in custom wix and nsis templates including `.exe`
16 changes: 14 additions & 2 deletions crates/tauri-bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,13 +869,25 @@ impl Settings {
.iter()
.find(|bin| bin.main)
.context("failed to find main binary, make sure you have a `package > default-run` in the Cargo.toml file")
.map(|b| b.name.as_str())
.map(|b| b.name())
.map_err(Into::into)
}

/// Returns the path to the specified binary.
pub fn binary_path(&self, binary: &BundleBinary) -> PathBuf {
self.project_out_directory.join(binary.name())
let target_os = self
.target()
.split('-')
.nth(2)
.unwrap_or(std::env::consts::OS);

let path = self.project_out_directory.join(binary.name());

if target_os == "windows" {
path.with_extension("exe")
} else {
path
}
}

/// Returns the list of binaries to bundle.
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-bundler/src/bundle/windows/nsis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ fn build_nsis_app_installer(
data.insert("language_files", to_json(language_files_paths));

let main_binary = settings.main_binary()?;
let main_binary_path = settings.binary_path(main_binary).with_extension("exe");
let main_binary_path = settings.binary_path(main_binary);
data.insert("main_binary_name", to_json(main_binary.name()));
data.insert("main_binary_path", to_json(&main_binary_path));

Expand Down
4 changes: 2 additions & 2 deletions crates/tauri-cli/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait AppSettings {
features: &[String],
) -> crate::Result<tauri_bundler::BundleSettings>;
fn app_binary_path(&self, options: &Options) -> crate::Result<PathBuf>;
fn get_binaries(&self, target: &str) -> crate::Result<Vec<tauri_bundler::BundleBinary>>;
fn get_binaries(&self) -> crate::Result<Vec<tauri_bundler::BundleBinary>>;
fn app_name(&self) -> Option<String>;
fn lib_name(&self) -> Option<String>;

Expand All @@ -55,7 +55,7 @@ pub trait AppSettings {
tauri_utils::platform::target_triple()?
};

let mut bins = self.get_binaries(&target)?;
let mut bins = self.get_binaries()?;
if let Some(main_binary_name) = &config.main_binary_name {
let main = bins.iter_mut().find(|b| b.main()).context("no main bin?")?;
main.set_name(main_binary_name.to_owned());
Expand Down
23 changes: 10 additions & 13 deletions crates/tauri-cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ impl AppSettings for RustAppSettings {
}

fn app_binary_path(&self, options: &Options) -> crate::Result<PathBuf> {
let binaries = self.get_binaries(&self.target_triple)?;
let binaries = self.get_binaries()?;
let bin_name = binaries
.iter()
.find(|x| x.main())
Expand All @@ -884,25 +884,22 @@ impl AppSettings for RustAppSettings {
Ok(out_dir.join(bin_name).with_extension(ext))
}

fn get_binaries(&self, target: &str) -> crate::Result<Vec<BundleBinary>> {
fn get_binaries(&self) -> crate::Result<Vec<BundleBinary>> {
let mut binaries: Vec<BundleBinary> = vec![];

let ext = if target.contains("windows") {
".exe"
} else {
""
};

if let Some(bins) = &self.cargo_settings.bin {
let default_run = self
.package_settings
.default_run
.clone()
.unwrap_or_default();
for bin in bins {
let name = format!("{}{}", bin.name, ext);
let is_main = bin.name == self.cargo_package_settings.name || bin.name == default_run;
binaries.push(BundleBinary::with_path(name, is_main, bin.path.clone()))
binaries.push(BundleBinary::with_path(
bin.name.clone(),
is_main,
bin.path.clone(),
))
}
}

Expand Down Expand Up @@ -943,21 +940,21 @@ impl AppSettings for RustAppSettings {
.iter()
.any(|bin| bin.name() == name || path.ends_with(bin.src_path().unwrap_or(&"".to_string())));
if !bin_exists {
binaries.push(BundleBinary::new(format!("{name}{ext}"), false))
binaries.push(BundleBinary::new(name, false))
}
}

if let Some(default_run) = self.package_settings.default_run.as_ref() {
if let Some(binary) = binaries.iter_mut().find(|bin| bin.name() == default_run) {
binary.set_main(true);
} else {
binaries.push(BundleBinary::new(format!("{}{}", default_run, ext), true));
binaries.push(BundleBinary::new(default_run.clone(), true));
}
}

match binaries.len() {
0 => binaries.push(BundleBinary::new(
format!("{}{}", self.cargo_package_settings.name, ext),
self.cargo_package_settings.name.clone(),
true,
)),
1 => binaries.get_mut(0).unwrap().set_main(true),
Expand Down

0 comments on commit 94e9d47

Please sign in to comment.