From 00889948ddabcf1bb488665d6afe52c50506a34c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 19 Dec 2021 13:24:08 -0800 Subject: [PATCH] fix(lsp): avoid attaching to unloaded buffers (#16726) Closes https://github.com/neovim/neovim/issues/16562 https://github.com/neovim/neovim/issues/16249 https://github.com/neovim/neovim/issues/16297 * buf_attach_client can be called on an unloaded buffer * on_attach will prematurely fail, while the language server client tracks this buffer as attached * The language server client will track this buffer as attached despite textDocument/didChange notifications not being sent to the server * Instead, check if the buffer is loaded and return early, warning via the lsp logger that buf_attach_client was called on an invalid buffer (cherry picked from commit 6d63cb8f6a4fb76ae654f252f315866a310b8f16) Co-authored-by: Michael Lingelbach --- runtime/lua/vim/lsp.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index dbbfd7d1d8..5a9d63a51f 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -1155,6 +1155,12 @@ function lsp.buf_attach_client(bufnr, client_id) client_id = {client_id, 'n'}; } bufnr = resolve_bufnr(bufnr) + if not vim.api.nvim_buf_is_loaded(bufnr) then + local _ = log.warn() and log.warn( + string.format("buf_attach_client called on unloaded buffer (id: %d): ", bufnr) + ) + return false + end local buffer_client_ids = all_buffer_active_clients[bufnr] -- This is our first time attaching to this buffer. if not buffer_client_ids then