From a673c83198c87eb0a4b9bb3b0335a8fb4c835a93 Mon Sep 17 00:00:00 2001 From: Diomendius <42310725+Diomendius@users.noreply.github.com> Date: Sat, 5 Oct 2024 22:38:52 +1300 Subject: [PATCH] fix: check buffer validity try_add()/try_add_wrapper() Currently avoids an issue where a `FileType` autocommand schedules a call to one of these functions but the buffer gets wiped out before the next event loop. `nvim_get_option_value()` with the `filetype` argument can cause this as it creates a transient buffer that only lasts for a single function call. --- lua/lspconfig/manager.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lua/lspconfig/manager.lua b/lua/lspconfig/manager.lua index 3457014854..51b145c636 100644 --- a/lua/lspconfig/manager.lua +++ b/lua/lspconfig/manager.lua @@ -244,6 +244,10 @@ end function M:try_add(bufnr, project_root) bufnr = bufnr or api.nvim_get_current_buf() + if not api.nvim_buf_is_valid(bufnr) then + return + end + if vim.bo[bufnr].buftype == 'nofile' then return end @@ -294,6 +298,10 @@ end --- @param bufnr integer --- @param project_root? string function M:try_add_wrapper(bufnr, project_root) + if not api.nvim_buf_is_valid(bufnr) then + return + end + local config = self.config -- `config.filetypes = nil` means all filetypes are valid. if not config.filetypes or vim.tbl_contains(config.filetypes, vim.bo[bufnr].filetype) then