From d0fa87e36c73ed69c61525ed07c2528bd26b5ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dj8yf0=CE=BCl?= Date: Fri, 13 Dec 2024 23:57:27 +0200 Subject: [PATCH] fix: running `compile_project` concurrently in tests --- cargo-near-build/src/fs.rs | 11 +++++++ cargo-near-build/src/near/build/mod.rs | 44 ++++++++++++++++++-------- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/cargo-near-build/src/fs.rs b/cargo-near-build/src/fs.rs index 87a111f6..037de3b5 100644 --- a/cargo-near-build/src/fs.rs +++ b/cargo-near-build/src/fs.rs @@ -13,6 +13,17 @@ pub fn copy(from: &Utf8Path, to: &Utf8Path) -> eyre::Result { Ok(out_path) } +/// Copy a file to a destination. +/// +/// Does nothing if the destination is the same as the source to avoid truncating the file. +pub fn copy_full_target(from: &Utf8Path, to: &Utf8Path) -> eyre::Result { + if from != to { + std::fs::copy(from, to) + .wrap_err_with(|| format!("failed to copy `{}` to `{}`", from, to))?; + } + Ok(to.to_path_buf()) +} + /// Create the directory if it doesn't exist, and return the absolute path to it. pub fn force_canonicalize_dir(dir: &Utf8Path) -> eyre::Result { std::fs::create_dir_all(dir) diff --git a/cargo-near-build/src/near/build/mod.rs b/cargo-near-build/src/near/build/mod.rs index 585671d4..cf401d31 100644 --- a/cargo-near-build/src/near/build/mod.rs +++ b/cargo-near-build/src/near/build/mod.rs @@ -162,19 +162,37 @@ pub fn run(args: Opts) -> eyre::Result { color, )?; - wasm_artifact.path = crate::fs::copy(&wasm_artifact.path, &out_dir)?; - - if !args.no_wasmopt { - println!(); - pretty_print::handle_step( - "Running an optimize for size post-step with wasm-opt...", - || { - wasm_opt::OptimizationOptions::new_optimize_for_size() - .run(&wasm_artifact.path, &wasm_artifact.path)?; - Ok(()) - }, - )?; - } + let path_optimized = { + let name = wasm_artifact.path.file_stem().expect("is a file"); + let suffixed = [name, "_optimized"].concat(); + let extension = wasm_artifact.path.extension().expect("is a file"); + + let basename = wasm_artifact.path.parent().expect("has parent"); + + basename.join([&suffixed, ".", extension].concat()) + }; + wasm_artifact.path = { + let from_path = if !args.no_wasmopt { + println!(); + pretty_print::handle_step( + "Running an optimize for size post-step with wasm-opt...", + || { + println!("Optimizing to tmp-location {}", path_optimized); + wasm_opt::OptimizationOptions::new_optimize_for_size() + .run(&wasm_artifact.path, &path_optimized)?; + Ok(()) + }, + )?; + + path_optimized + } else { + wasm_artifact.path.clone() + }; + crate::fs::copy_full_target( + &from_path, + &out_dir.join(wasm_artifact.path.file_name().expect("has filename")), + )? + }; wasm_artifact.builder_version_info = Some(builder_version_info);