Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
XFajk opened this issue Mar 28, 2025 · 2 comments
Closed

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

XFajk opened this issue Mar 28, 2025 · 2 comments
Labels
C-support Category: support questions

Comments

@XFajk
Copy link

XFajk commented Mar 28, 2025

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

@XFajk XFajk added the C-support Category: support questions label Mar 28, 2025
@ChayimFriedman2
Copy link
Contributor

The way to pass configuration flags to rust-analyzer depends on your editor, and is usually not a file called rust-analyzer.json. For example, in VSCode, you need to create .vscode/settings.json. Also, the setting you want is "rust-analyzer.cargo.features": "all", not rust-analyzer.check.features.

@XFajk
Copy link
Author

XFajk commented Mar 28, 2025

@ChayimFriedman2 yeah thanks that actually fixed it I went to my configuration and added the setting there I got the idea of rust-analyzer.json from chatGPT but that was clearly a mistake thanks

@XFajk XFajk closed this as completed Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-support Category: support questions
Projects
None yet
Development

No branches or pull requests

2 participants