简体   繁体   中英

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM