fix(codegen): use TAURI_ENV_TARGET_TRIPLE to determine the current platform-specific config (#9646)

* fix(codegen): use `TAURI_ENV_TARGET_TRIPLE` to determine the current platform-specific config

* set env var

* Update .changes/tauri-codegen-use-correct-env.md

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Amr Bashir 2024-05-28 19:48:24 +03:00 committed by GitHub
parent bcc63bf36d
commit ccc3ea729d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 25 additions and 6 deletions

View File

@ -0,0 +1,5 @@
---
"tauri-codegen": "patch"
---
Use `TAURI_ENV_TARGET_TRIPLE` (which is set by `tauri-build`) to determine the target when reading the config file.

View File

@ -0,0 +1,6 @@
---
"tauri": "patch:bug"
---
Parse the correct platform `tauri.<platform>.conf.json` config file when building or developing for mobile.

View File

@ -530,6 +530,8 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
tauri_utils::plugin::load_global_api_scripts(&out_dir);
println!("cargo:rustc-env=TAURI_ENV_TARGET_TRIPLE={target_triple}");
// when running codegen in this build script, we need to access the env var directly
std::env::set_var("TAURI_ENV_TARGET_TRIPLE", &target_triple);
// TODO: far from ideal, but there's no other way to get the target dir, see <https://github.com/rust-lang/cargo/issues/5457>
let target_dir = out_dir

View File

@ -139,8 +139,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
assets,
} = data;
let target = std::env::var("TARGET")
.or_else(|_| std::env::var("TAURI_ENV_TARGET_TRIPLE"))
let target = std::env::var("TAURI_ENV_TARGET_TRIPLE")
.as_deref()
.map(Target::from_triple)
.unwrap_or_else(|_| Target::current());

View File

@ -18,6 +18,7 @@ use std::{
path::{Path, PathBuf},
};
pub use tauri_utils::config::{parse::ConfigError, Config};
use tauri_utils::platform::Target;
mod context;
pub mod embedded_assets;
@ -63,22 +64,28 @@ pub fn get_config(path: &Path) -> Result<(Config, PathBuf), CodegenConfigError>
.map(ToOwned::to_owned)
.ok_or_else(|| CodegenConfigError::Parent(path.into_owned()))?;
let target = std::env::var("TAURI_ENV_TARGET_TRIPLE")
.as_deref()
.map(Target::from_triple)
.unwrap_or_else(|_| Target::current());
// in the future we may want to find a way to not need the TAURI_CONFIG env var so that
// it is impossible for the content of two separate configs to get mixed up. The chances are
// already unlikely unless the developer goes out of their way to run the cli on a different
// project than the target crate.
let mut config = serde_json::from_value(tauri_utils::config::parse::read_from(
tauri_utils::platform::Target::current(),
target,
parent.clone(),
)?)?;
if let Ok(env) = std::env::var("TAURI_CONFIG") {
let merge_config: serde_json::Value =
serde_json::from_str(&env).map_err(CodegenConfigError::FormatInline)?;
json_patch::merge(&mut config, &merge_config);
}
let old_cwd = std::env::current_dir().map_err(CodegenConfigError::CurrentDir)?;
// Set working directory to where `tauri.config.json` is, so that relative paths in it are parsed correctly.
let old_cwd = std::env::current_dir().map_err(CodegenConfigError::CurrentDir)?;
std::env::set_current_dir(parent.clone()).map_err(CodegenConfigError::CurrentDir)?;
let config = serde_json::from_value(config)?;

View File

@ -586,9 +586,9 @@ pub fn build_wix_app_installer(
data.insert("feature_group_refs", to_json(&wix.feature_group_refs));
data.insert("feature_refs", to_json(&wix.feature_refs));
data.insert("merge_refs", to_json(&wix.merge_refs));
fragment_paths = wix.fragment_paths.clone();
fragment_paths.clone_from(&wix.fragment_paths);
enable_elevated_update_task = wix.enable_elevated_update_task;
custom_template_path = wix.template.clone();
custom_template_path.clone_from(&wix.template);
if let Some(banner_path) = &wix.banner_path {
let filename = banner_path