nix-community / nixvim

Configure Neovim with Nix! [maintainer=@GaetanLepage, @traxys, @mattsturgeon, @khaneliman]
https://nix-community.github.io/nixvim
MIT License
1.59k stars 250 forks source link

[BUG] rust-analyzer stops providing diagnostics without clear reason #1960

Open vsiles opened 1 month ago

vsiles commented 1 month ago
Field Description
Plugin rustaceanvim (unsure)
Nixpkgs unstable
Home Manager unstable | <!-- [OPTIONAL]unstable` or specific version -->

Description

I have a strange behavior where rust-analyzer no longer shows diagnostics in nvim. I identified one occurrence (and can't explain why :D) but it's not the only one. nvim is install via home-manager/nixvim (https://github.com/vsiles/config/tree/master/home-manager)

Here's a repro:

$ cargo new test-project
$ cd test-project
$ cargo add whoami
$ nvim src/main.rs # add a line `const X : bool = 42;`, it should show an error
$ echo "fn main() { let _ = whoami::username(); }" > build.rs
$ nvim src/main.rs # no more error !
$ # download https://github.com/mrcjkb/rustaceanvim/blob/master/troubleshooting/minimal.lua
$ nvim -u minimal.lua src/main.rs # no error either

If I run rust-analyzer diagnostics . in the console, or cargo check, I do get the error. But with that simple build.rs, I don't have them in the editor.

I have other projects where I can't pinpoint what is bothering RA. How can I debug this ? I tried to set RA_LOG=info or debug but I don't see anything relevant. Any suggestions ?

For completeness:

This is why I think something's amiss in the way nixvim wraps nvim.

Minimal, Reproducible Example (MRE)

$ cargo new test-project
$ cd test-project
$ cargo add whoami
$ nvim src/main.rs # add a line `const X : bool = 42;`, it should show an error
$ echo "fn main() { let _ = whoami::username(); }" > build.rs
$ nvim src/main.rs # no more error !
$ # download https://github.com/mrcjkb/rustaceanvim/blob/master/troubleshooting/minimal.lua
$ nvim -u minimal.lua src/main.rs # no error either
GaetanLepage commented 1 month ago

This is weird, and I don't know what could cause this. However, the thing to try first is to set plugins.lsp.servers.rust-analyzer.installCargo and plugins.lsp.servers.rust-analyzer.installRustc to false. Like so, the LSP will fall back to using the cargo/rustc from your environment.

vsiles commented 1 month ago

Looks like it works ! In my config I'm using rustaceanvim instead of configuring rust-analyzer all by myself. Doing what you wrote made the error disappear, but I now have two RA running with the message:

nvim-lspconfig.rust_analyzer has been setup.
This will likely lead to conflicts with the rustaceanvim LSP client.
See ':h rustaceanvim.mason'

I tried to change my config in this way:

      rustaceanvim = {
        enable = true;
        settings = {
          server = {
            default_settings = {
              rust-analyzer = {
                installCargo = false;
                installRustc = false;
                cargo = {
                  allFeatures = true;
                };
                check = {
                  command = "clippy";
                };
                inlayHints = {
                  lifetimeElisionHints = {
                    enable = "always";
                  };
                };
              };
            };
            standalone = false;
          };
        };
      };

but it didn't work. I'll continue to see how I can avoid that warning message ;)

vsiles commented 1 month ago

Feels like I have to enable rust-analyzer (https://github.com/nix-community/nixvim/blob/main/plugins/lsp/language-servers/rust-analyzer.nix#L40) for the installCargo/Rustc option to be taken into account. Do you think there's a way around that without pulling the checks out of the lib.mkIf ?

vsiles commented 1 month ago

@GaetanLepage do you know of a way to configure installCargo and installRustc to false while using rustaceanvim and not rust-analyzer directly ?