From 5d46fa84e68d0ac8402b4ace04d2b325055d83bc Mon Sep 17 00:00:00 2001 From: Daniel Faust Date: Fri, 22 Sep 2023 10:28:27 +0200 Subject: [PATCH] Fix passing relative paths to cargo Fixes #704, #1156, #1252 --- src/build/mod.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/build/mod.rs b/src/build/mod.rs index 691ec49..d657071 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -5,7 +5,7 @@ use crate::command::build::BuildProfile; use crate::emoji; use crate::manifest::Crate; use crate::PBAR; -use anyhow::{bail, Context, Result}; +use anyhow::{anyhow, bail, Context, Result}; use std::path::Path; use std::process::Command; use std::str; @@ -107,7 +107,28 @@ pub fn cargo_build_wasm( } cmd.arg("--target").arg("wasm32-unknown-unknown"); - cmd.args(extra_options); + + // The `cargo` command is executed inside the directory at `path`, so relative paths set via extra options won't work. + // To remedy the situation, all detected paths are converted to absolute paths. + let mut handle_path = false; + let extra_options_with_absolute_paths = extra_options + .iter() + .map(|option| -> Result { + let value = if handle_path && Path::new(option).is_relative() { + std::env::current_dir()? + .join(option) + .to_str() + .ok_or_else(|| anyhow!("path contains non-UTF-8 characters"))? + .to_string() + } else { + option.to_string() + }; + handle_path = matches!(&**option, "--target-dir" | "--out-dir" | "--manifest-path"); + Ok(value) + }) + .collect::>>()?; + cmd.args(extra_options_with_absolute_paths); + child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?; Ok(()) }