rust-analyzer在Neovim上在macOS上无法工作。

huangapple go评论63阅读模式
英文:

rust-analyzer on neovim not working on macOS

问题

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 recognized 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)

英文:

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

得分: 2

以下是翻译好的内容:

修复列表中的第3项已合并到v2.0分支中<br/>
升级到最新版本应该能解决这个问题。


你可以解决这个问题的3种方法。

1)从rustup卸载rust-analyzer(rustup安装了一个无法使用的版本,请查看此回复

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

或者

2)使用homebrew安装rust-analyzer

brew install rust-analyzer

或者

3)在你的**~/.config/nvim/lua/core/init.lua文件中将第61行替换为 vim.env.PATH = vim.fn.stdpath "data" .. "/mason/bin" .. (is_windows and ";" or ":") .. vim.env.PATH。这将在你的PATH的开头添加~/.local/share/nvim/mason/bin/**。(来自问题 #1289

--   ~/.config/nvim/lua/core/init.lua
59 -- 将mason.nvim安装的二进制文件添加到路径中
...
---&gt; 61 vim.env.PATH = vim.env.PATH .. (is_windows and ";" or ":") .. vim.fn.stdpath "data" .. "/mason/bin"
+++&gt; 61 vim.env.PATH = vim.fn.stdpath "data" .. "/mason/bin" .. (is_windows and ";" or ":") .. vim.env.PATH

<br/>

然后将rust_analyzer添加到lspconfig.lua中。
(如果使用NvChad,编辑**~/.config/nvim/lua/custom/configs/lspconfig.lua**以添加local servers=(... "rust_analyzer")

在工作目录中确保项目有一个Cargo.toml文件

cargo init
nvim main.rs

查看此问题以了解问题,还请查看此回复

英文:

Fix #3 from this list has been merged into branch v2.0<br/>
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 &quot;data&quot; .. &quot;/mason/bin&quot; .. (is_windows and &quot;; &quot; or &quot;:&quot;) .. 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
    ...
    ---&gt; 61 vim.env.PATH = vim.env.PATH .. (is_windows and &quot;; &quot; or &quot;:&quot;) .. vim.fn.stdpath &quot;data&quot; .. &quot;/mason/bin&quot;
    +++&gt; 61 vim.env.PATH = vim.fn.stdpath &quot;data&quot; .. &quot;/mason/bin&quot; .. (is_windows and &quot;; &quot; or &quot;:&quot;) .. vim.env.PATH
    
    

<br/>

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

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

huangapple
  • 本文由 发表于 2023年5月8日 00:46:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定