Skip to content

Commit fb0d997

Browse files
authored
fix(cli): regression on --config not accepting file paths (#8783)
* fix(cli): regression on --config not accepting file paths * enhance dev server config parsing * use serde_json::json! * pass config to setup
1 parent 8751c32 commit fb0d997

13 files changed

Lines changed: 144 additions & 139 deletions

File tree

.changes/fix-config-arg.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
Fixes a regression on the `--config` argument not accepting file paths.

examples/api/src-tauri/Cargo.lock

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

tooling/cli/src/build.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ use crate::{
66
helpers::{
77
app_paths::{app_dir, tauri_dir},
88
command_env,
9-
config::{get as get_config, FrontendDist, HookCommand, MERGE_CONFIG_EXTENSION_NAME},
10-
resolve_merge_config,
9+
config::{get as get_config, ConfigHandle, FrontendDist, HookCommand},
1110
updater_signature::{secret_key as updater_secret_key, sign_file},
1211
},
1312
interface::{AppInterface, AppSettings, Interface},
14-
CommandExt, Result,
13+
CommandExt, ConfigValue, Result,
1514
};
1615
use anyhow::{bail, Context};
1716
use base64::Engine;
@@ -57,7 +56,7 @@ pub struct Options {
5756
pub bundles: Option<Vec<String>>,
5857
/// JSON string or path to JSON file to merge with tauri.conf.json
5958
#[clap(short, long)]
60-
pub config: Option<String>,
59+
pub config: Option<ConfigValue>,
6160
/// Command line arguments passed to the runner. Use `--` to explicitly mark the start of the arguments.
6261
pub args: Vec<String>,
6362
/// Skip prompting for values
@@ -75,14 +74,14 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
7574
.map(Target::from_triple)
7675
.unwrap_or_else(Target::current);
7776

78-
let config = get_config(target, options.config.as_deref())?;
77+
let config = get_config(target, options.config.as_ref().map(|c| &c.0))?;
7978

8079
let mut interface = AppInterface::new(
8180
config.lock().unwrap().as_ref().unwrap(),
8281
options.target.clone(),
8382
)?;
8483

85-
setup(target, &interface, &mut options, false)?;
84+
setup(&interface, &mut options, config.clone(), false)?;
8685

8786
let config_guard = config.lock().unwrap();
8887
let config_ = config_guard.as_ref().unwrap();
@@ -267,27 +266,20 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
267266
}
268267

269268
pub fn setup(
270-
target: Target,
271269
interface: &AppInterface,
272270
options: &mut Options,
271+
config: ConfigHandle,
273272
mobile: bool,
274273
) -> Result<()> {
275-
let (merge_config, merge_config_path) = resolve_merge_config(&options.config)?;
276-
options.config = merge_config;
277-
278-
let config = get_config(target, options.config.as_deref())?;
279-
280274
let tauri_path = tauri_dir();
281275
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;
282276

283277
let config_guard = config.lock().unwrap();
284278
let config_ = config_guard.as_ref().unwrap();
285279

286-
let bundle_identifier_source = match config_.find_bundle_identifier_overwriter() {
287-
Some(source) if source == MERGE_CONFIG_EXTENSION_NAME => merge_config_path.unwrap_or(source),
288-
Some(source) => source,
289-
None => "tauri.conf.json".into(),
290-
};
280+
let bundle_identifier_source = config_
281+
.find_bundle_identifier_overwriter()
282+
.unwrap_or_else(|| "tauri.conf.json".into());
291283

292284
if config_.identifier == "com.tauri.dev" {
293285
error!(

tooling/cli/src/dev.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use crate::{
66
helpers::{
77
app_paths::{app_dir, tauri_dir},
88
command_env,
9-
config::{get as get_config, reload as reload_config, BeforeDevCommand, FrontendDist},
10-
resolve_merge_config,
9+
config::{
10+
get as get_config, reload as reload_config, BeforeDevCommand, ConfigHandle, FrontendDist,
11+
},
1112
},
1213
interface::{AppInterface, DevProcess, ExitReason, Interface},
13-
CommandExt, Result,
14+
CommandExt, ConfigValue, Result,
1415
};
1516

1617
use anyhow::{bail, Context};
@@ -59,7 +60,7 @@ pub struct Options {
5960
pub exit_on_panic: bool,
6061
/// JSON string or path to JSON file to merge with tauri.conf.json
6162
#[clap(short, long)]
62-
pub config: Option<String>,
63+
pub config: Option<ConfigValue>,
6364
/// Run the code in release mode
6465
#[clap(long = "release")]
6566
pub release_mode: bool,
@@ -100,14 +101,14 @@ fn command_internal(mut options: Options) -> Result<()> {
100101
.map(Target::from_triple)
101102
.unwrap_or_else(Target::current);
102103

103-
let config = get_config(target, options.config.as_deref())?;
104+
let config = get_config(target, options.config.as_ref().map(|c| &c.0))?;
104105

105106
let mut interface = AppInterface::new(
106107
config.lock().unwrap().as_ref().unwrap(),
107108
options.target.clone(),
108109
)?;
109110

110-
setup(target, &interface, &mut options, false)?;
111+
setup(&interface, &mut options, config, false)?;
111112

112113
let exit_on_panic = options.exit_on_panic;
113114
let no_watch = options.no_watch;
@@ -160,16 +161,11 @@ pub fn local_ip_address(force: bool) -> &'static IpAddr {
160161
}
161162

162163
pub fn setup(
163-
target: Target,
164164
interface: &AppInterface,
165165
options: &mut Options,
166+
config: ConfigHandle,
166167
mobile: bool,
167168
) -> Result<()> {
168-
let (merge_config, _merge_config_path) = resolve_merge_config(&options.config)?;
169-
options.config = merge_config;
170-
171-
let config = get_config(target, options.config.as_deref())?;
172-
173169
let tauri_path = tauri_dir();
174170
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;
175171

@@ -344,15 +340,26 @@ pub fn setup(
344340
let server_url = format!("http://{server_url}");
345341
dev_url = Some(server_url.parse().unwrap());
346342

347-
if let Some(c) = &options.config {
348-
let mut c: tauri_utils::config::Config = serde_json::from_str(c)?;
349-
c.build.dev_url = dev_url.clone();
350-
options.config = Some(serde_json::to_string(&c).unwrap());
343+
if let Some(c) = &mut options.config {
344+
if let Some(build) = c
345+
.0
346+
.as_object_mut()
347+
.and_then(|root| root.get_mut("build"))
348+
.and_then(|build| build.as_object_mut())
349+
{
350+
build.insert("devUrl".into(), server_url.into());
351+
}
351352
} else {
352-
options.config = Some(format!(r#"{{ "build": {{ "devUrl": "{server_url}" }} }}"#))
353+
options
354+
.config
355+
.replace(crate::ConfigValue(serde_json::json!({
356+
"build": {
357+
"devUrl": server_url
358+
}
359+
})));
353360
}
354361

355-
reload_config(options.config.as_deref())?;
362+
reload_config(options.config.as_ref().map(|c| &c.0))?;
356363
}
357364
}
358365
}

tooling/cli/src/helpers/config.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use anyhow::Context;
65
use json_patch::merge;
76
use log::error;
87
use serde_json::Value as JsonValue;
@@ -118,7 +117,7 @@ fn config_handle() -> &'static ConfigHandle {
118117

119118
/// Gets the static parsed config from `tauri.conf.json`.
120119
fn get_internal(
121-
merge_config: Option<&str>,
120+
merge_config: Option<&serde_json::Value>,
122121
reload: bool,
123122
target: Target,
124123
) -> crate::Result<ConfigHandle> {
@@ -143,11 +142,10 @@ fn get_internal(
143142
}
144143

145144
if let Some(merge_config) = merge_config {
146-
set_var("TAURI_CONFIG", merge_config);
147-
let merge_config: JsonValue =
148-
serde_json::from_str(merge_config).with_context(|| "failed to parse config to merge")?;
149-
merge(&mut config, &merge_config);
150-
extensions.insert(MERGE_CONFIG_EXTENSION_NAME.into(), merge_config);
145+
let merge_config_str = serde_json::to_string(&merge_config).unwrap();
146+
set_var("TAURI_CONFIG", merge_config_str);
147+
merge(&mut config, merge_config);
148+
extensions.insert(MERGE_CONFIG_EXTENSION_NAME.into(), merge_config.clone());
151149
};
152150

153151
if config_path.extension() == Some(OsStr::new("json"))
@@ -198,11 +196,14 @@ fn get_internal(
198196
Ok(config_handle().clone())
199197
}
200198

201-
pub fn get(target: Target, merge_config: Option<&str>) -> crate::Result<ConfigHandle> {
199+
pub fn get(
200+
target: Target,
201+
merge_config: Option<&serde_json::Value>,
202+
) -> crate::Result<ConfigHandle> {
202203
get_internal(merge_config, false, target)
203204
}
204205

205-
pub fn reload(merge_config: Option<&str>) -> crate::Result<ConfigHandle> {
206+
pub fn reload(merge_config: Option<&serde_json::Value>) -> crate::Result<ConfigHandle> {
206207
let target = config_handle()
207208
.lock()
208209
.unwrap()

tooling/cli/src/helpers/mod.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ pub mod template;
1111
pub mod updater_signature;
1212
pub mod web_dev_server;
1313

14-
use anyhow::Context;
15-
1614
use std::{
1715
collections::HashMap,
1816
path::{Path, PathBuf},
@@ -54,16 +52,3 @@ pub fn cross_command(bin: &str) -> Command {
5452
let cmd = Command::new(bin);
5553
cmd
5654
}
57-
58-
pub fn resolve_merge_config(
59-
config: &Option<String>,
60-
) -> crate::Result<(Option<String>, Option<String>)> {
61-
match config {
62-
Some(config) if config.starts_with('{') => Ok((Some(config.to_string()), None)),
63-
Some(config) => Ok((
64-
Some(std::fs::read_to_string(config).with_context(|| "failed to read custom configuration")?),
65-
Some(config.clone()),
66-
)),
67-
None => Ok((None, None)),
68-
}
69-
}

tooling/cli/src/interface/rust.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ use tauri_bundler::{
2828
use tauri_utils::config::{parse::is_configuration_file, DeepLinkProtocol};
2929

3030
use super::{AppSettings, DevProcess, ExitReason, Interface};
31-
use crate::helpers::{
32-
app_paths::{app_dir, tauri_dir},
33-
config::{nsis_settings, reload as reload_config, wix_settings, BundleResources, Config},
31+
use crate::{
32+
helpers::{
33+
app_paths::{app_dir, tauri_dir},
34+
config::{nsis_settings, reload as reload_config, wix_settings, BundleResources, Config},
35+
},
36+
ConfigValue,
3437
};
3538
use tauri_utils::{display_path, platform::Target};
3639

@@ -48,7 +51,7 @@ pub struct Options {
4851
pub target: Option<String>,
4952
pub features: Option<Vec<String>>,
5053
pub args: Vec<String>,
51-
pub config: Option<String>,
54+
pub config: Option<ConfigValue>,
5255
pub no_watch: bool,
5356
}
5457

@@ -85,7 +88,7 @@ pub struct MobileOptions {
8588
pub debug: bool,
8689
pub features: Option<Vec<String>>,
8790
pub args: Vec<String>,
88-
pub config: Option<String>,
91+
pub config: Option<ConfigValue>,
8992
pub no_watch: bool,
9093
}
9194

@@ -186,7 +189,7 @@ impl Interface for Rust {
186189
rx.recv().unwrap();
187190
Ok(())
188191
} else {
189-
let config = options.config.clone();
192+
let config = options.config.clone().map(|c| c.0);
190193
let run = Arc::new(|rust: &mut Rust| {
191194
let on_exit = on_exit.clone();
192195
rust.run_dev(options.clone(), run_args.clone(), move |status, reason| {
@@ -215,7 +218,7 @@ impl Interface for Rust {
215218
runner(options)?;
216219
Ok(())
217220
} else {
218-
let config = options.config.clone();
221+
let config = options.config.clone().map(|c| c.0);
219222
let run = Arc::new(|_rust: &mut Rust| runner(options.clone()));
220223
self.run_dev_watcher(config, run)
221224
}
@@ -438,7 +441,7 @@ impl Rust {
438441

439442
fn run_dev_watcher<F: Fn(&mut Rust) -> crate::Result<Box<dyn DevProcess + Send>>>(
440443
&mut self,
441-
config: Option<String>,
444+
config: Option<serde_json::Value>,
442445
run: Arc<F>,
443446
) -> crate::Result<()> {
444447
let child = run(self)?;
@@ -503,7 +506,7 @@ impl Rust {
503506

504507
if !ignore_matcher.is_ignore(&event_path, event_path.is_dir()) {
505508
if is_configuration_file(self.app_settings.target, &event_path) {
506-
match reload_config(config.as_deref()) {
509+
match reload_config(config.as_ref()) {
507510
Ok(config) => {
508511
info!("Tauri configuration changed. Rewriting manifest...");
509512
*self.app_settings.manifest.lock().unwrap() =

0 commit comments

Comments
 (0)