Unable to import types from another canister. Works for a plain "lib" type

Here is a minimal reproduction

Basically, the issue is I am unable to import from a package that is a canister but can import from a package that contains a normal lib

These lines import from another canister

These lines import from another package in the workspace

The first one doesn’t work, the second one does. Can I not import from one canister package into another? Seems like an arbitrary limitation.

Thoughts?

Hi @saikatdas0790,

Likely, the constraint comes from following config in callee/Cargo.toml:

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

This is crucial for building target wasm. But it also make the crate not a rust lib anymore. Then it cannot be depended by other crates.

For data structures to be used by multiple canisters, it’s better to move them into a non-cdylib crate.
And let each canister crate depend on that crate.

This is indeed what you achieved with the utils crate.

2 Likes

Oh well, would have preferred to be able to do that, for the sake of colocating types, but as I read more, I understand this is a Rust limitation, not an IC limitation. :slight_smile:

I updated the code here to make the compiler happy, but the dfx build step still fails.

Basically, I tried 2 things:

  • Removing the crate type
  • Adding “lib” to the “cdylib” for the crate type array

Both of them make the compiler happy but the build fails.

What’s the reason for a canister type to specifically be “cdylib”?

You can definitely import from canisters but you might run into duplicate symbols, e.g. `rust-lld: error: duplicate symbol: canister_query http_request` when trying to use `ic-certified-assets` · Issue #304 · dfinity/cdk-rs · GitHub

So, better not to do it and just define things in a shared lib?