How to prevent an LSP client from getting attached multiple times

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
    augroup packer_user_config
      autocmd BufWritePost init.lua source <afile> | PackerCompile
    augroup end

return packer.startup(function(use)
  use 'wbthomason/packer.nvim'

  use {
    requires = {'neovim/nvim-lspconfig'},
    config = function()

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>

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

After some trial and error, found that calling LspStop after PackerCompile using autocmd solves the issue. If anyone is having the issue just update the augroup in the thread description as follows:

  augroup packer_user_config
    autocmd BufWritePost plugins.lua source <afile> | PackerCompile
    autocmd User PackerCompileDone LspStop
  augroup end

I still consider this as just a workaround for now, if anyone knows a proper solution kindly let me know. Thanks.

use packer’s bootstrap script provided on its repository.

Hi. Bootstrapping section mentioned in the packer’s readme is about installing packer for the first time, right? How does it solve the above mentioned issue?

By the way, one additional information I’d like to provide is that the issue disappeared when I made my nvim config directory (~/.config/nvim) a git repo (i.e. without adding LspStop autocommand).