Can't find local crate for WebAssembly

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?

Can you post the compilation error and the relevant code?

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

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

Figured it's clearest to link to the steps on specific lines, so...:

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

Fwiw I found somewhat of an explanation here: Cargo.toml - Hello wasm-pack!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.