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

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

resolver = "2"
members = [

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.

name = "mdb_client"
path = "client/src/"

You're using client/src/ 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:

name = "mdb_client"
path = "src/"
1 Like

Thank you! I should have checked that.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.