So in a new Rust project, I have my code written and sitting pretty and everything works. I am then ready to compile to wasm. I run wasm-pack init and get an error (described below). If I run the same command again, _everything works as expected_. π€ So weird.
Error:
$ wasm-pack init --scope tejaskumar
[1/8] π§ Checking crate configuration...
[2/8] π― Adding WASM target...
[3/8] π Compiling to WASM...
[4/8] π Creating a pkg directory...
[5/8] π Writing a package.json...
β οΈ [WARN]: Field description is missing from Cargo.toml. It is not necessary, but recommended
β οΈ [WARN]: Field repository is missing from Cargo.toml. It is not necessary, but recommended
β οΈ [WARN]: Field license is missing from Cargo.toml. It is not necessary, but recommended
[6/8] π― Copying over your README...
β οΈ [WARN]: origin crate has no README
[7/8] β¬οΈ Installing WASM-bindgen...
| [8/8] πββοΈ Running WASM-bindgen...
wasm-bindgen failed to execute properly. stderr:
error: failed to read `target/wasm32-unknown-unknown/release/my_wasmp.wasm`
caused by: No such file or directory (os error 2)
What should have happened?
It should have created a pkg folder with my npm package ready to go.
Clear steps describing how to reproduce the issue, including commands and flags run. If you are seeing an error, please include the full error message and stack trace.
Unfortunately, this is completely random and hard to reproduce. :(
Include the relevant details of your environment.
wasm-pack version: 0.4.2
rustc version: rustc 1.28.0 (9634041f0 2018-07-30)
hi @TejasQ - thank you for the thoughtful report, and so sorry you are running into this! due the unpredictable nature of the bug, i'll do my best to figure this out as soon as possible for ya, though it may take a bit to track down.
since it's only happening some of the time, i imagine there is a race condition... somewhere? is there any chance you could point me to the source code? or share a bit of it? would love to get this fixed for you and the source would def make it easier for me :)
Oh, the source code is just a reproduction of the README for a demo I'm doing at an event to get people fired up about this project.
Here's the code. I could even push up a repo if it helps. I'll do that shortly.
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
fn alert(message: &str);
}
#[wasm_bindgen]
pub fn say_hello() {
alert("Hello from wasm");
}
Thanks so much for your willingness to help, Ashley! π
hey @TejasQ thanks for the materials- i wasn't able to recreate the bug. one thing i can note- it looks like the binary created in your error case was called my_wasmp.wasm:
error: failed to read `target/wasm32-unknown-unknown/release/my_wasmp.wasm`
caused by: No such file or directory (os error 2)
however, the name in your Cargo.toml is my-wasm-app. did you change the name in your Cargo.toml at all? If not, that would suggest that wasm-pack is doing that, though I find that a bit surprising... (still not out of the question though!). Let me know and I'll keep poking in the meantime!
Oh no, I totally changed the name so it's more readable.
ah! ok- so perhaps changing the name was the issue... can you try again? that may have been the culprit. if you run wasm-pack and then change the name and run it again, it will look for the name in the Cargo.toml but i don't think it will always rebuild the binary (this is cargo- not wasm-pack). generally (and this is not ideal, but can guarantee no issue)- if i change anything in Cargo.toml rm -rf target and rm -rf pkg. this ensures that everything is rebuilt.
@alexcrichton am i right in observing that sometimes changes in Cargo.toml don't trigger a rebuild of the binary (if no other rust src files are touched?)
Okay, so for context:
The name of the app was my-wasmp in the beginning. I ran the compiler, and it failed with that error (which could be because of underscores instead of hyphens? π€). I ran it again, and it passed.
I then opened this issue, and renamed the project from my-wasmp to my-wasm-app _after_ the bug report for y'all's reading pleasure.
For that reason, I don't think changing the name was the culprit. I can try to reproduce it though, but now it's just working without breakages π€. I'll continue exploring and update this soon.
As an aside, I just demoed this great tool and it worked, so that's pretty great. I hope more people get fired up about the incredible doors this could open. π
@ashleygwilliams correct yeah, some metadata in Cargo.toml doesn't affect the final artifacts so it doesn't recompile, but things like changing the package name should recompile.
I was unable to reproduce this using the repo link above too :(
Ah, I'm sorry y'all βΒ it is _so random_.
Here's _exactly_ what I do to reproduce this on a semi-consistent basis:
cargo new my-wasm-appcd my-wasm-appCargo.tomlwasm-bindgen = "0.2.21" under dependenciescrate-type = ["cdylib"] under libsrc/lib.rs with the code from my repro repo.wasm-pack initI also use the --scope flag sometimes. Could _that_ be what breaks it? π I am so confused by this.
Thanks for trying to figure it out with me.
testing this out- but one thing to note! you'll probably want to pass --lib to the cargo new command :) the default changed a bit ago (it creates a binary project by default now, and you'd want a lib for this)
OK! can reproduce! here's the output i get
Ashleys-MacBook-Pro-2:my-wasm-app ag_dubs$ wasm-pack init
[1/8] π§ Checking crate configuration...
[2/8] π― Adding WASM target...
[3/8] π Compiling to WASM...
[4/8] π Creating a pkg directory...
[5/8] π Writing a package.json...
β οΈ [WARN]: Field description is missing from Cargo.toml. It is not necessary, but recommended
β οΈ [WARN]: Field repository is missing from Cargo.toml. It is not necessary, but recommended
β οΈ [WARN]: Field license is missing from Cargo.toml. It is not necessary, but recommended
[6/8] π― Copying over your README...
β οΈ [WARN]: origin crate has no README
[7/8] β¬οΈ Installing WASM-bindgen...
| [8/8] πββοΈ Running WASM-bindgen...
wasm-bindgen failed to execute properly. stderr:
error: failed to read `target/wasm32-unknown-unknown/release/my_wasm_app.wasm`
caused by: No such file or directory (os error 2)
and here's the repo with all assets, generated included: https://github.com/ashleygwilliams/303-repro
looks like it's a -/_ issue, and i should be able to fix it today
WOAH how'd you reproduce it? Was it following my steps?
@TejasQ yes! though i had to change a few things in your instructions :) i think i know what the issue is (we do account for the -/_ but i guess there's a bug in it! gonna try to have a patch today)
Thanks!
you'll probably want to pass
--libto thecargo newcommand
Thanks for the shorthand! So far, I've been manually making lib.rs and adding lib to Cargo.toml. π
@TejasQ so that generally works, but you'll note that Cargo.lock is not ignored for binary projects, but is ignored for library projects! if you keep doing it manually don't forget to add Cargo.lock to the .gitignore :)
Hah! I really shouldn't keep doing that manually. Always trust the abstraction, as they say. Β―\_(γ)_/Β―
Thanks for the info!
just so you know- i haven't forgotten about this.. the issue is that i cannot figure out how to test this successfully. you are very right that it fails the first time, and then works the second! with no changes at all. this is a very interesting bug- still working on it!
OK! 10 more minutes and i have a test that reproduces the error, https://github.com/rustwasm/wasm-pack/pull/322
ok so this is pretty weird. first run produces what is on the left, second+ subsequent runs, what is on the right

