Merge pull request #1331 from dfaust/fix-target-dir

Fix passing relative paths to cargo
master
Jesper Håkansson 10 months ago committed by GitHub
commit debee9bcdf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 12
      CHANGELOG.md
  2. 25
      src/build/mod.rs

@ -2,6 +2,18 @@
## 🤍 Unreleased ## 🤍 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 ## ☀ 0.12.1
- ### 🤕 Fixes - ### 🤕 Fixes

@ -5,7 +5,7 @@ use crate::command::build::BuildProfile;
use crate::emoji; use crate::emoji;
use crate::manifest::Crate; use crate::manifest::Crate;
use crate::PBAR; use crate::PBAR;
use anyhow::{bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use std::str; use std::str;
@ -107,7 +107,28 @@ pub fn cargo_build_wasm(
} }
cmd.arg("--target").arg("wasm32-unknown-unknown"); 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<String> {
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::<Result<Vec<_>>>()?;
cmd.args(extra_options_with_absolute_paths);
child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?; child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
Ok(()) Ok(())
} }

Loading…
Cancel
Save