You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Neovim中无法为.sol文件挂载solidity-lsp的问题求助

Troubleshooting Solidity-LSP Mount Issues in Neovim (v0.7.2, Ubuntu 20.04)

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-ls in your terminal to get the full path (e.g., /usr/local/bin/solidity-ls)
  • In your LSP config for solidity, explicitly set the cmd option 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:

  1. Run :luafile % to reload your config
  2. Open a .sol file in a valid Solidity project directory (or any directory for testing)
  3. Run :LspInfo to verify:
    • The solidity server shows as "attached"
    • The cmd path matches what you set
    • The root_dir points to your project root (or current directory)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 17:50:23