Skip to content

Commit

Permalink
fix(cli): android dev not working on Windows without --host
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Nov 8, 2024
1 parent 229d7f8 commit e84ffc5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changes/use-public-network-ip-windows-dev-android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:bug
"@tauri-apps/cli": patch:bug
---

Use the public network IP address on `android dev` by default on Windows.
15 changes: 9 additions & 6 deletions crates/tauri-cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use crate::{
},
interface::{AppInterface, Interface, MobileOptions, Options as InterfaceOptions},
mobile::{
use_network_address_for_dev_url, write_options, CliOptions, DevChild, DevProcess, TargetDevice,
use_network_address_for_dev_url, write_options, CliOptions, DevChild, DevHost, DevProcess,
TargetDevice,
},
ConfigValue, Result,
};
Expand All @@ -33,7 +34,7 @@ use cargo_mobile2::{
target::TargetTrait,
};

use std::{env::set_current_dir, net::IpAddr};
use std::env::set_current_dir;

#[derive(Debug, Clone, Parser)]
#[clap(
Expand Down Expand Up @@ -70,6 +71,8 @@ pub struct Options {
/// Use the public network address for the development server.
/// If an actual address it provided, it is used instead of prompting to pick one.
///
/// On Windows we use the public network address by default.
///
/// This option is particularly useful along the `--open` flag when you intend on running on a physical device.
///
/// This replaces the devUrl configuration value to match the public network address host,
Expand All @@ -79,8 +82,8 @@ pub struct Options {
/// When this is set or when running on an iOS device the CLI sets the `TAURI_DEV_HOST`
/// environment variable so you can check this on your framework's configuration to expose the development server
/// on the public network address.
#[clap(long)]
pub host: Option<Option<IpAddr>>,
#[clap(long, default_value_t, default_missing_value(""), num_args(0..=1))]
pub host: DevHost,
/// Disable the built-in dev server for static files.
#[clap(long)]
pub no_dev_server: bool,
Expand All @@ -103,7 +106,7 @@ impl From<Options> for DevOptions {
no_dev_server: options.no_dev_server,
port: options.port,
release_mode: options.release_mode,
host: None,
host: options.host.0.unwrap_or_default(),
}
}
}
Expand Down Expand Up @@ -197,7 +200,7 @@ fn run_dev(
noise_level: NoiseLevel,
) -> Result<()> {
// when running on an actual device we must use the network IP
if options.host.is_some()
if options.host.0.is_some()
|| device
.as_ref()
.map(|device| !device.serial_no().starts_with("emulator"))
Expand Down
16 changes: 9 additions & 7 deletions crates/tauri-cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use crate::{
flock,
},
interface::{AppInterface, Interface, MobileOptions, Options as InterfaceOptions},
mobile::{use_network_address_for_dev_url, write_options, CliOptions, DevChild, DevProcess},
mobile::{
use_network_address_for_dev_url, write_options, CliOptions, DevChild, DevHost, DevProcess,
},
ConfigValue, Result,
};
use clap::{ArgAction, Parser};
Expand All @@ -29,7 +31,7 @@ use cargo_mobile2::{
opts::{NoiseLevel, Profile},
};

use std::{env::set_current_dir, net::IpAddr};
use std::env::set_current_dir;

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";

Expand Down Expand Up @@ -84,8 +86,8 @@ pub struct Options {
/// When this is set or when running on an iOS device the CLI sets the `TAURI_DEV_HOST`
/// environment variable so you can check this on your framework's configuration to expose the development server
/// on the public network address.
#[clap(long)]
pub host: Option<Option<IpAddr>>,
#[clap(long, default_value_t, default_missing_value(""), num_args(0..=1))]
pub host: DevHost,
/// Disable the built-in dev server for static files.
#[clap(long)]
pub no_dev_server: bool,
Expand All @@ -108,7 +110,7 @@ impl From<Options> for DevOptions {
no_dev_server: options.no_dev_server,
no_dev_server_wait: options.no_dev_server_wait,
port: options.port,
host: None,
host: options.host.0.unwrap_or_default(),
}
}
}
Expand Down Expand Up @@ -230,7 +232,7 @@ fn run_dev(
noise_level: NoiseLevel,
) -> Result<()> {
// when running on an actual device we must use the network IP
if options.host.is_some()
if options.host.0.is_some()
|| device
.as_ref()
.map(|device| !matches!(device.kind(), DeviceKind::Simulator))
Expand All @@ -249,7 +251,7 @@ fn run_dev(
})?;
let _lock = flock::open_rw(out_dir.join("lock").with_extension("ios"), "iOS")?;

let set_host = options.host.is_some();
let set_host = options.host.0.is_some();

let open = options.open;
interface.mobile_dev(
Expand Down
43 changes: 41 additions & 2 deletions crates/tauri-cli/src/mobile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ use std::{
collections::HashMap,
env::{set_var, temp_dir},
ffi::OsString,
fmt::Write,
fmt::{Display, Write},
fs::{read_to_string, write},
net::{IpAddr, Ipv4Addr, SocketAddr},
net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr},
path::PathBuf,
process::{exit, ExitStatus},
str::FromStr,
sync::{
atomic::{AtomicBool, Ordering},
Arc, OnceLock,
Expand Down Expand Up @@ -141,6 +142,44 @@ pub struct TargetDevice {
name: String,
}

#[derive(Debug, Clone)]
pub struct DevHost(Option<Option<IpAddr>>);

impl FromStr for DevHost {
type Err = AddrParseError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
if s.is_empty() || s == "<public network address>" {
Ok(Self(Some(None)))
} else if s == "<none>" {
Ok(Self(None))
} else {
IpAddr::from_str(s).map(|addr| Self(Some(Some(addr))))
}
}
}

impl Display for DevHost {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0 {
Some(None) => write!(f, "<public network address>"),
Some(Some(addr)) => write!(f, "{addr}"),
None => write!(f, "<none>"),
}
}
}

impl Default for DevHost {
fn default() -> Self {
// on Windows we want to force using the public network address for the development server
// because the adb port forwarding does not work well
if cfg!(windows) {
Self(Some(None))
} else {
Self(None)
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliOptions {
pub dev: bool,
Expand Down

0 comments on commit e84ffc5

Please sign in to comment.