Skip to content

Commit

Permalink
fix: dup stdout/stderr to spawn processes on NAPI contexts (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Feb 22, 2024
1 parent 961c982 commit b12ef08
Show file tree
Hide file tree
Showing 32 changed files with 147 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-duct-pipes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-mobile2": patch
---

Fix child process spawning on NAPI contexts.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ thiserror = "1.0"
toml = { version = "0.8", features = [ "preserve_order" ] }
duct = "0.13"
which = "5.0"
os_pipe = "1"

[dev-dependencies]
rstest = "0.18"
Expand Down
10 changes: 4 additions & 6 deletions src/android/adb/device_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use crate::{
android::{device::Device, env::Env, target::Target},
env::ExplicitEnv as _,
util::cli::{Report, Reportable},
DuctExpressionExt,
};
use once_cell_regex::regex_multi_line;
use std::collections::BTreeSet;
use std::{collections::BTreeSet, process::Command};
use thiserror::Error;

#[derive(Debug, Error)]
Expand Down Expand Up @@ -41,11 +40,10 @@ impl Reportable for Error {
const ADB_DEVICE_REGEX: &str = r"^([\S]{6,100}) device\b";

pub fn device_list(env: &Env) -> Result<BTreeSet<Device<'static>>, Error> {
let cmd = duct::cmd(env.platform_tools_path().join("adb"), ["devices"])
.vars(env.explicit_env())
.stdout_capture();
let mut cmd = Command::new(env.platform_tools_path().join("adb"));
cmd.arg("devices").envs(env.explicit_env());

super::check_authorized(&cmd.run()?)
super::check_authorized(&cmd.output()?)
.map(|raw_list| {
regex_multi_line!(ADB_DEVICE_REGEX)
.captures_iter(&raw_list)
Expand Down
2 changes: 2 additions & 0 deletions src/android/adb/device_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn device_name(env: &Env, serial_no: &str) -> Result<String, Error> {
cmd.args(["emu", "avd", "name"]);
Ok(())
})
.stderr_capture()
.stdout_capture()
.start()?
.wait()?,
Expand All @@ -53,6 +54,7 @@ pub fn device_name(env: &Env, serial_no: &str) -> Result<String, Error> {
cmd.args(["shell", "dumpsys", "bluetooth_manager"]);
Ok(())
})
.stderr_capture()
.stdout_capture()
.start()?
.wait()?,
Expand Down
5 changes: 4 additions & 1 deletion src/android/adb/get_prop.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::adb;
use crate::{
android::env::Env,
util::cli::{Report, Reportable},
};
use std::str;
use thiserror::Error;

use super::adb;

#[derive(Debug, Error)]
pub enum Error {
#[error("Failed to run `adb shell getprop {prop}`: {source}")]
Expand Down Expand Up @@ -44,7 +45,9 @@ pub fn get_prop(env: &Env, serial_no: &str, prop: &str) -> Result<String, Error>
Ok(())
})
.stdout_capture()
.stderr_capture()
.start()?;

let output = handle.wait()?;
super::check_authorized(output).map_err(|source| Error::LookupFailed {
prop: prop.to_owned(),
Expand Down
14 changes: 9 additions & 5 deletions src/android/bundletool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::util::cli::{Report, Reportable};
use std::path::PathBuf;
use thiserror::Error;

use crate::DuctExpressionExt;

#[cfg(not(target_os = "macos"))]
pub const BUNDLE_TOOL_JAR_INFO: BundletoolJarInfo = BundletoolJarInfo { version: "1.8.0" };

Expand Down Expand Up @@ -35,10 +37,12 @@ impl BundletoolJarInfo {

fn run_command(&self) -> duct::Expression {
let installation_path = self.installation_path();
duct::cmd("java", ["-jar"]).before_spawn(move |cmd| {
cmd.arg(&installation_path);
Ok(())
})
duct::cmd("java", ["-jar"])
.dup_stdio()
.before_spawn(move |cmd| {
cmd.arg(&installation_path);
Ok(())
})
}
}

Expand All @@ -49,7 +53,7 @@ pub fn command() -> duct::Expression {
}
#[cfg(target_os = "macos")]
{
duct::cmd!("bundletool")
duct::cmd!("bundletool").dup_stdio()
}
}

Expand Down
38 changes: 26 additions & 12 deletions src/android/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,14 @@ impl<'a> Device<'a> {

fn wait_device_boot(&self, env: &Env) {
loop {
let cmd = self.adb(env).stdout_capture().before_spawn(move |cmd| {
cmd.args(["shell", "getprop", "init.svc.bootanim"]);
Ok(())
});
let cmd = self
.adb(env)
.stderr_capture()
.stdout_capture()
.before_spawn(move |cmd| {
cmd.args(["shell", "getprop", "init.svc.bootanim"]);
Ok(())
});
let handle = cmd.start();
if let Ok(handle) = handle {
if let Ok(output) = handle.wait() {
Expand Down Expand Up @@ -237,8 +241,10 @@ impl<'a> Device<'a> {
cmd.arg(&apk_path);
Ok(())
})
.dup_stdio()
.start()?
.wait()?;

Ok(())
}

Expand Down Expand Up @@ -315,6 +321,7 @@ impl<'a> Device<'a> {
cmd.args(["shell", "input", "keyevent", "KEYCODE_WAKEUP"]);
Ok(())
})
.dup_stdio()
.start()?
.wait()?;
Ok(())
Expand Down Expand Up @@ -363,8 +370,10 @@ impl<'a> Device<'a> {
cmd.args(["shell", "am", "start", "-n", &activity]);
Ok(())
})
.dup_stdio()
.start()?
.wait()?;

let _ = self.wake_screen(env);

let filter = format!(
Expand Down Expand Up @@ -394,6 +403,7 @@ impl<'a> Device<'a> {
],
)
.vars(env.explicit_env())
.stderr_capture()
.stdout_capture();
let handle = cmd.start()?;
if let Ok(out) = handle.wait() {
Expand All @@ -408,7 +418,8 @@ impl<'a> Device<'a> {
env.platform_tools_path().join("adb"),
["logcat", "-v", "color", "-s", &filter],
)
.vars(env.explicit_env());
.vars(env.explicit_env())
.dup_stdio();

let logcat_filter_specs = config.logcat_filter_specs().to_vec();
logcat = logcat.before_spawn(move |cmd| {
Expand All @@ -431,19 +442,22 @@ impl<'a> Device<'a> {
.unprefix_path(jnilibs::path(config, *self.target))
.expect("developer error: jnilibs subdir not prefixed");
// -d = print and exit
let logcat_command = adb::adb(env, &self.serial_no).before_spawn(move |cmd| {
cmd.args(["logcat", "-d"]);
cmd.arg("-sym");
cmd.arg(&jnilib_path);
Ok(())
});
let logcat_command = adb::adb(env, &self.serial_no)
.before_spawn(move |cmd| {
cmd.args(["logcat", "-d"]);
cmd.arg("-sym");
cmd.arg(&jnilib_path);
Ok(())
})
.dup_stdio();
let stack_command =
duct::cmd::<PathBuf, [String; 0]>(env.ndk.home().join(consts::NDK_STACK), [])
.vars(env.explicit_env())
.env(
"PATH",
util::prepend_to_path(env.ndk.home().display(), env.path().to_string_lossy()),
);
)
.dup_stdio();

if logcat_command.pipe(stack_command).start()?.wait().is_err() {
println!(" -- no stacktrace --");
Expand Down
1 change: 1 addition & 0 deletions src/android/emulator/avd_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn avd_list(env: &Env) -> Result<BTreeSet<Emulator>, Error> {
["-list-avds"],
)
.vars(env.explicit_env())
.stderr_capture()
.read()
.map(|raw_list| {
raw_list
Expand Down
1 change: 1 addition & 0 deletions src/android/emulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl Emulator {
["-avd", &self.name],
)
.vars(env.explicit_env())
.dup_stdio()
}

pub fn start(&self, env: &Env) -> Result<Handle, std::io::Error> {
Expand Down
1 change: 1 addition & 0 deletions src/android/ndk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ impl Env {
cmd.arg(&elf_path);
Ok(())
})
.stderr_capture()
.read()?
.as_str(),
)
Expand Down
19 changes: 14 additions & 5 deletions src/apple/deps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ use super::{
device_ctl_available,
system_profile::{self, DeveloperTools},
};
use crate::util::{
self,
cli::{Report, TextWrapper},
prompt,
use crate::{
util::{
self,
cli::{Report, TextWrapper},
prompt,
},
DuctExpressionExt,
};
use once_cell_regex::regex;
use std::collections::hash_set::HashSet;
Expand Down Expand Up @@ -62,6 +65,7 @@ impl GemCache {
pub fn initialize(&mut self) -> Result<(), Error> {
if self.set.is_empty() {
self.set = duct::cmd("gem", ["list"])
.stderr_capture()
.read()
.map_err(Error::GemListFailed)?
.lines()
Expand Down Expand Up @@ -93,20 +97,25 @@ impl GemCache {
"sudo gem install"
};
duct::cmd(command, [package])
.dup_stdio()
.run()
.map_err(|source| Error::InstallFailed { package, source })?;
Ok(())
}
}

fn installed_with_brew(package: &str) -> bool {
duct::cmd("brew", ["list", package]).run().is_ok()
duct::cmd("brew", ["list", package])
.dup_stdio()
.run()
.is_ok()
}

fn brew_reinstall(package: &'static str) -> Result<(), Error> {
// reinstall works even if it's not installed yet, and will upgrade
// if it's already installed!
duct::cmd("brew", ["reinstall", package])
.dup_stdio()
.run()
.map_err(|source| Error::InstallFailed { package, source })?;
Ok(())
Expand Down
2 changes: 2 additions & 0 deletions src/apple/deps/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl Outdated {
}

duct::cmd("brew", ["outdated", "--json=v2"])
.stderr_capture()
.stdout_capture()
.run()
.map_err(OutdatedError::CommandFailed)
Expand All @@ -112,6 +113,7 @@ impl Outdated {

pub fn load(gem_cache: &mut GemCache) -> Result<Self, OutdatedError> {
let outdated_strings = duct::cmd("gem", ["outdated"])
.stderr_capture()
.read()
.map_err(OutdatedError::CommandFailed)?;
let packages = Self::outdated_brew_deps()?
Expand Down
17 changes: 13 additions & 4 deletions src/apple/deps/xcode_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::util::{
self,
cli::{Report, TextWrapper},
repo::{self, Repo},
use crate::{
util::{
self,
cli::{Report, TextWrapper},
repo::{self, Repo},
},
DuctExpressionExt,
};
use std::{
fmt::{self, Display},
Expand Down Expand Up @@ -60,6 +63,7 @@ pub fn xcode_user_dir() -> Result<PathBuf, Error> {

pub fn xcode_developer_dir() -> Result<PathBuf, Error> {
duct::cmd("xcode-select", ["-p"])
.stderr_capture()
.read()
.map(|output| {
// This output is expected to end with a newline, but we'll err on
Expand Down Expand Up @@ -196,6 +200,7 @@ impl Context {
cmd.arg(&info_path).arg("DVTPlugInCompatibilityUUID");
Ok(())
})
.stderr_capture()
.read()
.map(|s| s.trim().to_owned())
.map_err(Error::UuidLookupFailed)?;
Expand Down Expand Up @@ -230,6 +235,7 @@ impl Context {
cmd.arg(&ide_plugin_path).arg(&xcode_plugins_dir);
Ok(())
})
.dup_stdio()
.run()
.map_err(Error::PluginCopyFailed)?;
let spec_src = checkout.join("Specifications/Rust.xclangspec");
Expand All @@ -241,6 +247,7 @@ impl Context {
cmd.arg(&spec_src).arg(&spec_dst);
Ok(())
})
.dup_stdio()
.run()
.map_err(Error::SpecCopyFailed)?;
} else {
Expand All @@ -253,6 +260,7 @@ impl Context {
})?;
}
duct::cmd("cp", [&spec_src, &self.spec_dst])
.dup_stdio()
.run()
.map_err(Error::SpecCopyFailed)?;
}
Expand All @@ -264,6 +272,7 @@ impl Context {
cmd.arg(&meta_src).arg(&meta_dst);
Ok(())
})
.dup_stdio()
.run()
.map_err(Error::MetaCopyFailed)?;
}
Expand Down
1 change: 1 addition & 0 deletions src/apple/device/devicectl/device_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub fn device_list<'a>(env: &Env) -> Result<BTreeSet<Device<'a>>, DeviceListErro
cmd.arg(&json_output_path);
Ok(())
})
.stderr_capture()
.stdout_capture()
.vars(env.explicit_env())
.run()
Expand Down
Loading

0 comments on commit b12ef08

Please sign in to comment.