Rust-Analyzer bug with macros and file inclusion

Hi. I'm experiencing (what I assume to be) a bug with rust-analyzer, but my uses case is pretty unique so I wouldn't be surprised if this is something that isn't supported.

I'm using a declarative macro debug_release_modules in multiple places throughout my project to toggle what code gets compiled.

I'm using VS Code with the latest rust-analyzer version (v0.2.654).

I've managed to reduce my problem to this minimal example with three files.

// main.rs
macro_rules! debug_release_modules {
    () => {
        #[cfg(debug_assertions)]
        pub mod debug;
        #[cfg(debug_assertions)]
        pub use debug::*;

        #[cfg(not(debug_assertions))]
        pub mod release;
        #[cfg(not(debug_assertions))]
        pub use release::*;
    };
}

debug_release_modules!();

fn main() {
    println!("{}", MODE);
}

// debug.rs
pub const MODE: &str = "DEBUG";

const VALUE: i32 = 15.7;

// release.rs
pub const MODE: &str = "RELEASE";

const VALUE: i32 = 15.7;

When load my project with rust-analyzer with it's default settings, this works fine. It correctly identifies the error with assigning a float to the VALUE constant in the debug.rs file.

If I edit the extension settings like so:

"rust-analyzer.runnables.cargoExtraArgs": [
    "--release"
],
"rust-analyzer.checkOnSave.extraArgs": [
    "--release"
],

Then it now highlights the error in release.rs and the error in debug.rs is gone. This is the behavior I expect.

However, in both cases rust-analyzer gives me an error on the release.rs file saying:
file not included in module tree (unlinked-file)
I would expect this error to be present in release.rs when using debug compilation. But it doesn't switch to complaining that debug.rs isn't included with release compilation.

This isn't negatively affecting me too much, but it does add a lot of noise to the 'problems' pane in VS Code.

Should I file this as a bug in rust-analyzers repo, or is doing something like this with macros and module inclusion never going to work smoothly?

I believe checkOnSave is mainly used for getting compiler errors from rustc while rust-analyser does its own analysis for the unlinked-file lint (presumably with debug_assertions always set).

That makes sense, as all the errors that aren't unlinked-file line up with what happens if I run cargo check or cargo check --release directly.

I did some more digging and found this in the manual:

Then I inserted this into the extension settings:

"rust-analyzer.diagnostics.disabled": [
    "unlinked-file"
],

And that has solved my problem. Thanks for you help!

That diagnostic is only supposed to be a hint, not an error. It was accidentally changed, and should be fixed in the next release.

1 Like

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.