#Resolving dependency in a workspace

29 messages · Page 1 of 1 (latest)

weary crane
#

I'm setting up a project as a workspace with the structure as

├── Cargo.lock
├── Cargo.toml
├── game_client
│   ├── Cargo.toml
│   └── src
├── game_core
│   ├── Cargo.toml
│   └── src
└── target
    ├── CACHEDIR.TAG
    └── debug

Inside the root Cargo.toml I have

[workspace.dependencies]
bevy = "0.10"

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

And game_client's toml

[dependencies]
bevy = { workspace = true }
game_core = { path = "../game_core" }

game_core's toml:

[dependencies]
bevy = { workspace = true }

Now there is no problem finding the bevy crate within game_core, but in game_client it is unable to find it:

error[E0433]: failed to resolve: use of undeclared crate or module `bevy`
 --> game_client/src/main.rs:1:5
  |
1 | use bevy::prelude::*;
  |     ^^^^ use of undeclared crate or module `bevy`
high niche
#

Does cargo give any warnings before the error?

#

And are the 2 crates added as workspace members?

weary crane
#

This is the full cargo

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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

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

[workspace]
members = ["game_core", "game_client"]
resolver = "2"

[workspace.dependencies]
bevy = "0.10"
#

rust analyzer doesn't seem to have any issue with the way it's set up

high niche
#

Run a cargo check inside of game_client

#

And see what it says

#

In terms of both errors and warnings

weary crane
#

This is all I get

error[E0433]: failed to resolve: use of undeclared crate or module `bevy`
 --> game_client/src/main.rs:1:5
  |
1 | use bevy::prelude::*;
  |     ^^^^ use of undeclared crate or module `bevy`

error[E0433]: failed to resolve: use of undeclared type `App`
 --> game_client/src/main.rs:4:5
  |
4 |     App::new().run();
  |     ^^^ use of undeclared type `App`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `my_survivor` due to 2 previous errors
high niche
#

Preferably also do a cargo clean beforehand

weary crane
high niche
#

It's not using game_client's Cargo.toml at all

weary crane
#

oh?

high niche
#

So cargo commands to default to it

weary crane
#

If I remove the bin from the root cargo it makes rust analyzer very unhappy

high niche
#

Can I get the message?

weary crane
#

[ERROR rust_analyzer::main_loop] FetchWorkspaceError:
rust-analyzer failed to load workspace: Failed to read Cargo metadata from Cargo.toml file /home/samuel/Repositories/my_survivor/Cargo.toml, Some(Version { major: 1, minor: 68, patch: 0 }): Failed to run `cd "/home/samuel/Repositories/my_survivor" && "cargo" "metadata" "--format-version" "1" "--manifest-path" "/home/samuel/Repositories/my_survivor/Cargo.toml" "--filter-platform" "x86_64-unknown-linux-gnu"`: `cargo metadata` exited with an error: error: failed to parse manifest at `/home/samuel/Repositories/my_survivor/Cargo.toml`

Caused by:
  can't find library `my_survivor`, rename file to `src/lib.rs` or specify lib.path
high niche
#

Can you run a cargo check? It should give a better error message

high niche
weary crane
#

It looks like I fixed it by adding a src/lib.rs

high niche
#

Yeah it wants it because you said it was a package

#

Instead of a virtual workspace

weary crane
#

So if I remove the package section it won't require it?

high niche
#

Yeah

weary crane
#

looks like it worked 👍