Skip to content

Commit

Permalink
fix: running compile_project concurrently in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Dec 13, 2024
1 parent 6a39fbf commit d0fa87e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
11 changes: 11 additions & 0 deletions cargo-near-build/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ pub fn copy(from: &Utf8Path, to: &Utf8Path) -> eyre::Result<Utf8PathBuf> {
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<Utf8PathBuf> {
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<Utf8PathBuf> {
std::fs::create_dir_all(dir)
Expand Down
44 changes: 31 additions & 13 deletions cargo-near-build/src/near/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,37 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
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);

Expand Down

0 comments on commit d0fa87e

Please sign in to comment.