FRostri
FRostri

Reputation: 437

Use of undeclared crate or module with workspaces

I have the following structure:

-- project/
|
|-- Cargo.toml
|-- Cargo.lock
|-- src/ 
  |
  |-- main.rs
|-- crate1/
  |-- lib.rs
  |-- Cargo.toml
|-- tests
  |-- Cargo.toml
  |-- test.rs

and this are the content of the Cargo.toml

# Cargo.toml
...

[workspace]
members = [
  "crate1",

  "tests"
]

...
# crate1/Cargo.toml
[package]
name = "crate1"

...

[lib]
path = "lib.rs"

...

here I'm using another lib for my tests, I don't think the problem is here, because I already used this way to do my tests several times, and it worked perfectly, but for some reason, now this is happening to me, I don't know if everything is a typo error of my self or not, but I already checked it a lot of times

# tests/Cargo.tom
[package]
name = "tests"
version = "0.1.0"
edition = "2021"
publish = false

[dev-dependencies]
crate1 = { path = "../crate1" }

[[test]]
name = "crate1_test"
path = "crate1_test.rs"

[[test]]
name = "other_crate1_test"
path = "other_crate1_test.rs"

this is how one of the tests looks like

// tests/crate1_test.rs
use crate1::random_func;

[test]
fn random_func_test() {
    assert!(random_func());
}

And for some reason cargo don't recognize the "crate1" crate and throws me this error each time I import the crate:

error[E0433]: failed to resolve: use of undeclared crate or module `crate1`
 --> tests/crate1_test.rs:1:5
  |
1 | use crate1::random_func;
  |     ^^^^^^ use of undeclared crate or module `crate1`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `project-manager` due to previous error

Upvotes: 5

Views: 3371

Answers (2)

FRostri
FRostri

Reputation: 437

I found the problem, it was that I didn't make the crate1 as a dependency of the main project, this is how my root Cargo.toml looks now:

[package]
name = "project"
version = "0.1.0"
edition = "2021"

[workspace]
members = [
  "crate1",

  "tests"
]

[dependencies]
crate1 = { path = "crate1" }
reqwest = "0.11.13"
tokio = { version = "1", features = ["full"] }

and now I can build my tests correctly

Upvotes: 2

user20673258
user20673258

Reputation: 1

In src/main.rs you have to add mod crate1; and potentially mod tests;

Upvotes: -3

Related Questions