rustic
rustic copied to clipboard
Enable rust-analyzer optional cargo features
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 https://github.com/rust-lang/rust-analyzer/issues/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
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.
If you would have to configure rust-analyzer, that should be done via lsp-mode. Eg: https://github.com/emacs-lsp/lsp-mode/pull/4257
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.
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.
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?