From 53cb526521721f818cac8f2dc45b60b3fc7ed654 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 13 Aug 2024 16:15:21 -0300 Subject: [PATCH 1/5] fix(cli): builtin dev server should also be forwarded for Android This changes the CLI to persist the "merge config" across mobile command usage (required to pull this config on the xcode and android studio build scripts). This is required because the builtin dev server makes modifications to the Tauri config (devUrl rewrite) and we need to detect that to forward the dev URL port to Android using ADB. Also adds a warning on iOS when using `dev --open`: you need the `--host` option to run on physical devices. --- .changes/fix-cli-dev-server-android.md | 6 ++++++ tooling/cli/src/helpers/config.rs | 17 +++++++++++++++++ tooling/cli/src/lib.rs | 4 ++-- .../src/mobile/android/android_studio_script.rs | 6 ++++++ tooling/cli/src/mobile/android/build.rs | 1 + tooling/cli/src/mobile/android/dev.rs | 3 ++- tooling/cli/src/mobile/ios/build.rs | 1 + tooling/cli/src/mobile/ios/dev.rs | 13 ++++++++++++- tooling/cli/src/mobile/mod.rs | 3 +++ 9 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 .changes/fix-cli-dev-server-android.md diff --git a/.changes/fix-cli-dev-server-android.md b/.changes/fix-cli-dev-server-android.md new file mode 100644 index 000000000000..f7b17db546b5 --- /dev/null +++ b/.changes/fix-cli-dev-server-android.md @@ -0,0 +1,6 @@ +--- +"tauri-cli": patch:bug +"@tauri-apps/cli": patch:bug +--- + +Fixes `android dev` not working when using the builtin dev server. diff --git a/tooling/cli/src/helpers/config.rs b/tooling/cli/src/helpers/config.rs index 414977cb4995..52a4585b5031 100644 --- a/tooling/cli/src/helpers/config.rs +++ b/tooling/cli/src/helpers/config.rs @@ -215,3 +215,20 @@ pub fn reload(merge_config: Option<&serde_json::Value>) -> crate::Result crate::Result { + let handle = config_handle(); + if let Some(config_metadata) = &mut *handle.lock().unwrap() { + let merge_config_str = serde_json::to_string(merge_config).unwrap(); + set_var("TAURI_CONFIG", merge_config_str); + + let mut value = serde_json::to_value(config_metadata.inner.clone())?; + merge(&mut value, merge_config); + config_metadata.inner = serde_json::from_value(value)?; + + Ok(handle.clone()) + } else { + Err(anyhow::anyhow!("config not loaded")) + } +} diff --git a/tooling/cli/src/lib.rs b/tooling/cli/src/lib.rs index 88ff4a05a4c0..7fcf9cce3ace 100644 --- a/tooling/cli/src/lib.rs +++ b/tooling/cli/src/lib.rs @@ -34,7 +34,7 @@ use clap::{ArgAction, CommandFactory, FromArgMatches, Parser, Subcommand, ValueE use env_logger::fmt::style::{AnsiColor, Style}; use env_logger::Builder; use log::Level; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::io::{BufReader, Write}; use std::process::{exit, Command, ExitStatus, Output, Stdio}; use std::{ @@ -48,7 +48,7 @@ use std::{ }; /// Tauri configuration argument option. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct ConfigValue(pub(crate) serde_json::Value); impl FromStr for ConfigValue { diff --git a/tooling/cli/src/mobile/android/android_studio_script.rs b/tooling/cli/src/mobile/android/android_studio_script.rs index 76df42fca375..bd6e02797836 100644 --- a/tooling/cli/src/mobile/android/android_studio_script.rs +++ b/tooling/cli/src/mobile/android/android_studio_script.rs @@ -59,6 +59,7 @@ pub fn command(options: Options) -> Result<()> { ); (config, metadata, cli_options) }; + ensure_init( &tauri_config, config.app(), @@ -66,6 +67,10 @@ pub fn command(options: Options) -> Result<()> { MobileTarget::Android, )?; + if let Some(config) = &cli_options.config { + crate::helpers::config::merge_with(&config.0)?; + } + let env = env()?; if cli_options.dev { @@ -77,6 +82,7 @@ pub fn command(options: Options) -> Result<()> { .build .dev_url .clone(); + if let Some(port) = dev_url.and_then(|url| url.port_or_known_default()) { let forward = format!("tcp:{port}"); // ignore errors in case we do not have a device available diff --git a/tooling/cli/src/mobile/android/build.rs b/tooling/cli/src/mobile/android/build.rs index 590ef6536ec6..8962a51dd2c3 100644 --- a/tooling/cli/src/mobile/android/build.rs +++ b/tooling/cli/src/mobile/android/build.rs @@ -201,6 +201,7 @@ fn run_build( args: build_options.args.clone(), noise_level, vars: Default::default(), + config: build_options.config.clone(), }; let handle = write_options( &tauri_config.lock().unwrap().as_ref().unwrap().identifier, diff --git a/tooling/cli/src/mobile/android/dev.rs b/tooling/cli/src/mobile/android/dev.rs index 28eb7f19ce68..6400314ec83a 100644 --- a/tooling/cli/src/mobile/android/dev.rs +++ b/tooling/cli/src/mobile/android/dev.rs @@ -222,7 +222,7 @@ fn run_dev( debug: !options.release_mode, features: options.features, args: Vec::new(), - config: options.config, + config: dev_options.config.clone(), no_watch: options.no_watch, }, |options| { @@ -232,6 +232,7 @@ fn run_dev( args: options.args.clone(), noise_level, vars: Default::default(), + config: dev_options.config.clone(), }; let _handle = write_options( diff --git a/tooling/cli/src/mobile/ios/build.rs b/tooling/cli/src/mobile/ios/build.rs index 175fa5919fec..f53cb9205313 100644 --- a/tooling/cli/src/mobile/ios/build.rs +++ b/tooling/cli/src/mobile/ios/build.rs @@ -283,6 +283,7 @@ fn run_build( args: build_options.args.clone(), noise_level, vars: Default::default(), + config: build_options.config.clone(), }; let handle = write_options( &tauri_config.lock().unwrap().as_ref().unwrap().identifier, diff --git a/tooling/cli/src/mobile/ios/dev.rs b/tooling/cli/src/mobile/ios/dev.rs index cde4b8a7d804..b11637c80e5a 100644 --- a/tooling/cli/src/mobile/ios/dev.rs +++ b/tooling/cli/src/mobile/ios/dev.rs @@ -36,6 +36,8 @@ use std::{ sync::OnceLock, }; +const PHYSICAL_IPHONE_DEV_WARNING: &str = "To develop on physical phones you need the `--host` option (not required for Simulators). See the documentation for more information: https://v2.tauri.app/develop/#development-server"; + #[derive(Debug, Clone, Parser)] #[clap( about = "Run your app in development mode on iOS", @@ -367,6 +369,8 @@ fn run_dev( let out_dir = bin_path.parent().unwrap(); let _lock = flock::open_rw(out_dir.join("lock").with_extension("ios"), "iOS")?; + let set_host = options.host.is_some(); + configure_cargo(app, None)?; let open = options.open; @@ -377,7 +381,7 @@ fn run_dev( debug: true, features: options.features, args: Vec::new(), - config: options.config, + config: dev_options.config.clone(), no_watch: options.no_watch, }, |options| { @@ -387,6 +391,7 @@ fn run_dev( args: options.args.clone(), noise_level, vars: Default::default(), + config: dev_options.config.clone(), }; let _handle = write_options( &tauri_config.lock().unwrap().as_ref().unwrap().identifier, @@ -394,6 +399,9 @@ fn run_dev( )?; if open { + if !set_host { + log::warn!("{PHYSICAL_IPHONE_DEV_WARNING}"); + } open_and_wait(config, &env) } else if let Some(device) = &device { match run(device, options, config, &env) { @@ -409,6 +417,9 @@ fn run_dev( } } } else { + if !set_host { + log::warn!("{PHYSICAL_IPHONE_DEV_WARNING}"); + } open_and_wait(config, &env) } }, diff --git a/tooling/cli/src/mobile/mod.rs b/tooling/cli/src/mobile/mod.rs index bf4788498e26..b1c746e51cd1 100644 --- a/tooling/cli/src/mobile/mod.rs +++ b/tooling/cli/src/mobile/mod.rs @@ -8,6 +8,7 @@ use crate::{ config::{Config as TauriConfig, ConfigHandle}, }, interface::{AppInterface, AppSettings, DevProcess, Interface, Options as InterfaceOptions}, + ConfigValue, }; #[cfg(target_os = "macos")] use anyhow::Context; @@ -141,6 +142,7 @@ pub struct CliOptions { pub args: Vec, pub noise_level: NoiseLevel, pub vars: HashMap, + pub config: Option, } impl Default for CliOptions { @@ -151,6 +153,7 @@ impl Default for CliOptions { args: vec!["--lib".into()], noise_level: Default::default(), vars: Default::default(), + config: None, } } } From 888459aab30c4e70db59347d0e362d5320b999dc Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 13 Aug 2024 16:31:32 -0300 Subject: [PATCH 2/5] more logs on port forwarding --- .../permissions/autogenerated/reference.md | 2 +- tooling/cli/src/mobile/android/android_studio_script.rs | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/api/src-tauri/tauri-plugin-sample/permissions/autogenerated/reference.md b/examples/api/src-tauri/tauri-plugin-sample/permissions/autogenerated/reference.md index 4ca87ac5a329..b6faa8c8cd9e 100644 --- a/examples/api/src-tauri/tauri-plugin-sample/permissions/autogenerated/reference.md +++ b/examples/api/src-tauri/tauri-plugin-sample/permissions/autogenerated/reference.md @@ -1,5 +1,5 @@ -### Permission Table +## Permission Table diff --git a/tooling/cli/src/mobile/android/android_studio_script.rs b/tooling/cli/src/mobile/android/android_studio_script.rs index bd6e02797836..b9ff6b35368c 100644 --- a/tooling/cli/src/mobile/android/android_studio_script.rs +++ b/tooling/cli/src/mobile/android/android_studio_script.rs @@ -85,12 +85,16 @@ pub fn command(options: Options) -> Result<()> { if let Some(port) = dev_url.and_then(|url| url.port_or_known_default()) { let forward = format!("tcp:{port}"); + log::info!("Forwarding port {port} with adb"); // ignore errors in case we do not have a device available - let _ = adb::adb(&env, ["reverse", &forward, &forward]) + let result = adb::adb(&env, ["reverse", &forward, &forward]) .stdin_file(os_pipe::dup_stdin().unwrap()) .stdout_file(os_pipe::dup_stdout().unwrap()) .stderr_capture() .run(); + if let Err(e) = result { + log::error!("Failed to forward port {port}: {e}"); + } } } From ddd957f31faa5572c60b1161f9a1bffbeb417684 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 13 Aug 2024 16:33:30 -0300 Subject: [PATCH 3/5] print adb reverse stderr --- tooling/cli/src/mobile/android/android_studio_script.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/cli/src/mobile/android/android_studio_script.rs b/tooling/cli/src/mobile/android/android_studio_script.rs index b9ff6b35368c..94fb7b327a8a 100644 --- a/tooling/cli/src/mobile/android/android_studio_script.rs +++ b/tooling/cli/src/mobile/android/android_studio_script.rs @@ -90,7 +90,7 @@ pub fn command(options: Options) -> Result<()> { let result = adb::adb(&env, ["reverse", &forward, &forward]) .stdin_file(os_pipe::dup_stdin().unwrap()) .stdout_file(os_pipe::dup_stdout().unwrap()) - .stderr_capture() + .stderr_file(os_pipe::dup_stdout().unwrap()) .run(); if let Err(e) = result { log::error!("Failed to forward port {port}: {e}"); From bd832bf4e110f65d1108dec0aaca2ee1123921ee Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 13 Aug 2024 16:42:12 -0300 Subject: [PATCH 4/5] Revert "more logs on port forwarding" This reverts commit 888459aab30c4e70db59347d0e362d5320b999dc. --- .../permissions/autogenerated/reference.md | 2 +- tooling/cli/src/mobile/android/android_studio_script.rs | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/api/src-tauri/tauri-plugin-sample/permissions/autogenerated/reference.md b/examples/api/src-tauri/tauri-plugin-sample/permissions/autogenerated/reference.md index b6faa8c8cd9e..4ca87ac5a329 100644 --- a/examples/api/src-tauri/tauri-plugin-sample/permissions/autogenerated/reference.md +++ b/examples/api/src-tauri/tauri-plugin-sample/permissions/autogenerated/reference.md @@ -1,5 +1,5 @@ -## Permission Table +### Permission Table
diff --git a/tooling/cli/src/mobile/android/android_studio_script.rs b/tooling/cli/src/mobile/android/android_studio_script.rs index 94fb7b327a8a..3831a4ab8334 100644 --- a/tooling/cli/src/mobile/android/android_studio_script.rs +++ b/tooling/cli/src/mobile/android/android_studio_script.rs @@ -85,16 +85,12 @@ pub fn command(options: Options) -> Result<()> { if let Some(port) = dev_url.and_then(|url| url.port_or_known_default()) { let forward = format!("tcp:{port}"); - log::info!("Forwarding port {port} with adb"); // ignore errors in case we do not have a device available - let result = adb::adb(&env, ["reverse", &forward, &forward]) + let _ = adb::adb(&env, ["reverse", &forward, &forward]) .stdin_file(os_pipe::dup_stdin().unwrap()) .stdout_file(os_pipe::dup_stdout().unwrap()) .stderr_file(os_pipe::dup_stdout().unwrap()) .run(); - if let Err(e) = result { - log::error!("Failed to forward port {port}: {e}"); - } } } From 6f050b4159b3600e413ddc1bd3c57845395b07fd Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 13 Aug 2024 16:42:17 -0300 Subject: [PATCH 5/5] Revert "print adb reverse stderr" This reverts commit ddd957f31faa5572c60b1161f9a1bffbeb417684. --- tooling/cli/src/mobile/android/android_studio_script.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/cli/src/mobile/android/android_studio_script.rs b/tooling/cli/src/mobile/android/android_studio_script.rs index 3831a4ab8334..bd6e02797836 100644 --- a/tooling/cli/src/mobile/android/android_studio_script.rs +++ b/tooling/cli/src/mobile/android/android_studio_script.rs @@ -89,7 +89,7 @@ pub fn command(options: Options) -> Result<()> { let _ = adb::adb(&env, ["reverse", &forward, &forward]) .stdin_file(os_pipe::dup_stdin().unwrap()) .stdout_file(os_pipe::dup_stdout().unwrap()) - .stderr_file(os_pipe::dup_stdout().unwrap()) + .stderr_capture() .run(); } }