wasm-pack icon indicating copy to clipboard operation
wasm-pack copied to clipboard

Building workspace fails

Open mysterycommand opened this issue 6 years ago β€’ 10 comments

πŸ› 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

mysterycommand avatar May 07 '19 15:05 mysterycommand

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.

ashleygwilliams avatar May 16 '19 19:05 ashleygwilliams

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.

mysterycommand avatar May 16 '19 20:05 mysterycommand

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 avatar Oct 02 '19 21:10 wdanilo

@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 avatar Oct 02 '19 22:10 Pauan

@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 avatar Oct 02 '19 23:10 wdanilo

@wdanilo Indeed, and that's what this feature request is about: polishing the experience to make it nicer.

Pauan avatar Oct 03 '19 07:10 Pauan

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 avatar Apr 12 '20 15:04 nathan-at-least

@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.

Pauan avatar Apr 12 '20 16:04 Pauan

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.

mrchantey avatar Mar 17 '24 01:03 mrchantey

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

DragonDev1906 avatar Jul 01 '24 07:07 DragonDev1906