From 5a38a2f0eac3f9d8a8d1a80181782e86b71c59b9 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Wed, 18 Dec 2024 14:57:07 +0800 Subject: [PATCH] internal: tweak native backend spawn subprocess args (#529) --- crates/moonbuild/src/build.rs | 31 +++++++++++++++++-------- crates/moonbuild/src/runtest.rs | 40 ++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/crates/moonbuild/src/build.rs b/crates/moonbuild/src/build.rs index f2b6a3c1..5c093fd5 100644 --- a/crates/moonbuild/src/build.rs +++ b/crates/moonbuild/src/build.rs @@ -39,30 +39,43 @@ pub fn load_moon_proj( } pub fn run_wat(path: &Path, args: &[String], verbose: bool) -> anyhow::Result<()> { - run("moonrun", path, args, verbose) + run(Some("moonrun"), path, args, verbose) } pub fn run_js(path: &Path, args: &[String], verbose: bool) -> anyhow::Result<()> { - run("node", path, args, verbose) + run(Some("node"), path, args, verbose) } pub fn run_native(path: &Path, args: &[String], verbose: bool) -> anyhow::Result<()> { - run(path.to_str().unwrap(), path, args, verbose) + run(None, path, args, verbose) } -fn run(command: &str, path: &Path, args: &[String], verbose: bool) -> anyhow::Result<()> { +fn run(runtime: Option<&str>, path: &Path, args: &[String], verbose: bool) -> anyhow::Result<()> { if verbose { - eprintln!("{} {} {}", command, path.display(), args.join(" ")); + if let Some(runtime) = runtime { + eprintln!("{} {} {}", runtime, path.display(), args.join(" ")); + } else { + eprintln!("{} {}", path.display(), args.join(" ")); + } } - let mut execution = Command::new(command) - .arg(path) - .args(args) + let mut subprocess = Command::new(if let Some(runtime) = runtime { + runtime + } else { + path.to_str().unwrap() + }); + + if runtime.is_some() { + subprocess.arg(path); + } + subprocess.args(args); + + let mut execution = subprocess .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .spawn() .context(format!( "failed to execute: {} {} {}", - command, + runtime.unwrap_or(""), path.display(), if args.is_empty() { "".to_string() diff --git a/crates/moonbuild/src/runtest.rs b/crates/moonbuild/src/runtest.rs index 6a77780e..2e4cd96a 100644 --- a/crates/moonbuild/src/runtest.rs +++ b/crates/moonbuild/src/runtest.rs @@ -78,7 +78,7 @@ pub async fn run_wat( let mut _args = vec!["--test-args".to_string()]; _args.push(serde_json_lenient::to_string(args).unwrap()); run( - "moonrun", + Some("moonrun"), path, target_dir, &_args, @@ -96,7 +96,7 @@ pub async fn run_js( verbose: bool, ) -> anyhow::Result>> { run( - "node", + Some("node"), path, target_dir, &[serde_json_lenient::to_string(args).unwrap()], @@ -114,7 +114,7 @@ pub async fn run_native( verbose: bool, ) -> anyhow::Result>> { run( - path.to_str().unwrap(), + None, path, target_dir, &[serde_json_lenient::to_string(args).unwrap()], @@ -125,7 +125,7 @@ pub async fn run_native( } async fn run( - command: &str, + runtime: Option<&str>, path: &Path, target_dir: &Path, args: &[String], @@ -133,16 +133,36 @@ async fn run( verbose: bool, ) -> anyhow::Result>> { if verbose { - eprintln!("{} {} {}", command, path.display(), args.join(" ")); + if let Some(runtime) = runtime { + eprintln!("{} {} {}", runtime, path.display(), args.join(" ")); + } else { + eprintln!("{} {}", path.display(), args.join(" ")); + } + } + + let mut subprocess = tokio::process::Command::new(if let Some(runtime) = runtime { + runtime + } else { + path.to_str().unwrap() + }); + + if runtime.is_some() { + subprocess.arg(path); } - let mut execution = tokio::process::Command::new(command) - .arg(path) - .args(args) + subprocess.args(args); + + let mut execution = subprocess .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::inherit()) .spawn() - .with_context(|| format!("failed to execute '{} {}'", command, path.display()))?; + .with_context(|| { + format!( + "failed to execute '{} {}'", + runtime.unwrap_or(""), + path.display() + ) + })?; let mut stdout = execution.stdout.take().unwrap(); let mut test_capture = @@ -159,7 +179,7 @@ async fn run( .await .context(format!( "failed to read stdout for {} {} {}", - command, + runtime.unwrap_or(""), path.display(), args.join(" ") ))?;