Hi. I’m using packer with autcommand to run PackerCompile on updating the config file. But this is also causing lsp client to attach again every time the file is updated.
How to reproduce:
local status_ok, packer = pcall(require, 'packer')
if status_ok then
vim.cmd([[
augroup packer_user_config
autocmd!
autocmd BufWritePost init.lua source <afile> | PackerCompile
augroup end
]])
end
return packer.startup(function(use)
use 'wbthomason/packer.nvim'
use {
'williamboman/nvim-lsp-installer',
requires = {'neovim/nvim-lspconfig'},
config = function()
require('nvim-lsp-installer').setup()
require('lspconfig')['sumneko_lua'].setup({})
end,
}
end)
Now try running LspInfo
before and after making any changes to the file, we can see that sumneko_lua
getting reattached every time.
What all I have tried so far:
I found this issue since I was seeing the diagnostics count getting doubled on every write, which took me some to figure out the actual issue. I tried going through the issues list of neovim, lsp-installer and nvim-lspconfig, couldn’t find any related issues. Surprisingly couldn’t find any related topics online in general (I wonder why this is the case given that packer and lspconfig are so popular!).
I also found that reloading the file using :edit
will leave us with only one client attached as desired. So I added an autocommand as a workaround to reload file after PackerCompile
using :autocmd User PackerCompileDone edit
. This almost seemed like solving the issue, but ended up causing some other error on running PackerSync
as follows:
[packer.nvim] [WARN 23:15:22] clean.lua:79: Cleaning cancelled!
Error executing vim.schedule lua callback: ...ite/pack/packer/start/packer.nvim/lua/packer/display.lua:247: bad argument #2 to 'sub' (number expected, got nil)
stack traceback:
[C]: in function 'sub'
...ite/pack/packer/start/packer.nvim/lua/packer/display.lua:247: in function ''
vim/_editor.lua: in function <vim/_editor.lua:0>
Error executing vim.schedule lua callback: ...ite/pack/packer/start/packer.nvim/lua/packer/display.lua:215: Index out of bounds
stack traceback:
[C]: in function 'nvim_buf_set_lines'
...ite/pack/packer/start/packer.nvim/lua/packer/display.lua:215: in function 'set_lines'
...ite/pack/packer/start/packer.nvim/lua/packer/display.lua:531: in function ''
vim/_editor.lua: in function <vim/_editor.lua:0>
Question
I guess conditionally loading lsp client setup might fix the issue. So how do I check if a client is already attached and invoke setup only if it hasn’t?
PS: I’m new to the neovim ecosystem in general. I copied the error messages following the suggestion here: error - Copy vim messages into clipboard - Vi and Vim Stack Exchange. Please let me know if any extra details have to be provided, thanks.
Neovim version: 0.7.2