英文:
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 ifCargo.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 ifCargo.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安装的二进制文件添加到路径中
...
---> 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
<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.
-
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
-
Installing
rust-analyzer
with homebrewbrew install rust-analyzer
or
-
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
<br/>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论