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

Enable rust-analyzer optional cargo features #529

Open
EnigmaCurry opened this issue Sep 8, 2023 · 5 comments
Open

Enable rust-analyzer optional cargo features #529

EnigmaCurry opened this issue Sep 8, 2023 · 5 comments

Comments

@EnigmaCurry
Copy link

EnigmaCurry commented Sep 8, 2023

I am playing around with Leptos which lets you write isomorphic Rust code that runs both on the server and in the web-browser. It uses code blocks like this to run code that needs to execute only in one environment or the other:

use cfg_if::cfg_if;

cfg_if! {
if #[cfg(feature = "ssr")] {
//   nb:  "ssr" means exclusive server side code!
     ... 
} else {
// run this on the client only 
}

// run anything else in the file in both environments
...

The problem is that rust-analyzer does not consider any of the code nested inside the "ssr" feature check, and it does not report on any coding mistakes or warnings inside the block.

According to rust-lang/rust-analyzer#14045 rust-analyzer can only support ONE path through this block, but thats better than nothing. So the ssr feature can be enabled by setting the rust-analyzer.cargo.features setting by the lsp client, and this should enable rust-anaylzer to properly check the code inside the ssr block (at least).

So how do I set the rust-analyzer.cargo.features setting via rustic in Emacs? I found these likely variables and set them accordingly, but to no avail:

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 '(flycheck-rust-features '("ssr"))
 '(lsp-rust-all-features t)
 '(lsp-rust-features ["ssr"]))

Environment

$ emacs --version
GNU Emacs 29.1
$ rustc --version
`rustc` 1.74.0-nightly (ef85656a1 2023-08-21)
$ rust-analyzer --version
rust-analyzer 1.74.0-nightly (ef85656 2023-08-21)

rustic version: 3.4

@upbqdn
Copy link

upbqdn commented Jan 28, 2024

I found these likely variables and set them accordingly, but to no avail: [...]

I also tried setting the same variables, but I still can't get error lints for code behind features.

@psibi
Copy link
Collaborator

psibi commented Jan 29, 2024

If you would have to configure rust-analyzer, that should be done via lsp-mode. Eg: emacs-lsp/lsp-mode#4257

@upbqdn
Copy link

upbqdn commented Jan 29, 2024

This is my config for rustic in doom/config.el (simplified):

(after! rustic
  (setq lsp-rust-all-features t
        lsp-rust-features "all"))

I based it on https://emacs-lsp.github.io/lsp-mode/page/lsp-rust-rls/#lsp-rust-all-features and https://emacs-lsp.github.io/lsp-mode/page/lsp-rust-rls/#lsp-rust-features.
What I'm trying to do is to get lsp hints for code like the following:

fn main() {
    #[cfg(feature = "foobar")]
    {
        let foo = 42;
        println!("{}", bar);
    }
}

The problem is that no matter what I do in my config, I don't get lsp hints for the code behind features. For example, in the snippet above, there should be a hint that foo is unused and bar cannot be found. I get the hints only if I remove the line with #[cfg(feature = "foobar")].

I think enabling all features used to work about a year ago.

@psibi
Copy link
Collaborator

psibi commented Jan 30, 2024

When something like this happens, I try to check it's behavior with VSCode and see the language server logs and try to compare it with the lsp-mode interaction to see what is missing in lsp-mode.

@upbqdn
Copy link

upbqdn commented Jan 30, 2024

compare it with the lsp-mode interaction

That would help. Is there a way I can actually see the communication between lsp-mode and rust-analyzer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants