Cargo workspace issue with one of the sub-crates

Regarding this commit GitHub - malwaredb/malwaredb-rs at 1cdbab09ad36915758c921f3265d96bf8d6a85e8...

I'm working on a client/server system, where the server is the main project, some common items are sub-crates in crates/, and a client application in client/. I'm using Cargo workspaces, and I've run into a weird issue.

I'm trying to use the reqwest crate in the client/ sub-crate, and if I'm in the client/ directory, cargo build works fine. However, if I'm in the project directory, cargo build fails with use of undeclared crate or module 'reqwest'. The Cargo.toml file for the project has reqwest listed in [workspace.dependencies], and client/Cargo.toml has request = { workspace = true }.

If I add reqwest as a crate for the root project in [dependencies], then building in the root directory works fine. Am I missing something?

The sub-crate crates/server has workspace dependencies not listed as [dependencies] in the root Cargo.toml, and it works fine. Also, trying to move client/ to crates/client/ didn't help.

Root Cargo.toml

[workspace.dependencies]
reqwest = { version = "0.11.6", features = ["blocking", "json", "deflate", "gzip"], default-features = false }

[workspace]
resolver = "2"
members = [
    'crates/api',
    'crates/types',
    'crates/server',
    'client',
]

client/Cargo.toml
reqwest = { workspace = true }

PWD=${PROJECT_HOME}/client/: cargo build works fine!
PWD=${PROJECT_HOME}/: cargo build has the error: undeclared crate or module 'reqwest'

"undeclared crate or module 'reqwest'" is an error from the compiler, indicating that whichever crate is being compiled doesn't have a dependency on reqwest.

If I add reqwest as a crate for the root project in [dependencies] , then building in the root directory works fine. Am I missing something?

So, your workspace's root package must be mentioning reqwest somewhere. Either that's needed, so keep the dependency, or it's unwanted, so look at the code where the error is triggered and remove it.

If something is still confusing, please share the full output from cargo check. Not just the main message, but everything from the start to the end of the command. That will give us more clues.

Oh, I looked harder and found what is probably your problem, in your root Cargo.toml.

[[bin]]
name = "mdb_client"
path = "client/src/main.rs"

You're using client/src/main.rs as the source code of a target in the root package. You should probably not do that, because it's causing your problem, and also because it's generally confusing for one package to be compiling source files that look like they're in another package.

If you want mdb_client to be a binary target name, then declare it in client/Cargo.toml instead:

[[bin]]
name = "mdb_client"
path = "src/main.rs"
1 Like

Thank you! I should have checked that.