diff --git a/.changes/change-pr-14379.md b/.changes/change-pr-14379.md new file mode 100644 index 000000000000..96efd7519772 --- /dev/null +++ b/.changes/change-pr-14379.md @@ -0,0 +1,5 @@ +--- +"tauri-cli": patch:enhance +--- + +Properly read the `required-features` field of binaries in Cargo.toml to prevent bundling issues when the features weren't enabled. diff --git a/crates/tauri-cli/src/interface/mod.rs b/crates/tauri-cli/src/interface/mod.rs index e0f74ebf7420..00ca4c3d1437 100644 --- a/crates/tauri-cli/src/interface/mod.rs +++ b/crates/tauri-cli/src/interface/mod.rs @@ -34,7 +34,7 @@ pub trait AppSettings { features: &[String], ) -> crate::Result; fn app_binary_path(&self, options: &Options) -> crate::Result; - fn get_binaries(&self) -> crate::Result>; + fn get_binaries(&self, options: &Options) -> crate::Result>; fn app_name(&self) -> Option; fn lib_name(&self) -> Option; @@ -57,7 +57,7 @@ pub trait AppSettings { tauri_utils::platform::target_triple().context("failed to get target triple")? }; - let mut bins = self.get_binaries()?; + let mut bins = self.get_binaries(&options)?; 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()); diff --git a/crates/tauri-cli/src/interface/rust.rs b/crates/tauri-cli/src/interface/rust.rs index fd49ac35bb57..0f7d2fd02973 100644 --- a/crates/tauri-cli/src/interface/rust.rs +++ b/crates/tauri-cli/src/interface/rust.rs @@ -695,11 +695,13 @@ struct WorkspacePackageSettings { } #[derive(Clone, Debug, Deserialize)] +#[serde(rename_all = "kebab-case")] struct BinarySettings { name: String, /// This is from nightly: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#different-binary-name filename: Option, path: Option, + required_features: Option>, } impl BinarySettings { @@ -919,7 +921,7 @@ impl AppSettings for RustAppSettings { } fn app_binary_path(&self, options: &Options) -> crate::Result { - let binaries = self.get_binaries()?; + let binaries = self.get_binaries(options)?; let bin_name = binaries .iter() .find(|x| x.main()) @@ -945,8 +947,8 @@ impl AppSettings for RustAppSettings { Ok(path) } - fn get_binaries(&self) -> crate::Result> { - let mut binaries: Vec = vec![]; + fn get_binaries(&self, options: &Options) -> crate::Result> { + let mut binaries = Vec::new(); if let Some(bins) = &self.cargo_settings.bin { let default_run = self @@ -955,6 +957,14 @@ impl AppSettings for RustAppSettings { .clone() .unwrap_or_default(); for bin in bins { + if let (Some(req_features), Some(opt_features)) = + (&bin.required_features, &options.features) + { + // Check if all required features are enabled. + if !req_features.iter().all(|feat| opt_features.contains(feat)) { + continue; + } + } let file_name = bin.file_name(); let is_main = file_name == self.cargo_package_settings.name || file_name == default_run; binaries.push(BundleBinary::with_path( @@ -1659,7 +1669,7 @@ mod tests { #[test] fn parse_cargo_option() { - let args = vec![ + let args = [ "build".into(), "--".into(), "--profile".into(),