Skip to content

Commit 60e86d5

Browse files
authored
fix(cli): android dev not working on Windows without --host (#11624)
ref https://discord.com/channels/616186924390023171/1291159454397628477
1 parent b284358 commit 60e86d5

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
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+
Use the public network IP address on `android dev` by default on Windows.

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use crate::{
1515
},
1616
interface::{AppInterface, Interface, MobileOptions, Options as InterfaceOptions},
1717
mobile::{
18-
use_network_address_for_dev_url, write_options, CliOptions, DevChild, DevProcess, TargetDevice,
18+
use_network_address_for_dev_url, write_options, CliOptions, DevChild, DevHost, DevProcess,
19+
TargetDevice,
1920
},
2021
ConfigValue, Result,
2122
};
@@ -33,7 +34,7 @@ use cargo_mobile2::{
3334
target::TargetTrait,
3435
};
3536

36-
use std::{env::set_current_dir, net::IpAddr};
37+
use std::env::set_current_dir;
3738

3839
#[derive(Debug, Clone, Parser)]
3940
#[clap(
@@ -70,6 +71,8 @@ pub struct Options {
7071
/// Use the public network address for the development server.
7172
/// If an actual address it provided, it is used instead of prompting to pick one.
7273
///
74+
/// On Windows we use the public network address by default.
75+
///
7376
/// This option is particularly useful along the `--open` flag when you intend on running on a physical device.
7477
///
7578
/// This replaces the devUrl configuration value to match the public network address host,
@@ -79,8 +82,8 @@ pub struct Options {
7982
/// When this is set or when running on an iOS device the CLI sets the `TAURI_DEV_HOST`
8083
/// environment variable so you can check this on your framework's configuration to expose the development server
8184
/// on the public network address.
82-
#[clap(long)]
83-
pub host: Option<Option<IpAddr>>,
85+
#[clap(long, default_value_t, default_missing_value(""), num_args(0..=1))]
86+
pub host: DevHost,
8487
/// Disable the built-in dev server for static files.
8588
#[clap(long)]
8689
pub no_dev_server: bool,
@@ -103,7 +106,7 @@ impl From<Options> for DevOptions {
103106
no_dev_server: options.no_dev_server,
104107
port: options.port,
105108
release_mode: options.release_mode,
106-
host: None,
109+
host: options.host.0.unwrap_or_default(),
107110
}
108111
}
109112
}
@@ -197,7 +200,7 @@ fn run_dev(
197200
noise_level: NoiseLevel,
198201
) -> Result<()> {
199202
// when running on an actual device we must use the network IP
200-
if options.host.is_some()
203+
if options.host.0.is_some()
201204
|| device
202205
.as_ref()
203206
.map(|device| !device.serial_no().starts_with("emulator"))

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use crate::{
1414
flock,
1515
},
1616
interface::{AppInterface, Interface, MobileOptions, Options as InterfaceOptions},
17-
mobile::{use_network_address_for_dev_url, write_options, CliOptions, DevChild, DevProcess},
17+
mobile::{
18+
use_network_address_for_dev_url, write_options, CliOptions, DevChild, DevHost, DevProcess,
19+
},
1820
ConfigValue, Result,
1921
};
2022
use clap::{ArgAction, Parser};
@@ -29,7 +31,7 @@ use cargo_mobile2::{
2931
opts::{NoiseLevel, Profile},
3032
};
3133

32-
use std::{env::set_current_dir, net::IpAddr};
34+
use std::env::set_current_dir;
3335

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

@@ -84,8 +86,8 @@ pub struct Options {
8486
/// When this is set or when running on an iOS device the CLI sets the `TAURI_DEV_HOST`
8587
/// environment variable so you can check this on your framework's configuration to expose the development server
8688
/// on the public network address.
87-
#[clap(long)]
88-
pub host: Option<Option<IpAddr>>,
89+
#[clap(long, default_value_t, default_missing_value(""), num_args(0..=1))]
90+
pub host: DevHost,
8991
/// Disable the built-in dev server for static files.
9092
#[clap(long)]
9193
pub no_dev_server: bool,
@@ -108,7 +110,7 @@ impl From<Options> for DevOptions {
108110
no_dev_server: options.no_dev_server,
109111
no_dev_server_wait: options.no_dev_server_wait,
110112
port: options.port,
111-
host: None,
113+
host: options.host.0.unwrap_or_default(),
112114
}
113115
}
114116
}
@@ -230,7 +232,7 @@ fn run_dev(
230232
noise_level: NoiseLevel,
231233
) -> Result<()> {
232234
// when running on an actual device we must use the network IP
233-
if options.host.is_some()
235+
if options.host.0.is_some()
234236
|| device
235237
.as_ref()
236238
.map(|device| !matches!(device.kind(), DeviceKind::Simulator))
@@ -249,7 +251,7 @@ fn run_dev(
249251
})?;
250252
let _lock = flock::open_rw(out_dir.join("lock").with_extension("ios"), "iOS")?;
251253

252-
let set_host = options.host.is_some();
254+
let set_host = options.host.0.is_some();
253255

254256
let open = options.open;
255257
interface.mobile_dev(

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ use std::{
3030
collections::HashMap,
3131
env::{set_var, temp_dir},
3232
ffi::OsString,
33-
fmt::Write,
33+
fmt::{Display, Write},
3434
fs::{read_to_string, write},
35-
net::{IpAddr, Ipv4Addr, SocketAddr},
35+
net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr},
3636
path::PathBuf,
3737
process::{exit, ExitStatus},
38+
str::FromStr,
3839
sync::{
3940
atomic::{AtomicBool, Ordering},
4041
Arc, OnceLock,
@@ -141,6 +142,44 @@ pub struct TargetDevice {
141142
name: String,
142143
}
143144

145+
#[derive(Debug, Clone)]
146+
pub struct DevHost(Option<Option<IpAddr>>);
147+
148+
impl FromStr for DevHost {
149+
type Err = AddrParseError;
150+
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
151+
if s.is_empty() || s == "<public network address>" {
152+
Ok(Self(Some(None)))
153+
} else if s == "<none>" {
154+
Ok(Self(None))
155+
} else {
156+
IpAddr::from_str(s).map(|addr| Self(Some(Some(addr))))
157+
}
158+
}
159+
}
160+
161+
impl Display for DevHost {
162+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163+
match self.0 {
164+
Some(None) => write!(f, "<public network address>"),
165+
Some(Some(addr)) => write!(f, "{addr}"),
166+
None => write!(f, "<none>"),
167+
}
168+
}
169+
}
170+
171+
impl Default for DevHost {
172+
fn default() -> Self {
173+
// on Windows we want to force using the public network address for the development server
174+
// because the adb port forwarding does not work well
175+
if cfg!(windows) {
176+
Self(Some(None))
177+
} else {
178+
Self(None)
179+
}
180+
}
181+
}
182+
144183
#[derive(Debug, Clone, Serialize, Deserialize)]
145184
pub struct CliOptions {
146185
pub dev: bool,

0 commit comments

Comments
 (0)