2

I use NeoVim with LSP Zero plugin + Rust analyzer for rust programming.

When I jump to a definition in some external crate (including std) Nvim's diagnostic starts to show me all the errors from that crate.

For example, if I just create new project via cargo init some_project, then open it via nvim some_project and jump to the definition of println macro, diagnostic starts to show me tons of this:

/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/lib.rs|100 col 1-32 error| `#![feature]` may not be used on the stable release channel
/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/lib.rs|100 col 12-30 error| `#![feature]` may not be used on the stable release channel
/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/lib.rs|101 col 12-30 error| `#![feature]` may not be used on the stable release channel

Also I tried use CoC instead of native LSP, but it doesn't change anything.

What is correct way to disable diagnostic for crates outside my current workspace?

cafce25
  • 15,907
  • 4
  • 25
  • 31
Dmitry
  • 1,426
  • 5
  • 11

1 Answers1

1

Looks like I found solution for this issue.

We need to specify custom root_dir function when configuring LSP with Rust Analyzer:

local util = require('lspconfig.util')

local rustPorjectDir = nil -- caches current project dir
local rustRootPattern = util.root_pattern("Cargo.toml", "rust-project.json") -- default `root_dir` implementation

local function rust_root_dir(fname)
    local function get()
        rustPorjectDir = rustRootPattern(fname)
        return rustPorjectDir
    end

    return rustPorjectDir or get()
end
Dmitry
  • 1,426
  • 5
  • 11