Skip to content

Commit debee9b

Browse files
authored
Merge pull request #1331 from dfaust/fix-target-dir
Fix passing relative paths to cargo
2 parents d70be1e + 3b49173 commit debee9b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
## 🤍 Unreleased
44

5+
- ### 🤕 Fixes
6+
7+
- **Fix passing relative paths to cargo - [dfaust], [issue/704], [issue/1156], [issue/1252], [pull/1331]**
8+
9+
When building a crate located in a sub-directory, relative paths, passed as extra options to cargo (like `--target-dir`), are now handled correctly.
10+
11+
[issue/704]: https://github.com/rustwasm/wasm-pack/issues/704
12+
[issue/1156]: https://github.com/rustwasm/wasm-pack/issues/1156
13+
[issue/1252]: https://github.com/rustwasm/wasm-pack/issues/1252
14+
[pull/1331]: https://github.com/rustwasm/wasm-pack/pull/1331
15+
[dfaust]: https://github.com/dfaust
16+
517
## ☀️ 0.12.1
618

719
- ### 🤕 Fixes

src/build/mod.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::command::build::BuildProfile;
55
use crate::emoji;
66
use crate::manifest::Crate;
77
use crate::PBAR;
8-
use anyhow::{bail, Context, Result};
8+
use anyhow::{anyhow, bail, Context, Result};
99
use std::path::Path;
1010
use std::process::Command;
1111
use std::str;
@@ -107,7 +107,28 @@ pub fn cargo_build_wasm(
107107
}
108108

109109
cmd.arg("--target").arg("wasm32-unknown-unknown");
110-
cmd.args(extra_options);
110+
111+
// The `cargo` command is executed inside the directory at `path`, so relative paths set via extra options won't work.
112+
// To remedy the situation, all detected paths are converted to absolute paths.
113+
let mut handle_path = false;
114+
let extra_options_with_absolute_paths = extra_options
115+
.iter()
116+
.map(|option| -> Result<String> {
117+
let value = if handle_path && Path::new(option).is_relative() {
118+
std::env::current_dir()?
119+
.join(option)
120+
.to_str()
121+
.ok_or_else(|| anyhow!("path contains non-UTF-8 characters"))?
122+
.to_string()
123+
} else {
124+
option.to_string()
125+
};
126+
handle_path = matches!(&**option, "--target-dir" | "--out-dir" | "--manifest-path");
127+
Ok(value)
128+
})
129+
.collect::<Result<Vec<_>>>()?;
130+
cmd.args(extra_options_with_absolute_paths);
131+
111132
child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
112133
Ok(())
113134
}

0 commit comments

Comments
 (0)