Skip to content

Commit

Permalink
fix(cli): regression on --config not accepting file paths (#8783)
Browse files Browse the repository at this point in the history
* fix(cli): regression on --config not accepting file paths

* enhance dev server config parsing

* use serde_json::json!

* pass config to setup
  • Loading branch information
lucasfernog authored Feb 7, 2024
1 parent 8751c32 commit fb0d997
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 139 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-config-arg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:bug
"@tauri-apps/cli": patch:bug
---

Fixes a regression on the `--config` argument not accepting file paths.
16 changes: 8 additions & 8 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 9 additions & 17 deletions tooling/cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ use crate::{
helpers::{
app_paths::{app_dir, tauri_dir},
command_env,
config::{get as get_config, FrontendDist, HookCommand, MERGE_CONFIG_EXTENSION_NAME},
resolve_merge_config,
config::{get as get_config, ConfigHandle, FrontendDist, HookCommand},
updater_signature::{secret_key as updater_secret_key, sign_file},
},
interface::{AppInterface, AppSettings, Interface},
CommandExt, Result,
CommandExt, ConfigValue, Result,
};
use anyhow::{bail, Context};
use base64::Engine;
Expand Down Expand Up @@ -57,7 +56,7 @@ pub struct Options {
pub bundles: Option<Vec<String>>,
/// JSON string or path to JSON file to merge with tauri.conf.json
#[clap(short, long)]
pub config: Option<String>,
pub config: Option<ConfigValue>,
/// Command line arguments passed to the runner. Use `--` to explicitly mark the start of the arguments.
pub args: Vec<String>,
/// Skip prompting for values
Expand All @@ -75,14 +74,14 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
.map(Target::from_triple)
.unwrap_or_else(Target::current);

let config = get_config(target, options.config.as_deref())?;
let config = get_config(target, options.config.as_ref().map(|c| &c.0))?;

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

setup(target, &interface, &mut options, false)?;
setup(&interface, &mut options, config.clone(), false)?;

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

pub fn setup(
target: Target,
interface: &AppInterface,
options: &mut Options,
config: ConfigHandle,
mobile: bool,
) -> Result<()> {
let (merge_config, merge_config_path) = resolve_merge_config(&options.config)?;
options.config = merge_config;

let config = get_config(target, options.config.as_deref())?;

let tauri_path = tauri_dir();
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;

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

let bundle_identifier_source = match config_.find_bundle_identifier_overwriter() {
Some(source) if source == MERGE_CONFIG_EXTENSION_NAME => merge_config_path.unwrap_or(source),
Some(source) => source,
None => "tauri.conf.json".into(),
};
let bundle_identifier_source = config_
.find_bundle_identifier_overwriter()
.unwrap_or_else(|| "tauri.conf.json".into());

if config_.identifier == "com.tauri.dev" {
error!(
Expand Down
43 changes: 25 additions & 18 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use crate::{
helpers::{
app_paths::{app_dir, tauri_dir},
command_env,
config::{get as get_config, reload as reload_config, BeforeDevCommand, FrontendDist},
resolve_merge_config,
config::{
get as get_config, reload as reload_config, BeforeDevCommand, ConfigHandle, FrontendDist,
},
},
interface::{AppInterface, DevProcess, ExitReason, Interface},
CommandExt, Result,
CommandExt, ConfigValue, Result,
};

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

let config = get_config(target, options.config.as_deref())?;
let config = get_config(target, options.config.as_ref().map(|c| &c.0))?;

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

setup(target, &interface, &mut options, false)?;
setup(&interface, &mut options, config, false)?;

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

pub fn setup(
target: Target,
interface: &AppInterface,
options: &mut Options,
config: ConfigHandle,
mobile: bool,
) -> Result<()> {
let (merge_config, _merge_config_path) = resolve_merge_config(&options.config)?;
options.config = merge_config;

let config = get_config(target, options.config.as_deref())?;

let tauri_path = tauri_dir();
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;

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

if let Some(c) = &options.config {
let mut c: tauri_utils::config::Config = serde_json::from_str(c)?;
c.build.dev_url = dev_url.clone();
options.config = Some(serde_json::to_string(&c).unwrap());
if let Some(c) = &mut options.config {
if let Some(build) = c
.0
.as_object_mut()
.and_then(|root| root.get_mut("build"))
.and_then(|build| build.as_object_mut())
{
build.insert("devUrl".into(), server_url.into());
}
} else {
options.config = Some(format!(r#"{{ "build": {{ "devUrl": "{server_url}" }} }}"#))
options
.config
.replace(crate::ConfigValue(serde_json::json!({
"build": {
"devUrl": server_url
}
})));
}

reload_config(options.config.as_deref())?;
reload_config(options.config.as_ref().map(|c| &c.0))?;
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions tooling/cli/src/helpers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use anyhow::Context;
use json_patch::merge;
use log::error;
use serde_json::Value as JsonValue;
Expand Down Expand Up @@ -118,7 +117,7 @@ fn config_handle() -> &'static ConfigHandle {

/// Gets the static parsed config from `tauri.conf.json`.
fn get_internal(
merge_config: Option<&str>,
merge_config: Option<&serde_json::Value>,
reload: bool,
target: Target,
) -> crate::Result<ConfigHandle> {
Expand All @@ -143,11 +142,10 @@ fn get_internal(
}

if let Some(merge_config) = merge_config {
set_var("TAURI_CONFIG", merge_config);
let merge_config: JsonValue =
serde_json::from_str(merge_config).with_context(|| "failed to parse config to merge")?;
merge(&mut config, &merge_config);
extensions.insert(MERGE_CONFIG_EXTENSION_NAME.into(), merge_config);
let merge_config_str = serde_json::to_string(&merge_config).unwrap();
set_var("TAURI_CONFIG", merge_config_str);
merge(&mut config, merge_config);
extensions.insert(MERGE_CONFIG_EXTENSION_NAME.into(), merge_config.clone());
};

if config_path.extension() == Some(OsStr::new("json"))
Expand Down Expand Up @@ -198,11 +196,14 @@ fn get_internal(
Ok(config_handle().clone())
}

pub fn get(target: Target, merge_config: Option<&str>) -> crate::Result<ConfigHandle> {
pub fn get(
target: Target,
merge_config: Option<&serde_json::Value>,
) -> crate::Result<ConfigHandle> {
get_internal(merge_config, false, target)
}

pub fn reload(merge_config: Option<&str>) -> crate::Result<ConfigHandle> {
pub fn reload(merge_config: Option<&serde_json::Value>) -> crate::Result<ConfigHandle> {
let target = config_handle()
.lock()
.unwrap()
Expand Down
15 changes: 0 additions & 15 deletions tooling/cli/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ pub mod template;
pub mod updater_signature;
pub mod web_dev_server;

use anyhow::Context;

use std::{
collections::HashMap,
path::{Path, PathBuf},
Expand Down Expand Up @@ -54,16 +52,3 @@ pub fn cross_command(bin: &str) -> Command {
let cmd = Command::new(bin);
cmd
}

pub fn resolve_merge_config(
config: &Option<String>,
) -> crate::Result<(Option<String>, Option<String>)> {
match config {
Some(config) if config.starts_with('{') => Ok((Some(config.to_string()), None)),
Some(config) => Ok((
Some(std::fs::read_to_string(config).with_context(|| "failed to read custom configuration")?),
Some(config.clone()),
)),
None => Ok((None, None)),
}
}
21 changes: 12 additions & 9 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ use tauri_bundler::{
use tauri_utils::config::{parse::is_configuration_file, DeepLinkProtocol};

use super::{AppSettings, DevProcess, ExitReason, Interface};
use crate::helpers::{
app_paths::{app_dir, tauri_dir},
config::{nsis_settings, reload as reload_config, wix_settings, BundleResources, Config},
use crate::{
helpers::{
app_paths::{app_dir, tauri_dir},
config::{nsis_settings, reload as reload_config, wix_settings, BundleResources, Config},
},
ConfigValue,
};
use tauri_utils::{display_path, platform::Target};

Expand All @@ -48,7 +51,7 @@ pub struct Options {
pub target: Option<String>,
pub features: Option<Vec<String>>,
pub args: Vec<String>,
pub config: Option<String>,
pub config: Option<ConfigValue>,
pub no_watch: bool,
}

Expand Down Expand Up @@ -85,7 +88,7 @@ pub struct MobileOptions {
pub debug: bool,
pub features: Option<Vec<String>>,
pub args: Vec<String>,
pub config: Option<String>,
pub config: Option<ConfigValue>,
pub no_watch: bool,
}

Expand Down Expand Up @@ -186,7 +189,7 @@ impl Interface for Rust {
rx.recv().unwrap();
Ok(())
} else {
let config = options.config.clone();
let config = options.config.clone().map(|c| c.0);
let run = Arc::new(|rust: &mut Rust| {
let on_exit = on_exit.clone();
rust.run_dev(options.clone(), run_args.clone(), move |status, reason| {
Expand Down Expand Up @@ -215,7 +218,7 @@ impl Interface for Rust {
runner(options)?;
Ok(())
} else {
let config = options.config.clone();
let config = options.config.clone().map(|c| c.0);
let run = Arc::new(|_rust: &mut Rust| runner(options.clone()));
self.run_dev_watcher(config, run)
}
Expand Down Expand Up @@ -438,7 +441,7 @@ impl Rust {

fn run_dev_watcher<F: Fn(&mut Rust) -> crate::Result<Box<dyn DevProcess + Send>>>(
&mut self,
config: Option<String>,
config: Option<serde_json::Value>,
run: Arc<F>,
) -> crate::Result<()> {
let child = run(self)?;
Expand Down Expand Up @@ -503,7 +506,7 @@ impl Rust {

if !ignore_matcher.is_ignore(&event_path, event_path.is_dir()) {
if is_configuration_file(self.app_settings.target, &event_path) {
match reload_config(config.as_deref()) {
match reload_config(config.as_ref()) {
Ok(config) => {
info!("Tauri configuration changed. Rewriting manifest...");
*self.app_settings.manifest.lock().unwrap() =
Expand Down
Loading

0 comments on commit fb0d997

Please sign in to comment.