Vscode/rust-analyzer problems with multi binary project?

I use vscode and rust-analyzer in simple binary create that has multiple binaries (Advent of code, 1 to 24 single file bins to be exact).

For some strange reason, some basic errors are ignored/invisible in the opened files in vscode, e.g.:

fn main() {
    let args: Vec<String> = env::args().collect();
    dbg!(args);

shows as "ok" code, while cargo run --bin d02 shows:

cargo run --bin d02
   Compiling advent-of-code-2023 v0.1.0 (/home/user/github/aoc2023)
error[E0433]: failed to resolve: use of undeclared crate or module `env`
  --> src/d02.rs:98:29
   |
98 |     let args: Vec<String> = env::args().collect();
   |                             ^^^ use of undeclared crate or module `env`
   |
help: consider importing this module
   |
3  + use std::env;
   |

For more information about this error, try `rustc --explain E0433`.

which is obvious and expected...

cargo.toml has about 24 binaries declared in style of:

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

Strange thing is that i believe that last year this worked fine without any issues (older versions of vscode/plugins of course)

Anyone with similar experience?
Could this be some other vscode plugin affecting rust-analyzer?
Any tips/recommendations?

rust-analyzer should not have any trouble with a single package regardless of how many binaries it has got.

One of the common problems is if your VS Code workspace is not the package directory but some parent directory, in which case rust-analyzer will go looking for one package or Cargo workspace and stop when it finds one (or sometimes find none at all). Can you confirm that you've opened the parent directory of the Cargo.toml and not any higher directory as the workspace? A screenshot of the Explorer sidebar in VS Code will check this.

If that doesn't help, please post more information:

  • the full text of your Cargo.toml
  • the file tree of your project

thanks for leads... it looks like it might be something with my code/macro breaking the rust-analyzer...

ill try to build the reproducible minimal example, but i found out that RA stops pointing to errors when i try to use following macro from other file in my create...

// utils.rs
#[macro_export]
macro_rules! print_timed_result {
    ( $prefix:literal, $expression:expr ) => {
        let start = std::time::Instant::now();
        let result = $expression;
        let duration = start.elapsed();
        println!("{}: {:?} ({:?})", $prefix, result, duration);
    };
}

// d02.rs
mod utils;

fn main() {
    let suffix_arg: Option<String> = env::args().nth(1);  // <---- expected invisible error
    let suffix = suffix_arg.unwrap_or("".to_string());

    let path = format!("inputs/{:02}{suffix}.txt", DAY_ID);
    let data_str = utils::string_from_file(&path);
    let data;
    print_timed_result!("parse input", data = parse_input(&data_str));   // <--- culprit
    ...

When i comment out the "culprit" line (3 uses of macro in succession with different args to be exact), errors start showing straight away. :thinking:

Seems like it must be a bug. Perhaps details of the trouble might be visible in the RA server logs (click on the rust-analyzer status bar button to see it).

i may have found the reason - not sure if this is error of RA per se, or just improper usage form my side...

Explanation: my original last eyar directory had 25 binaries declared in Cargo.toml like:

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

etc., 25 of those in total per each december "challenge" day.

I have copied the directory sturcture from 2022 to 2023, and deleted source files for all but first 1-2 "days", that i have implemented this year (today is pending day 2). I've left Cargo.toml unedited, leaving the non-existent binaries there...

This seems to cause RA to silently "crash"/stop working. When i hover the rust-analyzer tab in vscode satus bar, and select "open logs", nothing is happening... (or, maybe is -> it seems that "output" panel is opened, being empty?)

When i try to run cargo clippy from command line, only there i see a "reminder" that there are some "missing" files/binaries...

error: couldn't read src/d18.rs: No such file or directory (os error 2)
error: couldn't read src/d04.rs: No such file or directory (os error 2)
error: could not compile `advent-of-code-2023` (bin "d18") due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `advent-of-code-2023` (bin "d04") due to previous error
error: couldn't read src/d19.rs: No such file or directory (os error 2)
...

My original expectation would be that RA lets me know i have something broken in project, but not sure how "insane" my specific breaking conditions were to expect it to manage.

Removing all the non-existent [[bin]] sections from Cargo.toml leads to RA "getting to work" and i see all the errors/clippies etc. in my existing d01/d02 files...

The RA status bar item should be reporting an error (with details on hover):

image

A little tip.
If you put the binaries in the src/bin folder, they will all work as executables without having to specify every binary target separately in Cargo.toml.

1 Like

unfortunately "nada", same color (no exclamation/x/other icon next to RA name) as rest of status bar, and as i mentioned no logs (or empty ones) :frowning:

Woah, I never thought about it (suspected something like this might exist), but it makes sense that this exists/works...
Thanks for tip, will definitely move to this with my crate structure! :slight_smile: