fix(cli): resolve IP when dev URL host is unspecified, closes #13356 (#14115)

currently the `use_network_address_for_dev_url` function already detects Ipv4Addr::UNSPECIFIED to resolve the local IP address for mobile development when the dev URL host is 0.0.0.0, but we only call it when `--host` is provided or running on a physical device. This change detects the unspecified host early and force the resolution to run even for simulator builds
This commit is contained in:
Lucas Fernandes Nogueira
2025-10-06 13:11:48 -03:00
committed by GitHub
parent 06d4a4ed6c
commit b0012424c5
4 changed files with 41 additions and 7 deletions

View File

@@ -0,0 +1,6 @@
---
"@tauri-apps/cli": patch:bug
"tauri-cli": patch:bug
---
Resolve local IP address when `tauri.conf.json > build > devUrl` host is `0.0.0.0`.

View File

@@ -33,8 +33,9 @@ use cargo_mobile2::{
opts::{FilterLevel, NoiseLevel, Profile},
target::TargetTrait,
};
use url::Host;
use std::{env::set_current_dir, path::PathBuf};
use std::{env::set_current_dir, net::Ipv4Addr, path::PathBuf};
#[derive(Debug, Clone, Parser)]
#[clap(
@@ -231,12 +232,26 @@ fn run_dev(
metadata: &AndroidMetadata,
noise_level: NoiseLevel,
) -> Result<()> {
// when running on an actual device we must use the network IP
// when --host is provided or running on a physical device or resolving 0.0.0.0 we must use the network IP
if options.host.0.is_some()
|| device
.as_ref()
.map(|device| !device.serial_no().starts_with("emulator"))
.unwrap_or(false)
|| tauri_config
.lock()
.unwrap()
.as_ref()
.unwrap()
.build
.dev_url
.as_ref()
.is_some_and(|url| {
matches!(
url.host(),
Some(Host::Ipv4(i)) if i == Ipv4Addr::UNSPECIFIED
)
})
{
use_network_address_for_dev_url(&tauri_config, &mut dev_options, options.force_ip_prompt)?;
}

View File

@@ -32,8 +32,9 @@ use cargo_mobile2::{
env::Env,
opts::{NoiseLevel, Profile},
};
use url::Host;
use std::{env::set_current_dir, path::PathBuf};
use std::{env::set_current_dir, net::Ipv4Addr, path::PathBuf};
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";
@@ -271,12 +272,26 @@ fn run_dev(
config: &AppleConfig,
noise_level: NoiseLevel,
) -> Result<()> {
// when running on an actual device we must use the network IP
// when --host is provided or running on a physical device or resolving 0.0.0.0 we must use the network IP
if options.host.0.is_some()
|| device
.as_ref()
.map(|device| !matches!(device.kind(), DeviceKind::Simulator))
.unwrap_or(false)
|| tauri_config
.lock()
.unwrap()
.as_ref()
.unwrap()
.build
.dev_url
.as_ref()
.is_some_and(|url| {
matches!(
url.host(),
Some(Host::Ipv4(i)) if i == Ipv4Addr::UNSPECIFIED
)
})
{
use_network_address_for_dev_url(&tauri_config, &mut dev_options, options.force_ip_prompt)?;
}

View File

@@ -268,9 +268,7 @@ fn use_network_address_for_dev_url(
let ip = if let Some(url) = &mut dev_url {
let localhost = match url.host() {
Some(url::Host::Domain(d)) => d == "localhost",
Some(url::Host::Ipv4(i)) => {
i == std::net::Ipv4Addr::LOCALHOST || i == std::net::Ipv4Addr::UNSPECIFIED
}
Some(url::Host::Ipv4(i)) => i == Ipv4Addr::LOCALHOST || i == Ipv4Addr::UNSPECIFIED,
_ => false,
};