Skip to content

rust-analyzer not analyzing module behind #[cfg(feature)] #19468

@XFajk

Description

@XFajk

I'm creating a library crate with optional features and dependencies. In my Cargo.toml, I have:

[dependencies]
foo = { version = "0.4.14", optional = true }

[features]
bar_feature = ["dep:foo"]

In lib.rs, I declared a module like this:

#[cfg(feature = "bar_feature")]
pub mod bar_backend;

Then I created bar_backend/mod.rs. However, rust-analyzer isn't analyzing the file (not providing IDE services) because the module declaration is under a cfg attribute. The declaration appears greyed out with a message saying the code ignored feature is disabled

I understand that since the feature is disabled, the code is ignored, which prevents the module from being analyzed. I tried to enable the feature by:

  1. Creating a rust-analyzer.json file with:
{
  "rust-analyzer.check.features": "all",
}

This didn't work, and I couldn't verify if the rust-analyzer.json file was being read by rust-analyzer.

  1. Then I tried adding this to my Cargo.toml:
[profile.dev]
features = ["bar_feature"]

This un-greyed the module declaration (the #[cfg] part), but bar_backend/mod.rs is still not being analyzed.
Any way to get the IDE services in that file

I am working in neovim if you need other details ask

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-supportCategory: support questions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions