Skip to content

Commit

Permalink
fix(cli): adb port forward error handling, add logs, closes #9509 (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Aug 14, 2024
1 parent 39bf0a1 commit 9f75d06
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changes/android-port-forward-fixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:bug
"@tauri-apps/cli": patch:bug
---

Fixes `android dev` port forward failing under some conditions, add better logging and error handling.
4 changes: 2 additions & 2 deletions tooling/cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tooling/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ name = "cargo-tauri"
path = "src/main.rs"

[dependencies]
cargo-mobile2 = { version = "0.13.1", default-features = false }
cargo-mobile2 = { version = "0.13.2", default-features = false }
jsonrpsee = { version = "0.24", features = [ "server" ] }
jsonrpsee-core = "0.24"
jsonrpsee-client-transport = { version = "0.24", features = [ "ws" ] }
Expand Down
61 changes: 55 additions & 6 deletions tooling/cli/src/mobile/android/android_studio_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,35 @@ 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}");
// ignore errors in case we do not have a device available
let _ = adb::adb(&env, ["reverse", &forward, &forward])
.stdin_file(os_pipe::dup_stdin().unwrap())
.stdout_file(os_pipe::dup_stdout().unwrap())
.stderr_capture()
.run();
log::info!("Forwarding port {port} with adb");

let devices = adb::device_list(&env).unwrap_or_default();

// clear port forwarding for all devices
for device in &devices {
remove_adb_reverse(&env, device.serial_no(), &forward);
}

// if there's a known target, we should force use it
if let Some(target_device) = &cli_options.target_device {
run_adb_reverse(&env, &target_device.id, &forward, &forward).with_context(|| {
format!(
"failed to forward port with adb, is the {} device connected?",
target_device.name,
)
})?;
} else if devices.len() == 1 {
let device = devices.first().unwrap();
run_adb_reverse(&env, device.serial_no(), &forward, &forward).with_context(|| {
format!(
"failed to forward port with adb, is the {} device connected?",
device.name(),
)
})?;
} else {
anyhow::bail!("Multiple Android devices are connected ({}), please disconnect devices you do not intend to use so Tauri can determine which to use",
devices.iter().map(|d| d.name()).collect::<Vec<_>>().join(", "));
}
}
}

Expand Down Expand Up @@ -152,3 +175,29 @@ fn validate_lib(path: &Path) -> Result<()> {

Ok(())
}

fn run_adb_reverse(
env: &cargo_mobile2::android::env::Env,
device_serial_no: &str,
remote: &str,
local: &str,
) -> std::io::Result<std::process::Output> {
adb::adb(env, ["-s", device_serial_no, "reverse", remote, local])
.stdin_file(os_pipe::dup_stdin().unwrap())
.stdout_file(os_pipe::dup_stdout().unwrap())
.stderr_file(os_pipe::dup_stdout().unwrap())
.run()
}

fn remove_adb_reverse(
env: &cargo_mobile2::android::env::Env,
device_serial_no: &str,
remote: &str,
) {
// ignore errors in case the port is not forwarded
let _ = adb::adb(env, ["-s", device_serial_no, "reverse", "--remove", remote])
.stdin_file(os_pipe::dup_stdin().unwrap())
.stdout_file(os_pipe::dup_stdout().unwrap())
.stderr_file(os_pipe::dup_stdout().unwrap())
.run();
}
1 change: 1 addition & 0 deletions tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ fn run_build(
noise_level,
vars: Default::default(),
config: build_options.config.clone(),
target_device: None,
};
let handle = write_options(
&tauri_config.lock().unwrap().as_ref().unwrap().identifier,
Expand Down
6 changes: 5 additions & 1 deletion tooling/cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
flock,
},
interface::{AppInterface, AppSettings, Interface, MobileOptions, Options as InterfaceOptions},
mobile::{write_options, CliOptions, DevChild, DevProcess},
mobile::{write_options, CliOptions, DevChild, DevProcess, TargetDevice},
ConfigValue, Result,
};
use clap::{ArgAction, Parser};
Expand Down Expand Up @@ -233,6 +233,10 @@ fn run_dev(
noise_level,
vars: Default::default(),
config: dev_options.config.clone(),
target_device: device.as_ref().map(|d| TargetDevice {
id: d.serial_no().to_string(),
name: d.name().to_string(),
}),
};

let _handle = write_options(
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/src/mobile/ios/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ fn run_build(
noise_level,
vars: Default::default(),
config: build_options.config.clone(),
target_device: None,
};
let handle = write_options(
&tauri_config.lock().unwrap().as_ref().unwrap().identifier,
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ fn run_dev(
noise_level,
vars: Default::default(),
config: dev_options.config.clone(),
target_device: None,
};
let _handle = write_options(
&tauri_config.lock().unwrap().as_ref().unwrap().identifier,
Expand Down
8 changes: 8 additions & 0 deletions tooling/cli/src/mobile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ impl Target {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TargetDevice {
id: String,
name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliOptions {
pub dev: bool,
Expand All @@ -143,6 +149,7 @@ pub struct CliOptions {
pub noise_level: NoiseLevel,
pub vars: HashMap<String, OsString>,
pub config: Option<ConfigValue>,
pub target_device: Option<TargetDevice>,
}

impl Default for CliOptions {
Expand All @@ -154,6 +161,7 @@ impl Default for CliOptions {
noise_level: Default::default(),
vars: Default::default(),
config: None,
target_device: None,
}
}
}
Expand Down

0 comments on commit 9f75d06

Please sign in to comment.