Dan Forbes
Dan Forbes

Reputation: 2824

Import Workspace Sibling

I do not understand why Cargo is failing to resolve a local import in a workspace project I have created. I will detail the problem below, but for those that wish to see a working example I have created a demo repo.

In this example consumer.rs is failing to compiling w/ the following error: use of undeclared crate or module `producer`

What makes things even more confusing for me is that rust-analyzer for VSCode seems to be able to kind of resolve the import (it is underlined in red, but Intellisense works).

What is the correct way to set-up my project so that Cargo can resolve my local import?

Cargo.toml

[workspace]
members = [
    "consumer",
    "producer",
]

[profile.release]
panic = "unwind"

consumer/Cargo.toml

[package]
name = "consumer"
version = "0.0.0"
edition = "2021"

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

[dependencies]
producer = { path = "../producer" }

consumer/src/lib.rs

use producer::Foo;

pub fn get_foo() -> Foo {
    Foo::Bar
}

producer/Cargo.toml

[package]
name = "producer"
version = "0.0.0"
edition = "2021"

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

producer/src/lib.rs

pub enum Foo {
    Bar
}

Upvotes: 2

Views: 1564

Answers (1)

Dan Forbes
Dan Forbes

Reputation: 2824

The crate-type of producer cannot be cdylib as this will produce a library that is intended to be used from other languages (ref: The Rust Reference). This confusion came about due to my limited experience with Rust as well as copypasta while exploring Rust for Wasm. I think in my case the best solution is just to remove the entire [lib] section from producer/Cargo.toml

Upvotes: 3

Related Questions