Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building workspace fails #642

Open
mysterycommand opened this issue May 7, 2019 · 10 comments
Open

Building workspace fails #642

mysterycommand opened this issue May 7, 2019 · 10 comments

Comments

@mysterycommand
Copy link

🐛 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
@ashleygwilliams
Copy link
Member

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.

@mysterycommand
Copy link
Author

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.

@wdanilo
Copy link

wdanilo commented Oct 2, 2019

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! :)

@Pauan
Copy link
Contributor

Pauan commented Oct 2, 2019

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

@wdanilo
Copy link

wdanilo commented Oct 2, 2019

@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

@Pauan
Copy link
Contributor

Pauan commented Oct 3, 2019

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

@nathan-at-least
Copy link

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?

@Pauan
Copy link
Contributor

Pauan commented Apr 12, 2020

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

@mrchantey
Copy link

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.

@DragonDev1906
Copy link

DragonDev1906 commented Jul 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants