LSP config error: module lspconfig not found

Hi, I’m trying to configure rust_analyzer LSP server. As per the docs, I had the following in my ~/.config/nvim/init.vim, but it isnt working:

lua << EOF
local nvim_lsp = require'lspconfig'

local on_attach = function(client)
    require'completion'.on_attach(client)
end

nvim_lsp.rust_analyzer.setup({
    on_attach=on_attach,
    settings = {
        ["rust-analyzer"] = {
            assist = {
                importGranularity = "module",
                importPrefix = "by_self",
            },
            cargo = {
                loadOutDirsFromCheck = true
            },
            procMacro = {
                enable = true
            },
        }
    }
})
EOF

Here is a screenshot of the error message I’m getting:

I’m getting the same error if I put these lines in ~/.config/nvim/config/init.vim. What am I missing ?

Seems like you don’t have lspconfig installed.

I think it is installed? Because running :help lspconfig while in nvim opens up what its supposed to:

I also verified that its installed using DeinUI plugin GitHub - wsdjeg/dein-ui.vim: UI for Shougo's dein.vim

I’m not really familiar with that plugin. What I can tell you, is that our example configuration works fine when installed via vim-plug, packer.nvim, or paq.

Maybe you have lspconfig lazy-loaded?

Thats quite possible. I’m able to install the LSP servers using :LspInstall command. I think I will start with a fresh configuration. Thanks for the responses!

Hello @jayaura , were you able to resolve this issue. I am struggling with same things when I am trying to implement lspconfig in my init.vim

I found what I was doing wrong.

Any call to a plugin such as lua require('<plugin_name>') should be after call plug#end()

1 Like

I appreciate you following up on this. Been struggling the last hour with the same issue. Never would have guessed this was the solution. Thank you!

It looks like you are using a mix of vimscript and lua… like me while I move things to lua over time. That said, make sure you init.vim or whatever you are using as an entry point to your configuration loads the plugins first. My hybrid config is sensitive to making sure I only depend on lspconfig once it has been loaded.

That need, introduces an issue of how to share the on_attach and capabilities properties. This is required because anytime you write to setup you overwrite the previous value; no merging, just overwrite.

Here are a couple of snippets that may be of use. Others please feel free to comment/improve.

" ------------------------------------------------------------------------------
" ~/dotfiles/init.vim
" symlinked to: ~/.config/nvim/config/init.vim
" last updated: June 7, 2023
" ------------------------------------------------------------------------------
" Note: Sequence of sourcing matters.
" ------------------------------------------------------------------------------
source $HOME/dotfiles/nvim-plugins.vim
source $HOME/dotfiles/nvim-functions.vim
source $HOME/dotfiles/nvim-other.vim
" Lua files
source $HOME/dotfiles/nvim-mason.lua
" -- non-lsp plugin configs
source $HOME/dotfiles/nvim-cmp-npm.lua
source $HOME/dotfiles/nvim-lualine.lua
source $HOME/dotfiles/nvim-cmd.lua
source $HOME/dotfiles/nvim-treesitter.lua

" -- lspconfig is defined here
source $HOME/dotfiles/nvim-lsp.lua

" -- require lspconfig
source $HOME/dotfiles/nvim-rust-tools.lua
source $HOME/dotfiles/nvim-yamlls.lua
source $HOME/dotfiles/nvim-tsserver.lua
source $HOME/dotfiles/nvim-eslint.lua

" -- bindings (also see .config/nvim/lua/user for handlers and env
source $HOME/dotfiles/nvim-bindings.vim
source $HOME/dotfiles/nvim-emoji-bindings.vim
source $HOME/dotfiles/nvim-typo-db.vim

" Options written in lua
source $HOME/dotfiles/nvim-options.lua

Where I setup lspconfig and where I require lspconfig I also import the env and handlers to include in the setup

// lua/user/env
local M = {}

M.capabilities = require("cmp_nvim_lsp").default_capabilities(
    vim.lsp.protocol.make_client_capabilities()
)

return M

Similarly, I have a handlers module for specifying the shared keybindings for the lsp features.