Can't find local crate for WebAssembly

#1

I have the following structure:

root (created via rust webpack template)

  • crate
    • Cargo.toml
    • src
      • lib.rs <-- the entry point for the app via webpack
    • my_lib
      Cargo.toml
      • src
        • lib.rs <-- the entry point for my library

The app’s Cargo.toml (at /crate/Cargo.toml) contains:

[dependencies]
my_lib = { path = "my_lib" }

However, I can’t seem to use my_lib::* from within /crate/src/lib.rs

cargo check from within my_lib checks out fine (e.g. it’s not like the crate is invalid)

cargo check from within crate/ (i.e. the app’s rust root) doesn’t show that it can’t find the path - rather it’s the use statement which crashes things

Any idea what I’m doing wrong?

#2

Can you post the compilation error and the relevant code?

Maybe you forgot to export the relevant types/functions from my_lib with pub?

#3

Here, I created a small repo: rust-wasm-lib-boilerplate.

Figured it’s clearest to link to the steps on specific lines, so…:

#4

OK I looked at some other example I had, and saw a difference… tried it and it worked!

Adding rlib to the lib’s Cargo.toml

Updated the repo and now it runs (kept the bullet-list above linking to the failed commit, for reference)

The fix is here:

[lib]
crate-type = ["cdylib", "rlib"]

As opposed to just:

[lib]
crate-type = ["cdylib"]

Is rlib a requirement for wasm libs?

Also - what are the implications, and should I be having both cdylib and rlib in there? ( I just blindly copy/pasted… not really sure what’s going on…)

#5

Fwiw I found somewhat of an explanation here: https://rustwasm.github.io/wasm-pack/book/tutorials/npm-browser-packages/template-deep-dive/cargo-toml.html