cc @alexcrichton @fitzgen you have any ideas? i am... baffled. these aren't even files that wasm-pack generates.
@TejasQ so- i think that you may have run into this because you were running cargo new which generated a main.rs- did you have one when u ran the wasm-pack init command?
Yes! Why would that break things? π€
@TejasQ so the best i can tell, this is the sitch we have (i'm gonna be way way way more methodical today so that i can really nail it down...):
lib.rs is compiled, it produces a wasm and a d file that always inflect - to _main.rs is compiled, it produces a wasm and a d file that maintain the pkg name, regardless of hyphen usagemain scenario (with hypens) but only half the lib scenario (with underscores). wasm-pack looks for the lib wasm which has underscores. because it doesn't fine the underscore wasm, it fails. on the second run (why??? i have no idea), it seems to finally make the underscore wasm.there's clearly some questions still here, HOWEVER, i think if you never have main.rs it will avoid this entirely. that doesn't mean that we have nothing to fix of course (we should warn or error, and also figure out why it's behaving this way) but for you, i can practically suggest that if you remove the main.rs before any compilation steps you shouldn't bump into this (if you do, please let us know!)
Thanks, Ashley! π
While I'm happy to have a way to make it work, my inner engineer is _dying_ to either: a) answer the questions to which we have no idea, or b) find the answer. Can I help y'all look? Can we look together? Ah, this is my favorite part about coding!
oh i promise you i continue to dig :) a thing that would certainly help is to see if you can reproduce without a main.rs. also- one thing i noted is the rustc version you listed is actually a stable rust, wasm-pack enforces nightly at the moment, so i'm trying to figure out what version of nightly rust and cargo you ended up using the day you ran this. if you have any further ideas about that please let me know!
FOR SURE! I'll get on it and report back with anything interesting that might come up.
it's been a second since this has been updated- do you continue to see this behavior with the latest releases? i'm going to close, but please comment/reopen if you continue to have trouble!
Most helpful comment
just so you know- i haven't forgotten about this.. the issue is that i cannot figure out how to test this successfully. you are very right that it fails the first time, and then works the second! with no changes at all. this is a very interesting bug- still working on it!