Home > Net >  Why is rust-analyzer not finding dependencies in workspace member?
Why is rust-analyzer not finding dependencies in workspace member?

Time:05-06

I have a cargo workspace with one library "./raytracing" and one binary "./cli". The two workspace members have different dependencies.

./raytracing/Cargo.toml:

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

[dependencies]
rand = "0.8.5"

./cli/Cargo.toml:

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

[dependencies]
raytracer = {path = "../raytracer"}
rand = "0.8.5"
rand_xorshift = "0.3.0"

Parent directory Cargo.toml (./Cargo.toml)

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

[workspace]
members = [
    "raytracer",
    "cli",
]

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

Compiling both the raytracer and cli packages using cargo build -p works, however in VSCode rust-analyzer complains about my dependencies in the ./cli package when I import them in main.rs: Error

CodePudding user response:

With your current setup you actually have two binaries compiled from the same source file: cli and raytracing. raytracing doesn't declare any [dependencies] so of course you're getting an error when trying to compile that. You haven't explained what you're trying to achieve, so I can't tell you the correct way to fix this problem. Generally, there's two things you can do:

  • Make the root package virtual by deleting everything from ./Cargo.toml except the [workspace] section. (normal)
  • Ditch the ./cli/Cargo.toml and move its [dependencies] section to ./Cargo.toml. (bit weird)
  • Related