Skip to content

Commit ec6065f

Browse files
authored
fix(cli): use original identifier to fix mobile options reading (#13625)
* fix(cli): read original identifier to fix mobile options reading the iOS and Android CLI commands leverage an android_studio_script/xcode_script that is executed by the native IDE or build tool. This script reads the Tauri configuration to find the app identifier used to communicate with the parent Tauri CLI process to read CLI options. The communication is broken when the `--config` arg is used, since the IDE script does not have access to that value before reaching the parent process, which is impossible without knowing the actual identifier used. To bypass this we'll agree on using the original identifier. This obviously won't work if the original tauri.conf.json do not have an identifier, so we error out in this case * fix build, lint
1 parent 18b5299 commit ec6065f

File tree

9 files changed

+45
-28
lines changed

9 files changed

+45
-28
lines changed
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 Android and iOS dev/build commands not working when the app identifier is being modified by the `--config` option.

crates/tauri-cli/src/helpers/config.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub const MERGE_CONFIG_EXTENSION_NAME: &str = "--config";
2222
pub struct ConfigMetadata {
2323
/// The current target.
2424
target: Target,
25+
26+
original_identifier: Option<String>,
2527
/// The actual configuration, merged with any extension.
2628
inner: Config,
2729
/// The config extensions (platform-specific config files or the config CLI argument).
@@ -39,15 +41,17 @@ impl std::ops::Deref for ConfigMetadata {
3941
}
4042

4143
impl ConfigMetadata {
44+
/// The original bundle identifier from the config file.
45+
/// This does not take any extensions into account.
46+
pub fn original_identifier(&self) -> Option<&str> {
47+
self.original_identifier.as_deref()
48+
}
49+
4250
/// Checks which config is overwriting the bundle identifier.
4351
pub fn find_bundle_identifier_overwriter(&self) -> Option<String> {
4452
for (ext, config) in &self.extensions {
4553
if let Some(identifier) = config
4654
.as_object()
47-
.and_then(|config| config.get("tauri"))
48-
.and_then(|tauri_config| tauri_config.as_object())
49-
.and_then(|tauri_config| tauri_config.get("bundle"))
50-
.and_then(|bundle_config| bundle_config.as_object())
5155
.and_then(|bundle_config| bundle_config.get("identifier"))
5256
.and_then(|id| id.as_str())
5357
{
@@ -153,6 +157,12 @@ fn get_internal(
153157
let config_file_name = config_path.file_name().unwrap().to_string_lossy();
154158
let mut extensions = HashMap::new();
155159

160+
let original_identifier = config
161+
.as_object()
162+
.and_then(|config| config.get("identifier"))
163+
.and_then(|id| id.as_str())
164+
.map(ToString::to_string);
165+
156166
if let Some((platform_config, config_path)) =
157167
tauri_utils::config::parse::read_platform(target, tauri_dir)?
158168
{
@@ -220,6 +230,7 @@ fn get_internal(
220230

221231
*config_handle().lock().unwrap() = Some(ConfigMetadata {
222232
target,
233+
original_identifier,
223234
inner: config,
224235
extensions,
225236
});

crates/tauri-cli/src/mobile/android/android_studio_script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn command(options: Options) -> Result<()> {
5151
let (config, metadata, cli_options) = {
5252
let tauri_config_guard = tauri_config.lock().unwrap();
5353
let tauri_config_ = tauri_config_guard.as_ref().unwrap();
54-
let cli_options = read_options(&tauri_config_.identifier);
54+
let cli_options = read_options(tauri_config_);
5555
let (config, metadata) = get_config(
5656
&get_app(
5757
MobileTarget::Android,

crates/tauri-cli/src/mobile/android/build.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,7 @@ fn run_build(
213213
config: build_options.config,
214214
target_device: None,
215215
};
216-
let handle = write_options(
217-
&tauri_config.lock().unwrap().as_ref().unwrap().identifier,
218-
cli_options,
219-
)?;
216+
let handle = write_options(tauri_config.lock().unwrap().as_ref().unwrap(), cli_options)?;
220217

221218
inject_resources(config, tauri_config.lock().unwrap().as_ref().unwrap())?;
222219

crates/tauri-cli/src/mobile/android/dev.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,7 @@ fn run_dev(
275275
}),
276276
};
277277

278-
let _handle = write_options(
279-
&tauri_config.lock().unwrap().as_ref().unwrap().identifier,
280-
cli_options,
281-
)?;
278+
let _handle = write_options(tauri_config.lock().unwrap().as_ref().unwrap(), cli_options)?;
282279

283280
inject_resources(config, tauri_config.lock().unwrap().as_ref().unwrap())?;
284281

crates/tauri-cli/src/mobile/ios/build.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,7 @@ fn run_build(
295295
config: build_options.config.clone(),
296296
target_device: None,
297297
};
298-
let handle = write_options(
299-
&tauri_config.lock().unwrap().as_ref().unwrap().identifier,
300-
cli_options,
301-
)?;
298+
let handle = write_options(tauri_config.lock().unwrap().as_ref().unwrap(), cli_options)?;
302299

303300
if options.open {
304301
return Ok(handle);

crates/tauri-cli/src/mobile/ios/dev.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,7 @@ fn run_dev(
279279
config: dev_options.config.clone(),
280280
target_device: None,
281281
};
282-
let _handle = write_options(
283-
&tauri_config.lock().unwrap().as_ref().unwrap().identifier,
284-
cli_options,
285-
)?;
282+
let _handle = write_options(tauri_config.lock().unwrap().as_ref().unwrap(), cli_options)?;
286283

287284
let open_xcode = || {
288285
if !set_host {

crates/tauri-cli/src/mobile/ios/xcode_script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn command(options: Options) -> Result<()> {
8787
let (config, metadata, cli_options) = {
8888
let tauri_config_guard = tauri_config.lock().unwrap();
8989
let tauri_config_ = tauri_config_guard.as_ref().unwrap();
90-
let cli_options = read_options(&tauri_config_.identifier);
90+
let cli_options = read_options(tauri_config_);
9191
let (config, metadata) = get_config(
9292
&get_app(
9393
MobileTarget::Ios,

crates/tauri-cli/src/mobile/mod.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
use crate::{
66
helpers::{
77
app_paths::tauri_dir,
8-
config::{reload as reload_config, Config as TauriConfig, ConfigHandle},
8+
config::{reload as reload_config, Config as TauriConfig, ConfigHandle, ConfigMetadata},
99
},
1010
interface::{AppInterface, AppSettings, DevProcess, Interface, Options as InterfaceOptions},
1111
ConfigValue,
1212
};
13-
#[cfg(unix)]
1413
use anyhow::Context;
1514
use anyhow::{bail, Result};
1615
use heck::ToSnekCase;
@@ -365,7 +364,10 @@ fn env() -> Result<Env, EnvError> {
365364
pub struct OptionsHandle(#[allow(unused)] Runtime, #[allow(unused)] ServerHandle);
366365

367366
/// Writes CLI options to be used later on the Xcode and Android Studio build commands
368-
pub fn write_options(identifier: &str, mut options: CliOptions) -> crate::Result<OptionsHandle> {
367+
pub fn write_options(
368+
config: &ConfigMetadata,
369+
mut options: CliOptions,
370+
) -> crate::Result<OptionsHandle> {
369371
options.vars.extend(env_vars());
370372

371373
let runtime = Runtime::new().unwrap();
@@ -383,18 +385,28 @@ pub fn write_options(identifier: &str, mut options: CliOptions) -> crate::Result
383385
let (handle, addr) = r?;
384386

385387
write(
386-
temp_dir().join(format!("{identifier}-server-addr")),
388+
temp_dir().join(format!(
389+
"{}-server-addr",
390+
config
391+
.original_identifier()
392+
.context("app configuration is missing an identifier")?
393+
)),
387394
addr.to_string(),
388395
)?;
389396

390397
Ok(OptionsHandle(runtime, handle))
391398
}
392399

393-
fn read_options(identifier: &str) -> CliOptions {
400+
fn read_options(config: &ConfigMetadata) -> CliOptions {
394401
let runtime = tokio::runtime::Runtime::new().unwrap();
395402
let options = runtime
396403
.block_on(async move {
397-
let addr_path = temp_dir().join(format!("{identifier}-server-addr"));
404+
let addr_path = temp_dir().join(format!(
405+
"{}-server-addr",
406+
config
407+
.original_identifier()
408+
.context("app configuration is missing an identifier")?
409+
));
398410
let (tx, rx) = WsTransportClientBuilder::default()
399411
.build(
400412
format!(

0 commit comments

Comments
 (0)