From 56f0bb258ef959595e0bd7fc2dba0c26127a844a Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 16 Mar 2026 01:04:37 -0700 Subject: [PATCH] fix(lsp): handle errors and add missing client filters (#6900) - Add nil-check when no LSP client is found matching the filter - Add error handling with pcall for exec_cmd failures - Add client filter to typescript.goToSourceDefinition and typescript.findAllFileReferences - Show warning notification when command execution fails or client not found This fixes the issue where LSP commands would fail silently or cause crashes when executing on unfiltered clients, and where irrelevant LSP clients (like Copilot) would report errors for unsupported commands. --- lua/lazyvim/plugins/extras/lang/typescript.lua | 2 ++ lua/lazyvim/util/lsp.lua | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lua/lazyvim/plugins/extras/lang/typescript.lua b/lua/lazyvim/plugins/extras/lang/typescript.lua index bc91e830..9ad1a71d 100644 --- a/lua/lazyvim/plugins/extras/lang/typescript.lua +++ b/lua/lazyvim/plugins/extras/lang/typescript.lua @@ -72,6 +72,7 @@ return { local win = vim.api.nvim_get_current_win() local params = vim.lsp.util.make_position_params(win, "utf-16") LazyVim.lsp.execute({ + filter = "vtsls", command = "typescript.goToSourceDefinition", arguments = { params.textDocument.uri, params.position }, open = true, @@ -83,6 +84,7 @@ return { "gR", function() LazyVim.lsp.execute({ + filter = "vtsls", command = "typescript.findAllFileReferences", arguments = { vim.uri_from_bufnr(0) }, open = true, diff --git a/lua/lazyvim/util/lsp.lua b/lua/lazyvim/util/lsp.lua index ff248599..713f82f4 100644 --- a/lua/lazyvim/util/lsp.lua +++ b/lua/lazyvim/util/lsp.lua @@ -76,7 +76,14 @@ function M.execute(opts) local buf = vim.api.nvim_get_current_buf() ---@cast filter vim.lsp.get_clients.Filter - local client = vim.lsp.get_clients(LazyVim.merge({}, filter, { bufnr = buf }))[1] + local clients = vim.lsp.get_clients(LazyVim.merge({}, filter, { bufnr = buf })) + local client = clients[1] + + if not client then + local filter_name = type(opts.filter) == "string" and opts.filter or (filter.name or "any") + LazyVim.warn("No LSP client found for filter: " .. filter_name, { title = "LSP Execute" }) + return + end local params = { command = opts.command, @@ -89,7 +96,14 @@ function M.execute(opts) }) else vim.list_extend(params, { title = opts.title }) - return client:exec_cmd(params, { bufnr = buf }, opts.handler) + local ok, result = pcall(function() + return client:exec_cmd(params, { bufnr = buf }, opts.handler) + end) + if not ok then + LazyVim.warn("Failed to execute command '" .. opts.command .. "': " .. tostring(result), { title = "LSP Execute" }) + return + end + return result end end