How to get neovim-lsp work with solc (Solidity)

Hi there,

somehow I do not get nvim v0.6.1 running with lsp support for solidity files. It works for rust, it works for python, it works for typescript, but not for .sol files.

Is there any trick? When doing :LspInfo it shows that no language server is attached to the buffer, although I have a .sol file opened. I have solc in version 0.8.13 installed.

nvim-lspconfig now supports solc out of the box.

Edit: the below is not necessary anymore, they merged my PR adding this to the default configuration.

Note for monorepo users with their contracts in a hardhat package: The default solc lsp server config will pick the git repo’s root dir as the lsp root, which can lead to import errors if importing contracts from node packages like @openzeppelin/contracts. I got nvim to properly detect the root of the contracts package (instead of the monorepo root) for the solc lsp with the following configuration:

local lspconfig = require('lspconfig')

-- some lsp config with on_attach to setup keybindings

lspconfig['solc'].setup {
    on_attach = on_attach,
    root_dir = lspconfig.util.root_pattern('hardhat.config.*', '.git'),
}

This gives a hardhat.config.ts (or .js) file precedence for root dir detection over the git repo root.

I’ve set this all up but don’t appear to be getting any diagnostics in virtual text. Wondering if there’s more I’d need to do:

nvim_lsp.solc.setup{
  cmd = {'solc', '--lsp', '--base-path', './', '--include-path', './lib $(forge remappings)'}
}

Note even vanilla config without path params still gives me no diagnostics.

Edit: LspInfo recognizes it as a compatible Lsp but does not attach to .sol files.

1 Like