Skip to content

Commit bc4a4c6

Browse files
committed
fix: use generated binary path from cargo instead
Previously when we'd invoke wasm-bindgen we'd assume that the binary we wanted to process was `target/wasm32-unknown-unknown{debug|release}/crate.wasm`, which isn't the case for all flags that Cargo accepts.
1 parent 62ab39c commit bc4a4c6

4 files changed

Lines changed: 80 additions & 38 deletions

File tree

src/bindgen.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,10 @@ pub fn wasm_bindgen_build(
2121
reference_types: bool,
2222
target: Target,
2323
profile: BuildProfile,
24-
extra_options: &Vec<String>,
24+
wasm_path: &Path,
2525
) -> Result<()> {
26-
let release_or_debug = match profile {
27-
BuildProfile::Release | BuildProfile::Profiling => "release",
28-
BuildProfile::Dev => "debug",
29-
};
30-
3126
let out_dir = out_dir.to_str().unwrap();
3227

33-
let target_directory = {
34-
let mut has_target_dir_iter = extra_options.iter();
35-
has_target_dir_iter
36-
.find(|&it| it == "--target-dir")
37-
.and_then(|_| has_target_dir_iter.next())
38-
.map(Path::new)
39-
.unwrap_or(data.target_directory())
40-
};
41-
42-
let wasm_path = target_directory
43-
.join("wasm32-unknown-unknown")
44-
.join(release_or_debug)
45-
.join(data.crate_name())
46-
.with_extension("wasm");
47-
4828
let dts_arg = if disable_dts {
4929
"--no-typescript"
5030
} else {

src/build/mod.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ use crate::emoji;
66
use crate::manifest::Crate;
77
use crate::PBAR;
88
use anyhow::{anyhow, bail, Context, Result};
9-
use std::path::Path;
10-
use std::process::Command;
9+
use cargo_metadata::Message;
10+
use std::io::BufReader;
11+
use std::path::{Path, PathBuf};
12+
use std::process::{Command, Stdio};
1113
use std::str;
1214

1315
pub mod wasm_target;
@@ -77,12 +79,16 @@ pub fn cargo_build_wasm(
7779
path: &Path,
7880
profile: BuildProfile,
7981
extra_options: &[String],
80-
) -> Result<()> {
82+
) -> Result<PathBuf> {
8183
let msg = format!("{}Compiling to Wasm...", emoji::CYCLONE);
8284
PBAR.info(&msg);
8385

8486
let mut cmd = Command::new("cargo");
85-
cmd.current_dir(path).arg("build").arg("--lib");
87+
cmd.current_dir(path)
88+
.stdout(Stdio::piped())
89+
.arg("build")
90+
.arg("--lib")
91+
.arg("--message-format=json-render-diagnostics");
8692

8793
if PBAR.quiet() {
8894
cmd.arg("--quiet");
@@ -129,8 +135,36 @@ pub fn cargo_build_wasm(
129135
.collect::<Result<Vec<_>>>()?;
130136
cmd.args(extra_options_with_absolute_paths);
131137

132-
child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
133-
Ok(())
138+
let mut last_artifact = None;
139+
140+
child::run_with_handler(cmd, "cargo build", |child| {
141+
let stdout = child.stdout.take().unwrap();
142+
let reader = BufReader::new(stdout);
143+
144+
for message in cargo_metadata::Message::parse_stream(reader) {
145+
match message? {
146+
Message::CompilerArtifact(artifact) => last_artifact = Some(artifact),
147+
Message::CompilerMessage(msg) => {
148+
if let Some(rendered) = msg.message.rendered {
149+
println!("{}", rendered);
150+
}
151+
}
152+
_ => (),
153+
}
154+
}
155+
156+
Ok(())
157+
})
158+
.context("Compiling your crate to WebAssembly failed")?;
159+
160+
let last_artifact = last_artifact
161+
.ok_or_else(|| anyhow!("No artifacts were generated by cargo build"))?
162+
.filenames
163+
.into_iter()
164+
.next()
165+
.ok_or_else(|| anyhow!("No artifact filenames were generated by cargo build"))?;
166+
167+
Ok(PathBuf::from(last_artifact))
134168
}
135169

136170
/// Runs `cargo build --tests` targeting `wasm32-unknown-unknown`.

src/child.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::install::Tool;
77
use anyhow::{bail, Result};
88
use log::info;
9-
use std::process::{Command, Stdio};
9+
use std::process::{Child, Command, Stdio};
1010

1111
/// Return a new Command object
1212
pub fn new_command(program: &str) -> Command {
@@ -62,3 +62,29 @@ pub fn run_capture_stdout(mut command: Command, command_name: &Tool) -> Result<S
6262
)
6363
}
6464
}
65+
66+
/// Run the command and handle child, return on success.
67+
pub fn run_with_handler<T>(
68+
mut command: Command,
69+
command_name: &str,
70+
handle: impl FnOnce(&mut Child) -> Result<T>,
71+
) -> Result<T> {
72+
info!("Running {:?}", command);
73+
74+
let mut child = command.spawn()?;
75+
76+
let ret = handle(&mut child)?;
77+
78+
let status = child.wait()?;
79+
80+
if status.success() {
81+
Ok(ret)
82+
} else {
83+
bail!(
84+
"failed to execute `{}`: exited with {}\n full command: {:?}",
85+
command_name,
86+
status,
87+
command,
88+
)
89+
}
90+
}

src/command/build.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub struct Build {
4141
pub bindgen: Option<install::Status>,
4242
pub cache: Cache,
4343
pub extra_options: Vec<String>,
44+
// step state
45+
cargo_artifact: Option<PathBuf>,
4446
}
4547

4648
/// What sort of output we're going to be generating and flags we're invoking
@@ -247,6 +249,7 @@ impl Build {
247249
bindgen: None,
248250
cache: cache::get_wasm_pack_cache()?,
249251
extra_options: build_opts.extra_options,
252+
cargo_artifact: None,
250253
})
251254
}
252255

@@ -355,16 +358,13 @@ impl Build {
355358

356359
fn step_build_wasm(&mut self) -> Result<()> {
357360
info!("Building wasm...");
358-
build::cargo_build_wasm(&self.crate_path, self.profile, &self.extra_options)?;
361+
let cargo_artifact =
362+
build::cargo_build_wasm(&self.crate_path, self.profile, &self.extra_options)?;
363+
364+
info!("wasm built at {:#?}.", cargo_artifact);
365+
366+
self.cargo_artifact = Some(cargo_artifact);
359367

360-
info!(
361-
"wasm built at {:#?}.",
362-
&self
363-
.crate_path
364-
.join("target")
365-
.join("wasm32-unknown-unknown")
366-
.join("release")
367-
);
368368
Ok(())
369369
}
370370

@@ -431,7 +431,9 @@ impl Build {
431431
self.reference_types,
432432
self.target,
433433
self.profile,
434-
&self.extra_options,
434+
self.cargo_artifact
435+
.as_ref()
436+
.expect("bindgen ran before cargo build"),
435437
)?;
436438
info!("wasm bindings were built at {:#?}.", &self.out_dir);
437439
Ok(())

0 commit comments

Comments
 (0)