Skip to content

Commit fa028eb

Browse files
authored
refactor: do not pass entire config from CLI to core, send patch instead (#4598)
1 parent 2e61aba commit fa028eb

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed

.changes/tauri-config-refactor.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
"tauri-build": patch
5+
"tauri-codegen": patch
6+
---
7+
8+
The `TAURI_CONFIG` environment variable now represents the configuration to be merged instead of the entire JSON.

core/tauri-build/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ tauri-utils = { version = "1.0.2", path = "../tauri-utils", features = [ "build"
2424
cargo_toml = "0.11"
2525
serde_json = "1"
2626
heck = "0.4"
27+
json-patch = "0.2"
2728

2829
[target."cfg(windows)".dependencies]
2930
winres = "0.1"

core/tauri-build/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,14 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
185185
#[cfg(feature = "config-json5")]
186186
println!("cargo:rerun-if-changed=tauri.conf.json5");
187187

188-
let config: Config = if let Ok(env) = std::env::var("TAURI_CONFIG") {
189-
serde_json::from_str(&env)?
190-
} else {
191-
serde_json::from_value(tauri_utils::config::parse::read_from(
192-
std::env::current_dir().unwrap(),
193-
)?)?
194-
};
188+
let mut config = serde_json::from_value(tauri_utils::config::parse::read_from(
189+
std::env::current_dir().unwrap(),
190+
)?)?;
191+
if let Ok(env) = std::env::var("TAURI_CONFIG") {
192+
let merge_config: serde_json::Value = serde_json::from_str(&env)?;
193+
json_patch::merge(&mut config, &merge_config);
194+
}
195+
let config: Config = serde_json::from_value(config)?;
195196

196197
cfg_alias("dev", !has_feature("custom-protocol"));
197198

core/tauri-codegen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ uuid = { version = "1", features = [ "v4" ] }
2828
semver = "1"
2929
ico = "0.1"
3030
png = "0.17"
31+
json-patch = "0.2"
3132

3233
[target."cfg(target_os = \"macos\")".dependencies]
3334
plist = "1"

core/tauri-codegen/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ pub fn get_config(path: &Path) -> Result<(Config, PathBuf), CodegenConfigError>
5757
// it is impossible for the content of two separate configs to get mixed up. The chances are
5858
// already unlikely unless the developer goes out of their way to run the cli on a different
5959
// project than the target crate.
60-
let config = if let Ok(env) = std::env::var("TAURI_CONFIG") {
61-
serde_json::from_str(&env).map_err(CodegenConfigError::FormatInline)?
62-
} else {
63-
serde_json::from_value(tauri_utils::config::parse::read_from(parent.clone())?)?
64-
};
60+
let mut config = serde_json::from_value(tauri_utils::config::parse::read_from(parent.clone())?)?;
61+
if let Ok(env) = std::env::var("TAURI_CONFIG") {
62+
let merge_config: serde_json::Value =
63+
serde_json::from_str(&env).map_err(CodegenConfigError::FormatInline)?;
64+
json_patch::merge(&mut config, &merge_config);
65+
}
6566

66-
Ok((config, parent))
67+
Ok((serde_json::from_value(config)?, parent))
6768
}

examples/api/src-tauri/Cargo.lock

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

tooling/cli/src/helpers/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ fn get_internal(merge_config: Option<&str>, reload: bool) -> crate::Result<Confi
118118
}
119119

120120
if let Some(merge_config) = merge_config {
121+
set_var("TAURI_CONFIG", merge_config);
121122
let merge_config: JsonValue =
122123
serde_json::from_str(merge_config).with_context(|| "failed to parse config to merge")?;
123124
merge(&mut config, &merge_config);
@@ -145,7 +146,6 @@ fn get_internal(merge_config: Option<&str>, reload: bool) -> crate::Result<Confi
145146
}
146147

147148
let config: Config = serde_json::from_value(config)?;
148-
set_var("TAURI_CONFIG", serde_json::to_string(&config)?);
149149

150150
*config_handle().lock().unwrap() = Some(ConfigMetadata {
151151
inner: config,

0 commit comments

Comments
 (0)