fix: fix main_binary_name includes .exe regression on Windows (#11011)

* 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 <lucas@tauri.app>
This commit is contained in:
Amr Bashir 2024-09-15 22:30:47 +03:00 committed by GitHub
parent 5a0e922d40
commit 94e9d476ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 34 additions and 18 deletions

View File

@ -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`

View File

@ -869,13 +869,25 @@ impl Settings {
.iter() .iter()
.find(|bin| bin.main) .find(|bin| bin.main)
.context("failed to find main binary, make sure you have a `package > default-run` in the Cargo.toml file") .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) .map_err(Into::into)
} }
/// Returns the path to the specified binary. /// Returns the path to the specified binary.
pub fn binary_path(&self, binary: &BundleBinary) -> PathBuf { 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. /// Returns the list of binaries to bundle.

View File

@ -337,7 +337,7 @@ fn build_nsis_app_installer(
data.insert("language_files", to_json(language_files_paths)); data.insert("language_files", to_json(language_files_paths));
let main_binary = settings.main_binary()?; 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_name", to_json(main_binary.name()));
data.insert("main_binary_path", to_json(&main_binary_path)); data.insert("main_binary_path", to_json(&main_binary_path));

View File

@ -32,7 +32,7 @@ pub trait AppSettings {
features: &[String], features: &[String],
) -> crate::Result<tauri_bundler::BundleSettings>; ) -> crate::Result<tauri_bundler::BundleSettings>;
fn app_binary_path(&self, options: &Options) -> crate::Result<PathBuf>; 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 app_name(&self) -> Option<String>;
fn lib_name(&self) -> Option<String>; fn lib_name(&self) -> Option<String>;
@ -55,7 +55,7 @@ pub trait AppSettings {
tauri_utils::platform::target_triple()? 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 { if let Some(main_binary_name) = &config.main_binary_name {
let main = bins.iter_mut().find(|b| b.main()).context("no main bin?")?; let main = bins.iter_mut().find(|b| b.main()).context("no main bin?")?;
main.set_name(main_binary_name.to_owned()); main.set_name(main_binary_name.to_owned());

View File

@ -864,7 +864,7 @@ impl AppSettings for RustAppSettings {
} }
fn app_binary_path(&self, options: &Options) -> crate::Result<PathBuf> { 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 let bin_name = binaries
.iter() .iter()
.find(|x| x.main()) .find(|x| x.main())
@ -884,15 +884,9 @@ impl AppSettings for RustAppSettings {
Ok(out_dir.join(bin_name).with_extension(ext)) 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 mut binaries: Vec<BundleBinary> = vec![];
let ext = if target.contains("windows") {
".exe"
} else {
""
};
if let Some(bins) = &self.cargo_settings.bin { if let Some(bins) = &self.cargo_settings.bin {
let default_run = self let default_run = self
.package_settings .package_settings
@ -900,9 +894,12 @@ impl AppSettings for RustAppSettings {
.clone() .clone()
.unwrap_or_default(); .unwrap_or_default();
for bin in bins { for bin in bins {
let name = format!("{}{}", bin.name, ext);
let is_main = bin.name == self.cargo_package_settings.name || bin.name == default_run; 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(),
))
} }
} }
@ -943,7 +940,7 @@ impl AppSettings for RustAppSettings {
.iter() .iter()
.any(|bin| bin.name() == name || path.ends_with(bin.src_path().unwrap_or(&"".to_string()))); .any(|bin| bin.name() == name || path.ends_with(bin.src_path().unwrap_or(&"".to_string())));
if !bin_exists { if !bin_exists {
binaries.push(BundleBinary::new(format!("{name}{ext}"), false)) binaries.push(BundleBinary::new(name, false))
} }
} }
@ -951,13 +948,13 @@ impl AppSettings for RustAppSettings {
if let Some(binary) = binaries.iter_mut().find(|bin| bin.name() == default_run) { if let Some(binary) = binaries.iter_mut().find(|bin| bin.name() == default_run) {
binary.set_main(true); binary.set_main(true);
} else { } else {
binaries.push(BundleBinary::new(format!("{}{}", default_run, ext), true)); binaries.push(BundleBinary::new(default_run.clone(), true));
} }
} }
match binaries.len() { match binaries.len() {
0 => binaries.push(BundleBinary::new( 0 => binaries.push(BundleBinary::new(
format!("{}{}", self.cargo_package_settings.name, ext), self.cargo_package_settings.name.clone(),
true, true,
)), )),
1 => binaries.get_mut(0).unwrap().set_main(true), 1 => binaries.get_mut(0).unwrap().set_main(true),