diff --git a/crates/moon/src/cli/run.rs b/crates/moon/src/cli/run.rs index 89b2684b..66e66571 100644 --- a/crates/moon/src/cli/run.rs +++ b/crates/moon/src/cli/run.rs @@ -25,6 +25,7 @@ use moonutil::common::FileLock; use moonutil::common::RunMode; use moonutil::common::SurfaceTarget; use moonutil::common::TargetBackend; +use moonutil::common::TestArtifacts; use moonutil::common::MOONBITLANG_CORE; use moonutil::common::MOON_PKG_JSON; use moonutil::common::{MoonbuildOpt, OutputFormat}; @@ -34,7 +35,6 @@ use moonutil::dirs::PackageDirs; use moonutil::mooncakes::sync::AutoSyncFlags; use moonutil::mooncakes::RegistryConfig; use n2::trace; -use std::path::PathBuf; use super::{BuildFlags, UniversalFlags}; @@ -102,10 +102,9 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu .join(format!("{}.core", file_name)) .display() .to_string()); - let output_wasm_or_js_path = &(output_artifact_path - .join(format!("{}.{}", file_name, target_backend.to_extension())) - .display() - .to_string()); + + let output_wasm_or_js_path = + output_artifact_path.join(format!("{}.{}", file_name, target_backend.to_extension())); let pkg_name = "moon/run/single"; let build_package_command = [ @@ -133,7 +132,7 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu .display() .to_string()), "-o", - output_wasm_or_js_path, + &output_wasm_or_js_path.display().to_string(), "-pkg-sources", &format!("{}:{}", pkg_name, mbt_file_parent_path.display()), "-pkg-sources", @@ -156,7 +155,7 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu TargetBackend::Wasm | TargetBackend::WasmGC => "moonrun", TargetBackend::Js => "node", }; - println!("{runner} {output_wasm_or_js_path}"); + println!("{} {}", runner, output_wasm_or_js_path.display()); } return Ok(0); } @@ -184,16 +183,18 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu } if cmd.build_only { + let test_artifacts = TestArtifacts { + artifacts_path: vec![output_wasm_or_js_path], + }; + println!("{}", serde_json_lenient::to_string(&test_artifacts)?); return Ok(0); } trace::scope("run", || match target_backend { TargetBackend::Wasm | TargetBackend::WasmGC => { - moonbuild::build::run_wat(&PathBuf::from(output_wasm_or_js_path), &cmd.args) - } - TargetBackend::Js => { - moonbuild::build::run_js(&PathBuf::from(output_wasm_or_js_path), &cmd.args) + moonbuild::build::run_wat(&output_wasm_or_js_path, &cmd.args) } + TargetBackend::Js => moonbuild::build::run_js(&output_wasm_or_js_path, &cmd.args), })?; Ok(0) @@ -292,7 +293,13 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res trace::open("trace.json").context("failed to open `trace.json`")?; } - let result = entry::run_run(&package_path, &moonc_opt, &moonbuild_opt, &module); + let result = entry::run_run( + &package_path, + &moonc_opt, + &moonbuild_opt, + &module, + cmd.build_only, + ); if trace_flag { trace::close(); } diff --git a/crates/moon/src/cli/test.rs b/crates/moon/src/cli/test.rs index 192e1170..f13ba052 100644 --- a/crates/moon/src/cli/test.rs +++ b/crates/moon/src/cli/test.rs @@ -343,6 +343,12 @@ fn do_run_test( auto_update, module, )?; + + // don't print test summary if build_only + if build_only { + return Ok(0); + } + let total = test_res.len(); let passed = test_res.iter().filter(|r| r.is_ok()).count(); diff --git a/crates/moon/tests/test_cases/mod.rs b/crates/moon/tests/test_cases/mod.rs index a3f303fd..5a0a423d 100644 --- a/crates/moon/tests/test_cases/mod.rs +++ b/crates/moon/tests/test_cases/mod.rs @@ -67,6 +67,18 @@ fn test_design() { main2 "#]], ); + + get_stdout_with_args(&dir, ["clean"]); + check( + &get_stdout_with_args_and_replace_dir( + &dir, + ["run", "main2", "--target", "js", "--build-only"], + ), + expect![[r#" + {"artifacts_path":["$ROOT/target/js/release/build/main2/main2.js"]} + "#]], + ); + assert!(dir.join("target/js/release/build/main2/main2.js").exists()); } #[test] @@ -5213,6 +5225,18 @@ fn test_moon_run_single_mbt_file() { "#]], ); + let output = get_stdout_with_args_and_replace_dir( + &dir, + ["run", "a/b/single.mbt", "--target", "js", "--build-only"], + ); + check( + &output, + expect![[r#" + {"artifacts_path":["$ROOT/a/b/target/single.js"]} + "#]], + ); + assert!(dir.join("a/b/target/single.js").exists()); + let output = get_stdout_with_args_and_replace_dir(&dir, ["run", "a/b/single.mbt", "--dry-run"]); check( &output, @@ -5572,6 +5596,19 @@ fn test_specify_source_dir_004() { Finished. moon: ran 3 tasks, now up to date "#]], ); + + get_stdout_with_args(&dir, ["clean"]); + check( + &get_stdout_with_args_and_replace_dir( + &dir, + ["run", "nes/t/ed/src/main", "--target", "js", "--build-only"], + ), + expect![[r#" + {"artifacts_path":["$ROOT/target/js/release/build/main/main.js"]} + "#]], + ); + assert!(dir.join("target/js/release/build/main/main.js").exists()); + check( &get_stdout_with_args_and_replace_dir(&dir, ["run", "nes/t/ed/src/main"]), expect![[r#" diff --git a/crates/moonbuild/src/entry.rs b/crates/moonbuild/src/entry.rs index 8ab8b7c3..573d6d2d 100644 --- a/crates/moonbuild/src/entry.rs +++ b/crates/moonbuild/src/entry.rs @@ -34,7 +34,7 @@ use crate::check::normal::write_pkg_lst; use crate::expect::{apply_snapshot, render_snapshot_fail}; use crate::runtest::TestStatistics; -use moonutil::common::{MoonbuildOpt, MooncOpt, TargetBackend}; +use moonutil::common::{MoonbuildOpt, MooncOpt, TargetBackend, TestArtifacts}; use std::sync::{Arc, Mutex}; @@ -196,6 +196,7 @@ pub fn run_run( moonc_opt: &MooncOpt, moonbuild_opt: &MoonbuildOpt, module: &ModuleDB, + build_only: bool, ) -> anyhow::Result { run_build(moonc_opt, moonbuild_opt, module)?; let (source_dir, target_dir) = (&moonbuild_opt.source_dir, &moonbuild_opt.target_dir); @@ -242,6 +243,15 @@ pub fn run_run( )); let wat_path = dunce::canonicalize(&wat_path) .context(format!("cannot find wat file at `{:?}`", &wat_path))?; + + if build_only { + let test_artifacts = TestArtifacts { + artifacts_path: vec![wat_path], + }; + println!("{}", serde_json_lenient::to_string(&test_artifacts)?); + return Ok(0); + } + trace::scope("run", || { if moonc_opt.link_opt.target_backend == TargetBackend::Wasm || moonc_opt.link_opt.target_backend == TargetBackend::WasmGC @@ -315,10 +325,6 @@ pub fn run_test( let result = n2_run_interface(state, moonbuild_opt)?; render_result(result, moonbuild_opt.quiet, "testing")?; - if build_only { - return Ok(vec![]); - } - let runtime = tokio::runtime::Builder::new_multi_thread() // todo: add config item .worker_threads(16) @@ -333,6 +339,9 @@ pub fn run_test( let filter_index = test_opt.as_ref().and_then(|it| it.filter_index); let printed = Arc::new(AtomicBool::new(false)); + let mut test_artifacts = TestArtifacts { + artifacts_path: vec![], + }; for (pkgname, _) in module .packages .iter() @@ -399,6 +408,11 @@ pub fn run_test( ); std::fs::write(&wrapper_js_driver_path, js_driver)?; + test_artifacts + .artifacts_path + .push(wrapper_js_driver_path.clone()); + } else { + test_artifacts.artifacts_path.push(artifact_path.clone()); } let printed = Arc::clone(&printed); @@ -443,6 +457,11 @@ pub fn run_test( } } + if build_only { + println!("{}", serde_json_lenient::to_string(&test_artifacts)?); + return Ok(vec![]); + } + let res = if moonbuild_opt.no_parallelize { runtime.block_on(async { let mut results = vec![]; diff --git a/crates/moonutil/src/common.rs b/crates/moonutil/src/common.rs index b458626d..42252996 100644 --- a/crates/moonutil/src/common.rs +++ b/crates/moonutil/src/common.rs @@ -385,6 +385,11 @@ impl TestOpt { } } +#[derive(serde::Serialize, Clone)] +pub struct TestArtifacts { + pub artifacts_path: Vec, +} + #[derive(Debug, Clone, Default)] pub struct FmtOpt { pub check: bool,