wasm-pack
wasm-pack copied to clipboard
Building workspace fails
π Bug description
Running wasm-pack build in a workspace (e.g. the root Cargo.toml has [workspace] and members) fails with:
$ wasm-pack build
Error: failed to parse manifest: ./Cargo.toml
Caused by: missing field `package`
π€ Expected Behavior
wasm-pack build should build all the member crates into a common root-level pkg (or whatever) directory. This is the current behavior with cargo build --target wasm32-unknown-unknown (only it builds into target/wasm32-unknown-unknown/debug).
π Steps to reproduce
I created a minimal repro repo here: https://github.com/mysterycommand/wasm-pack-workspace β¦Β clone the repo, cd into the root, run wasm-pack build, share my pain.
π Your environment
$ rustup --version
rustup 1.18.2 (a0bf3c9cb 2019-05-02)
$ rustc --version
rustc 1.34.1 (fc50f328b 2019-04-24)
$ cargo --version
cargo 1.34.0 (6789d8a0a 2019-04-01)
$ wasm-pack --version
wasm-pack 0.8.1
$ wasm-bindgen --version
wasm-bindgen 0.2.43
repro'd immediately. thanks for giving the example repo! it's very helpful. i believe this is failing because wasm-pack wants to create a package.json for your project and give it a name. it will look for that name in the [package] of your Cargo.toml. in this case you don't have an overarching name for your project, you just have the 2 associated crates.
what would you want wasm-pack to name your package in this situation? once we sort that i think we can work this out! are you at all interested in contributing? if not, no worries, but i always like to ask :)
thanks again for the details and easily repro'd report.
Hey! Thanks for looking into this! I think it should just, build 2 packages? Letting the cargo part of the process share a root level target dir? I'm not really sure if that's actually how it works, but I was thinking like, before wasm-pack build:
$ tree .
.
βββ Cargo.lock
βββ Cargo.toml
βββ LICENSE-APACHE
βββ LICENSE-MIT
βββ README.md
βββ package.json
βββ src
β βββ examples
β βββ example-01
β β βββ Cargo.toml
β β βββ index.html
β β βββ src
β β βββ entry.js
β β βββ lib.rs
β β βββ utils.rs
β βββ example-02
β β βββ Cargo.toml
β β βββ index.html
β β βββ src
β β βββ entry.js
β β βββ lib.rs
β β βββ utils.rs
β βββ index.html
βββ yarn.lock
β¦Β and then after wasm-pack build:
$ tree .
.
βββ Cargo.lock
βββ Cargo.toml
βββ LICENSE-APACHE
βββ LICENSE-MIT
βββ README.md
βββ package.json
βββ src
β βββ examples
β βββ example-01
β β βββ Cargo.toml
β β βββ index.html
β β βββ pkg
β β β βββ example_01.d.ts
β β β βββ example_01.js
β β β βββ example_01_bg.d.ts
β β β βββ example_01_bg.wasm
β β β βββ package.json
β β βββ src
β β βββ entry.js
β β βββ lib.rs
β β βββ utils.rs
β βββ example-02
β β βββ Cargo.toml
β β βββ index.html
β β βββ pkg
β β β βββ example_02.d.ts
β β β βββ example_02.js
β β β βββ example_02_bg.d.ts
β β β βββ example_02_bg.wasm
β β β βββ package.json
β β βββ src
β β βββ entry.js
β β βββ lib.rs
β β βββ utils.rs
β βββ index.html
βββ target
β # lots of Rust/Cargo stuff here
βββ yarn.lock
I think that makes sense, you could publish multiple packages or crates from the same repo β¦Β maybe there's a future where like Cargo workspaces and Lerna/npm/Yarn workspaces could be pretty synonymous and not so much like a David Lynch movie (in the sense that like, all the pieces are there but they don't quite fit together).
I'm happy to help if I can. My background is in frontend/web/JS and I'm just starting to pick up/get the hang of Rust β¦ I might need some pointers.
The whole point of workspaces in Rust is the ability not to build every crate separately and share the intermediate results, so building crates separately with wasm-pack seems like a bad idea to me. I have a similar problem. I was using wasm-pack but I cannot anymore as I'm switching to workspaces :( Wasm-pack is so amazing that I would rather not leave it behind. Is this bug planned to be solved? :) If so, I would love to ask you to make it high-priority, as workspaces are crucial part of bigger rust projects. And as we all want Rust to become a great player in the WASM world, we need to support bigger projects, not only 1-crate examples! :)
@wdanilo Internally, wasm-pack calls cargo build (which does respect workspaces), so even if you call wasm-pack multiple times it will only generate one target directory, thus avoiding duplication.
@Pauan Oh, thats interesting! You are right, I just tested it and it works! You saved my life, thank you β€οΈ
The only downside is that my compile instruction is a little bit strange, but this is a very minor issue:
wasm-pack build --out-dir '../../pkg' lib/core
@wdanilo Indeed, and that's what this feature request is about: polishing the experience to make it nicer.
Any update on this feature request? I've started my first toy wasm example, and I immediately want a workspace, because I want a primary crate for web frontend application logic, then multiple supporting crates, so for example app_logic crate is the one which exports wasm bindings to JS and it depends on lib_a and lib_b for example (which do not export wasm bindings).
@wdanilo does this match your use? Can you describe your workspace layout and which directory you run that wasm-pack command from?
@nathan-at-least wasm-pack works fine with workspaces, you just have to run it once for each crate.
Or you can use an external plugin, such as rollup-plugin-rust (which allows you to build multiple crates). I've used this successfully in many of my own projects.
Ok so it works if you cd into the crate, ie
cd crates/my_crate && wasm-pack build
It'd be lovely if it 'just works' with no config.
Here is another (related) point: When using any of the following wasm-pack does not find the LICENSE file, which usually resides in the workspace root:
cd crates/my_crate && wasm-pack build
# or
wasm-pack build crates/my_crate
It's not a big thing, and can be solved by copying the license file over in advance or manually after building. But it would be nice if there would be a way for wasm-pack to recognize the LICENSE in the workspace root (unless the license differs or there is a local LICENSE file of course).
License key is set in Cargo.toml but no LICENSE file(s) were found; Please add the LICENSE file(s) to your project directory