From 76df000467713a55bb011cd273386dd36e7642f0 Mon Sep 17 00:00:00 2001 From: Cedric Koch-Hofer Date: Mon, 4 May 2026 09:43:32 +0000 Subject: [PATCH] fix: codelens.enable() nil error and GoListImports crash on empty result Two independent bug fixes: 1. codelens: replace non-existent vim.lsp.codelens.enable() with refresh() vim.lsp.codelens.enable() does not exist in Neovim 0.11.x (available API: refresh, clear, run, get, display, save). Commit c8a356b introduced a regression by replacing the working: vim.lsp.codelens.refresh({ bufnr = 0 }) with: vim.lsp.codelens.enable(true, { bufnr = 0 }) causing the following error on every BufWritePre for *.go files when lsp_codelens = true: Error executing lua callback: ...go/codelens.lua:59: attempt to call field 'enable' (a nil value) 2. GoListImports: guard against empty import list When gopls returns no PackageImports, the code fell through to vim.lsp.util.open_floating_preview() with height = 0, triggering a spurious error. Add an early return with an INFO notification instead. --- lua/go/codelens.lua | 2 +- lua/go/commands.lua | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lua/go/codelens.lua b/lua/go/codelens.lua index 77adcdcf7..fd5005ae2 100644 --- a/lua/go/codelens.lua +++ b/lua/go/codelens.lua @@ -56,7 +56,7 @@ function M.refresh() return end if _GO_NVIM_CFG.lsp_codelens == true then - vim.lsp.codelens.enable(true, { bufnr = 0 }) + vim.lsp.codelens.refresh({ bufnr = 0 }) else log('refresh codelens') vim.lsp.codelens.clear(gopls.id, 0) diff --git a/lua/go/commands.lua b/lua/go/commands.lua index 1bd062d0b..e97f3ef0b 100644 --- a/lua/go/commands.lua +++ b/lua/go/commands.lua @@ -521,6 +521,11 @@ return { create_cmd('GoListImports', function(_) local lines = require('go.gopls').list_imports().PackageImports or {} + if #lines == 0 then + vim.notify('GoListImports: no imports found', vim.log.levels.INFO) + return + end + local close_events = { 'CursorMoved', 'CursorMovedI', 'BufHidden', 'InsertCharPre' } local config = { close_events = close_events,