Skip to content

fix: codelens.enable() nil error and GoListImports crash on empty result - #616

Merged
ray-x merged 1 commit into
ray-x:masterfrom
kanard38:fix/codelens-enable-nil-nvim-0.11
May 4, 2026
Merged

fix: codelens.enable() nil error and GoListImports crash on empty result#616
ray-x merged 1 commit into
ray-x:masterfrom
kanard38:fix/codelens-enable-nil-nvim-0.11

Conversation

@kanard38

@kanard38 kanard38 commented May 4, 2026

Copy link
Copy Markdown
Contributor

Two independent bug fixes for Neovim 0.11.x compatibility and a spurious error in GoListImports.

Bug 1: BufWritePre error — attempt to call field 'enable' (a nil value)

When lsp_codelens = true, saving any .go file triggers:

Error executing lua callback: ...go/codelens.lua:59: attempt to call field 'enable' (a nil value)

vim.lsp.codelens.enable() does not exist in Neovim 0.11.x.
The available API is: refresh, clear, run, get, display, save.

Commit c8a356b introduced this regression by replacing the working:

vim.lsp.codelens.refresh({ bufnr = 0 })

with:

vim.lsp.codelens.enable(true, { bufnr = 0 })

This PR reverts to the working refresh() call.

Bug 2: GoListImports spurious error on files with no imports

When gopls returns an empty import list, the code fell through to vim.lsp.util.open_floating_preview() with height = 0, producing a spurious error. An early return with an INFO notification is added instead.


Changes

File Change
lua/go/codelens.lua Replace vim.lsp.codelens.enable() with vim.lsp.codelens.refresh({ bufnr = 0 })
lua/go/commands.lua Guard GoListImports against empty result before opening floating preview

Reproduction Steps

Bug 1: Set lsp_codelens = true, open any .go file, save it → error fires on BufWritePre:

Error executing lua callback: ...go/codelens.lua:59: attempt to call field 'enable' (a nil value)

Bug 2: Run :GoListImports on a file with no imports → spurious floating window error.

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.

@ray-x ray-x left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ray-x
ray-x merged commit 223500c into ray-x:master May 4, 2026
2 checks passed
@Kazuto

Kazuto commented May 31, 2026

Copy link
Copy Markdown

This fix has somehow been overwritten again in commit 7542616, breaking codelense again. Had to pin it to commit 223500c to fix it otherwise the plugin is unusable on neovim 0.11.*

@ray-x

ray-x commented Jun 1, 2026

Copy link
Copy Markdown
Owner

A release tag for nvim 0.11 is available: v0.11. Alternatively, you might use something like this in your config for old versions:

      vim.lsp.codelens.enable = function(...)
        vim.lsp.codelens.refresh(...)
      end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants