RA VSCode extension not working on bin files

Hi everyone!

I'm using the rust-analyzer vscode extension and it works for the src/lib.rs file, but not for the src/bin/foo.rs and src/bin/bar.rs files. Cargo check works on both of these files.

I'm very new to Rust so it's likely that I'm doing something wrong.

Thank you in advance for your help!


Project structure:

.
├── Cargo.lock
├── Cargo.toml
├── rustfmt.toml
└── src
    ├── bin
    │   ├── foo.rs
    │   └── bar.rs
    ├── lib.rs
    └── main.rs

Cargo.toml:

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

[lib]
name = "local"
path = "src/lib.rs"

[[bin]]
name = "foo"
path = "src/bin/foo.rs"

[[bin]]
name = "bar"
path = "src/bin/bar.rs"

[dependencies]
anyhow = "1.0.75"
bitvec = "1.0.1"
threadpool = "1.8.1"

did you edit Cargo.toml outside vscode? try to press Ctrl P in vscode and run the command rust-analyzer: Reload workspace.

if it still doesn't work, try rust-analyzer: Restart server command.

PS: you don't need to list the targets in Cargo.toml if you stick with the directory structure naming convention used by cargo's target auto discovery feature.

in your example, src/bin/foo.rs and src/bin/bar.rs can be automatically discovered. the src/lib.rs though, will be discovered with the same name as the package, i.e. foobar, if you don't explicit specify the [lib] target in Cargo.toml.

1 Like

Thank you for the quick reply!

It didn't work, but that's because the error was indeed the user type...

I had errors in lib.rs so the analyzer wouldn't even try to report on the foo.rs and bar.rs code as they used code from lib.rs.

So, fixing the errors in lib.rs "fixed" the "problem".

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.