Neovim中无法为.sol文件挂载solidity-lsp的问题求助
Hey there, let’s work through your solidity-lsp problems step by step—since you’re new to Neovim and Lua, I’ll keep each step straightforward and explain what’s going on behind the scenes.
1. Fix the Inconsistent Executable Status
You mentioned Mason installed solidity-ls but it wouldn’t run in the terminal, then you installed it globally via npm. First, we need to make sure Neovim is pointing to the correct working executable:
- Run
which solidity-lsin your terminal to get the full path (e.g.,/usr/local/bin/solidity-ls) - In your LSP config for solidity, explicitly set the
cmdoption to this path to avoid confusion between Mason’s local install and your global npm install:
require('lspconfig').solidity.setup({ cmd = { '/usr/local/bin/solidity-ls' }, -- Replace with your actual path from `which` -- We’ll add more config here next })
2. Fix Root Directory Detection & the ipairs Error
Your manual root_dir returning the current working directory is likely causing issues—solidity-lsp expects a proper project root (marked by files like hardhat.config.js, truffle-config.js, or a .git folder). The SERVER_REQUEST_HANDLER_ERROR with ipairs usually pops up when the LSP receives invalid data from a misconfigured root_dir.
Update your config to use a smarter root detector that prioritizes actual Solidity project markers, with a fallback to the current directory:
require('lspconfig').solidity.setup({ cmd = { '/usr/local/bin/solidity-ls' }, on_attach = function(client, bufnr) -- Keep your existing on_attach logic here (if any) -- Example basic mappings to get you started: vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = bufnr }) vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { buffer = bufnr }) end, root_dir = function(fname) -- Look for standard Solidity project markers first local project_root = require('lspconfig.util').root_pattern( 'hardhat.config.js', 'hardhat.config.ts', 'truffle-config.js', 'foundry.toml', '.git' )(fname) -- Fallback to current working directory if no project markers are found return project_root or vim.fn.getcwd() end, })
3. Address Neovim Version Compatibility
You’re using NVIM v0.7.2, which is a bit outdated. The LSP client in older Neovim versions has known bugs with modern language servers like solidity-ls. If the above steps don’t resolve the issue, consider upgrading to a newer stable version (v0.9+):
sudo add-apt-repository ppa:neovim-ppa/stable sudo apt update sudo apt upgrade neovim
This will fix many underlying compatibility issues.
Final Checks
After updating your config and (optionally) upgrading Neovim:
- Run
:luafile %to reload your config - Open a
.solfile in a valid Solidity project directory (or any directory for testing) - Run
:LspInfoto verify:- The
solidityserver shows as "attached" - The
cmdpath matches what you set - The
root_dirpoints to your project root (or current directory)
- The
If you still hit issues, double-check that your global solidity-ls works by running solidity-ls --version in the terminal—if it throws an error, reinstall it with npm uninstall -g solidity-ls && npm install -g solidity-ls.
内容的提问来源于stack exchange,提问作者Aro Hovhannisyan

