ibhagwan / fzf-lua

Improved fzf.vim written in lua
MIT License
2.35k stars 152 forks source link

Bug: ctx with invalid window not cleared #1508

Open tomtomjhj opened 3 weeks ago

tomtomjhj commented 3 weeks ago

RTFM Checklist

Operating system

ubuntu 22.04

Shell

bash

Neovim version (nvim --version)

nightly (b34e137e43)

Fzf version (fzf --version)

0.56.0 (ff16877)

Output of :lua print(os.getenv('FZF_DEFAULT_OPTS'))

--bind alt-a:select-all,alt-d:deselect-all,alt-t:toggle-all --color=dark

Is the problem reproducible with mini.sh?

Fzf-lua configuration

local fzf = require('fzf-lua')

fzf.setup {
  "fzf-native",
  winopts = {
    border = false,
    height = 0.499,
    width = 1,
    row = 1,
    col = 0,
    preview = {
      vertical = "up:45%",
    },
    backdrop = false,
  },
  keymap = {
    fzf = {
      ["ctrl-alt-j"] = "preview-half-page-down",
      ["ctrl-alt-k"] = "preview-half-page-up",
      ["shift-down"] = "preview-down",
      ["shift-up"]   = "preview-up",
      ["ctrl-/"]     = "toggle-preview",
      ["alt-a"]      = "select-all",
      ["alt-d"]      = "deselect-all",
    },
  },
  fzf_opts = {
    ['--border'] = 'top',
  },
  previewers = {
    builtin = { syntax = false },
  },

  -- providers
  default = {
    file_icons = false,
  },
  grep = {
    multiline = 1,
  },
  lsp = {
    includeDeclaration = false,
    symbols = {
      symbol_fmt = function() return "" end,
    },
    code_actions = {
      winopts = {
        row = 1,
      },
    },
  },
}

fzf.register_ui_select()

vim.keymap.set("n", "<leader>b", function() fzf.buffers({ fzf_opts = { ["--layout"] = "default" } }) end)
  nnoremap <buffer><leader>rf    <cmd>lua require('fzf-lua').lsp_references{jump_to_single_result=true}<CR>
  nnoremap <buffer>        <M-]> <cmd>lua require('fzf-lua').lsp_definitions{jump_to_single_result=true}<CR>
  nnoremap <buffer><leader>gd    <cmd>lua require('fzf-lua').lsp_declarations{jump_to_single_result=true}<CR>
  nnoremap <buffer><leader>gy    <cmd>lua require('fzf-lua').lsp_typedefs{jump_to_single_result=true}<CR>
  nnoremap <buffer><leader>gi    <cmd>lua require('fzf-lua').lsp_implementations{jump_to_single_result=true}<CR>
  nnoremap <buffer><leader>ds    <cmd>lua require('fzf-lua').lsp_document_symbols()<CR>
  nnoremap <buffer><leader>sb    <cmd>lua require('fzf-lua').lsp_workspace_symbols()<CR>
  nnoremap <buffer><leader>ac    <cmd>lua require('fzf-lua').lsp_code_actions()<CR>
  nnoremap <buffer><leader>dl    <cmd>lua require('fzf-lua').diagnostics_workspace{severity_limit=3}<CR>
  nnoremap <buffer><leader>DL    <cmd>lua require('fzf-lua').diagnostics_workspace()<CR>

Describe the bug / steps to reproduce

When I ran require('fzf-lua').lsp_references{jump_to_single_result=true}, I got

E5108: Error executing lua /path/to/neovim/runtime/lua/vim/lsp/util.lua:1862: Invalid window id: 1065
stack traceback:
    [C]: in function 'nvim_win_get_buf'
    /path/to/neovim/runtime/lua/vim/lsp/util.lua:1862: in function 'make_position_params'
    ...fzf-lua/lua/fzf-lua/providers/lsp.lua:469: in function 'fn_contents'
    ...fzf-lua/lua/fzf-lua/providers/lsp.lua:643: in function 'lsp_references'
    [string ":lua"]:1: in main chunk

This happens very rarely, so I don't have a good reproducer. Instead, I added this line to see what caused the error when it happens

diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua
index dff0d11..59652f4 100644
--- a/lua/fzf-lua/core.lua
+++ b/lua/fzf-lua/core.lua
@@ -267,6 +267,7 @@ M.CTX = function(includeBuflist)
       bufnr = vim.api.nvim_get_current_buf(),
       bname = vim.api.nvim_buf_get_name(0),
       winid = vim.api.nvim_get_current_win(),
+      traceback = debug.traceback(),
       alt_bufnr = vim.fn.bufnr("#"),
       tabnr = vim.fn.tabpagenr(),
       tabh = vim.api.nvim_win_get_tabpage(0),

Here is the value of __CTX when the error occurred:

{
  alt_bufnr = 109,
  bname = "/path/to/neovim/src/nvim/undo.c",
  bufnr = 1,
  cursor = { 195, 24 },
  curtab_wins = {
    ["1065"] = true
  },
  line = "           exp_uh_next, uhp->uh_next.ptr);",
  mode = "n",
  tabh = 2,
  tabnr = 2,
  traceback = "stack traceback:\n\t/path/to/fzf-lua/lua/fzf-lua/core.lua:270: in function 'CTX'\n\t.../path/to/fzf-lua/lua/fzf-lua/providers/lsp.lua:21: in function 'check_capabilities'\n\t.../path/to/fzf-lua/lua/fzf-lua/providers/lsp.lua:977: in function 'lsp_code_actions'\n\t[string \":lua\"]:1: in main chunk",
  winid = 1065
}

This suggests that after I did something with code action (which I don't remember), the context was not clearly properly.

ibhagwan commented 3 weeks ago

This suggests that after I did something with code action (which I don't remember), the context was not clearly properly.

Most likely, it would help if you have some direction on this, does it happen after a while or sometimes at the first couple of invokes? Does it happen with a specific language server or different ones?

ibhagwan commented 1 day ago

https://github.com/ibhagwan/fzf-lua/commit/a3f66ca0b9787129b13cc6bb491bb20476ba2da9

@tomtomjhj, the above should solve this issue regardless of repro steps.

tomtomjhj commented 18 hours ago

thanks!

ibhagwan commented 9 hours ago

@tomtomjhj, revered due to https://github.com/ibhagwan/fzf-lua/commit/a3f66ca0b9787129b13cc6bb491bb20476ba2da9#commitcomment-149465672, I wasn’t too happy with this “fix” anyway as it wasn’t addressing the exact conditions causing the CTX to not be cleared.

Will await accurate reproduction conditions if you can provide such.