3

I am using NeoVim with NvChad default configs on a M1 mac. Trying to edit a .rs file. Neovim lsp shows snippets but no error or autocompletion.

What I did

I have installed rust-analyzer with Mason and configured

# ~/.config/nvim/lua/custom/configs/lspconfig.lua
local servers=(... "rust_analyzer")

That caused Error:

[lspconfig] unhandled error: ...ig/lua/lspconfig/server_configurations/rust_analyzer.lua:41: Expected value but found T_END at character 1`

The problem was empty string was being passed to line 41: vim.json.decode that came from here

# ~/.local/share/nvim/lazy/nvim-lspconfig/lua/lspconfig/server_configurations/rust_analyzer.lua
   18   local jobid = vim.fn.jobstart(cmd, {
   19   │ on_stdout = function(_, data, _)
-->20   │ │ stdout[#stdout + 1] = table.concat(data, '\n')
   21   │ end,
...
-->41   stdout = vim.json.decode(table.concat(stdout, ''))
   42   return stdout and stdout['workspace_root'] or nil

Creating a Cargo.toml solved the problem.

But Client 1 quit with exit code 1 and signal 0 was the new error. It means for some reason the rust-analyzer crashed! The log

# ~/.local/state/nvim/lsp.log
[ERROR][2023-05-07 21:37:38] .../vim/lsp/rpc.lua:734    "rpc"   "rust-analyzer" "stderr"        "error: 'rust-analyzer' is not installed for the toolchain 'stable-aarch64-apple-darwin'"

I have installed it though with Mason , but does not work. Not even with :!rust-analyzer command. Error msg same as the log.

:!which rust-analyzer command outputs ~/.cargo/bin/rust-analyzer should be ~/.local/share/nvim/mason/bin/rust-analyzer. So the package installed with Mason is not recognised as it is later it the PATH. (see this issue)

Conclusion

  • Mason installs the correct packages of rust-analyzer for 'stable-aarch64-apple-darwin' toolchain.

  • If rustup version of rust-analyzer exists in ~/.cargo/bin/rust-analyzer it will be used. (this is the buggy package. issue)

  • rust-analyzer does not work if Cargo.toml is not at project root. (according to developers this is expected. see this reply)

1 Answers1

2

Fix #3 from this list has been merged into branch v2.0
Updating to latest version should fix this issue.


3 ways you can solve this issue.

  1. Uninstalling rust-analyzer from rustup ( rustup installs an unusable version of this package. see this reply )

     mv ~/.cargo/bin/rust-analyzer ~/.backup/rust-analyzer`
    

or

  1. Installing rust-analyzer with homebrew

     brew install rust-analyzer
    

or

  1. In your ~/.config/nvim/lua/core/init.lua file replace line:61 with vim.env.PATH = vim.fn.stdpath "data" .. "/mason/bin" .. (is_windows and "; " or ":") .. vim.env.PATH. This will add ~/.local/share/nvim/mason/bin/ at the beginning of your PATH. (from issue #1289 )

    --   ~/.config/nvim/lua/core/init.lua
         59 -- add binaries installed by mason.nvim to path
    ...
    ---> 61 vim.env.PATH = vim.env.PATH .. (is_windows and "; " or ":") .. vim.fn.stdpath "data" .. "/mason/bin"
    +++> 61 vim.env.PATH = vim.fn.stdpath "data" .. "/mason/bin" .. (is_windows and "; " or ":") .. vim.env.PATH
    
    

Then add rust_analyzer to lspconfig.lua. ( If using NvChad, edit ~/.config/nvim/lua/custom/configs/lspconfig.lua to add local servers=(... "rust_analyzer") )

In work directory make sure project has a Cargo.toml file

cargo init
nvim main.rs

see this issue to know the problem, also see this reply