Skip to content

rust-analyzer fails to recognize cfg-gated test crate despite server.extraEnv override #20461

@danielrachi1

Description

@danielrachi1

rust-analyzer version: 0.3.2577-standalone

rustc version: rustc 1.88.0 (6b00bc388 2025-06-23)

editor or extension: VS Code v1.103.0 with rust-lang.rust-analyzer extension v0.3.2577

relevant settings: The issue occurs with the following setting in .vscode/settings.json, which is intended to solve the problem but does not:

{
  "rust-analyzer.server.extraEnv": {
    "RUSTFLAGS": "-C debug-assertions=no"
  }
}

Problem Description

In a project with a custom test harness (autotests = false and a [[test]] target), rust-analyzer fails to analyze the test crate files if the crate's root file is gated by #![cfg(not(debug_assertions))].

Attempts to override this using various settings (cargo.extraEnv, check.overrideCommand, server.extraEnv) fail to make the crate visible to the analyzer, even though diagnostic logs show the settings are being received by the server. The files consistently show the warning: "This file is not included in any crates...".

The only successful workaround is to modify the source code to use #![cfg(test)], which is not always desirable.

Minimal Steps to Reproduce

The issue is reproducible from scratch with the following minimal project:

1. Directory Structure:

ra_test/
├── .vscode/
│   └── settings.json
├── src/
│   └── lib.rs
├── tests/
│   ├── main.rs
│   └── a_normal_test.rs
└── Cargo.toml

2. Cargo.toml content:

[package]
name = "ra_test"
version = "0.1.0"
edition = "2021"
autotests = false

[[test]]
name = "my_tests"
path = "tests/main.rs"

3. tests/main.rs content:

#![cfg(not(debug_assertions))]

mod a_normal_test;

4. tests/a_normal_test.rs content:

#[test]
pub fn i_am_a_test_function() {
    assert!(true);
}

5. .vscode/settings.json content:

{
  "rust-analyzer.server.extraEnv": {
    "RUSTFLAGS": "-C debug-assertions=no"
  }
}

Expected Behavior

After configuring the workspace with server.extraEnv, rust-analyzer should respect the RUSTFLAGS during its initial cargo metadata analysis. It should recognize that debug_assertions is disabled, and therefore successfully discover and analyze the my_tests crate, providing full IDE services for tests/main.rs and tests/a_normal_test.rs.

Actual Behavior

The files in the tests/ directory continue to show the warning: "This file is not included in any crates, so rust-analyzer can't offer IDE services."

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions