diff --git a/CHANGELOG.md b/CHANGELOG.md index 4908f43..dc49ab8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ ## 🤍 Unreleased +- ### 🤕 Fixes + + - **Fix passing relative paths to cargo - [dfaust], [issue/704], [issue/1156], [issue/1252], [pull/1331]** + + When building a crate located in a sub-directory, relative paths, passed as extra options to cargo (like `--target-dir`), are now handled correctly. + + [issue/704]: https://github.com/rustwasm/wasm-pack/issues/704 + [issue/1156]: https://github.com/rustwasm/wasm-pack/issues/1156 + [issue/1252]: https://github.com/rustwasm/wasm-pack/issues/1252 + [pull/1331]: https://github.com/rustwasm/wasm-pack/pull/1331 + [dfaust]: https://github.com/dfaust + ## ☀️ 0.12.1 - ### 🤕 Fixes 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(()) }