From 816b56f878f0291c00a9018d5057b7b2b00f1891 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 7 Feb 2024 13:05:33 +0000 Subject: [PATCH] fix(lsp): cancel watchers when closing a client --- runtime/lua/vim/lsp/_watchfiles.lua | 9 +++++++++ runtime/lua/vim/lsp/client.lua | 1 + test/functional/plugin/lsp_spec.lua | 4 +++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/runtime/lua/vim/lsp/_watchfiles.lua b/runtime/lua/vim/lsp/_watchfiles.lua index 220052be5f..c66a76feae 100644 --- a/runtime/lua/vim/lsp/_watchfiles.lua +++ b/runtime/lua/vim/lsp/_watchfiles.lua @@ -167,5 +167,14 @@ function M.unregister(unreg, ctx) end end +--- @param client_id integer +function M.cancel(client_id) + for _, reg_cancels in pairs(cancels[client_id]) do + for _, cancel in pairs(reg_cancels) do + cancel() + end + end +end + return M diff --git a/runtime/lua/vim/lsp/client.lua b/runtime/lua/vim/lsp/client.lua index c1b41a7183..1e709a1992 100644 --- a/runtime/lua/vim/lsp/client.lua +++ b/runtime/lua/vim/lsp/client.lua @@ -727,6 +727,7 @@ function Client:_stop(force) rpc.terminate() self._graceful_shutdown_failed = true end + vim.lsp._watchfiles.cancel(self.id) end) end diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 79fa4786d2..1610351090 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -4573,6 +4573,8 @@ describe('LSP', function() wait_for_message() + vim.lsp.stop_client(client_id) + return server.messages ]], root_dir, @@ -4581,7 +4583,7 @@ describe('LSP', function() local uri = vim.uri_from_fname(root_dir .. '/watch') - eq(4, #result) + eq(6, #result) eq({ method = 'workspace/didChangeWatchedFiles',