folke / trouble.nvim

🚦 A pretty diagnostics, references, telescope results, quickfix and location list to help you solve all the trouble your code is causing.
Apache License 2.0
5.36k stars 175 forks source link

bug: Error on exit buffer when trouble list is opened #284

Closed xu-cheng closed 1 year ago

xu-cheng commented 1 year ago

Did you check docs and existing issues?

Neovim version (nvim -v)

NVIM v0.9.0

Operating system/version

macOS 13.4

Describe the bug

We observe the following error when quit the last buffer and trouble list is opened

Error detected while processing BufEnter Autocommands for "<buffer=20>":
E5108: Error executing lua ...mp/test/.repro/plugins/trouble.nvim/lua/trouble/view.lua:338: Invalid window
 id: 1000
stack traceback:
        [C]: in function 'nvim_set_current_win'
        ...mp/test/.repro/plugins/trouble.nvim/lua/trouble/view.lua:338: in function 'on_enter'
        ...mp/test/.repro/plugins/trouble.nvim/lua/trouble/init.lua:204: in function 'action'
        [string ":lua"]:1: in main chunk

There are multiple ways to reproduce this bug:

A similar issue is reported at #253.

Steps To Reproduce

Case 1

  1. Load repro.
  2. Open a file.
  3. Run :Trouble to open the trouble list.
  4. Change focus back to the buffer.
  5. Run :q to close the buffer.

Case 2

  1. Change the repro to remove the noice plugin.
  2. Load repro.
  3. Open a file and make some changes without saving.
  4. Run :Trouble to open the trouble list.
  5. Change focus back to the buffer.
  6. Run :q to close the buffer.
  7. Vim will show Press ENTER or type command to continue. Press Enter.

Expected Behavior

The vim quits without error when closing last buffer.

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  "folke/trouble.nvim",
    {
        "folke/noice.nvim",
        dependencies = {
            "MunifTanjim/nui.nvim",
            "rcarriga/nvim-notify",
        },
    },
}

require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

require("trouble").setup({})
require("noice").setup({}) -- remove this line in the second case
cyaconi commented 1 year ago

Same here, exactly same stack traceback.

xu-cheng commented 1 year ago

@folke I want to report that the latest changes don't fix the problem. If you try the original repro, you will see the problem persists except with a slight different error message:

Error message in case 1:

Error detected while processing BufEnter Autocommands for "<buffer=20>":
E5108: Error executing lua ...mp/test/.repro/plugins/trouble.nvim/lua/trouble/view.lua:341: Vim:E444
: Cannot close last window
stack traceback:
        [C]: in function 'nvim_win_close'
        ...mp/test/.repro/plugins/trouble.nvim/lua/trouble/view.lua:341: in function 'on_enter'
        ...mp/test/.repro/plugins/trouble.nvim/lua/trouble/init.lua:220: in function 'action'
        [string ":lua"]:1: in main chunk

Error message in case 2:

Error detected while processing BufEnter Autocommands for "*":
E5108: Error executing lua vim/_editor.lua:0: BufEnter Autocommands for "*"..script nvim_exec2() cal
led at BufEnter Autocommands for "*":0: Vim(quit):E37: No write since last change
stack traceback:
        [C]: in function 'nvim_exec2'
        vim/_editor.lua: in function 'cmd'
        ...mp/test/.repro/plugins/trouble.nvim/lua/trouble/view.lua:271: in function 'on_win_enter'
        ...mp/test/.repro/plugins/trouble.nvim/lua/trouble/init.lua:189: in function 'action'
        [string ":lua"]:1: in main chunk

The latest commit is used in the test:

 Loaded (6)
~             ● lazy.nvim 1.38ms ο„‘  init.lua
~             ● noice.nvim 0.4ms ξ«“ start
~             ● nui.nvim 0.15ms ο’‡  noice.nvim
~             ● nvim-notify 0.12ms ο’‡  noice.nvim
~             ● tokyonight.nvim 0.16ms ξ«“ start
~             ● trouble.nvim 0.14ms ξ«“ start
~                 dir    /private/tmp/test/.repro/plugins/trouble.nvim
~                 url    https://github.com/folke/trouble.nvim
~                 branch main
~                 commit 23c1327
~                 readme README.md
~                 help   |trouble.nvim.txt|
~
waferbaby commented 1 year ago

Just leaving a comment to say I get the exact same thing (but only if I have Trouble set to auto-open, which would be my preference).

hanspinckaers commented 1 year ago

For people Googling; I don't know if this is a valid/logical fix. But this works for me:

diff --git a/lua/trouble/renderer.lua b/lua/trouble/renderer.lua
index 1c1ff07..1332ae1 100644
--- a/lua/trouble/renderer.lua
+++ b/lua/trouble/renderer.lua
@@ -33,7 +33,11 @@ end
 ---@param view TroubleView
 function renderer.render(view, opts)
   opts = opts or {}
-  local buf = vim.api.nvim_win_get_buf(view.parent)
+  local win_id = view.parent
+  if not vim.api.nvim_win_is_valid(win_id) then
+    return
+  end
+  local buf = vim.api.nvim_win_get_buf(win_id)
   providers.get(view.parent, buf, function(items, messages)
     local auto_jump = vim.tbl_contains(config.options.auto_jump, opts.mode)
     if opts.on_open and #items == 1 and auto_jump and not opts.auto then
diff --git a/lua/trouble/view.lua b/lua/trouble/view.lua
index 69a1b71..66c0568 100644
--- a/lua/trouble/view.lua
+++ b/lua/trouble/view.lua
@@ -338,13 +338,16 @@ function View:close()
     if vim.api.nvim_win_is_valid(self.parent) then
       vim.api.nvim_set_current_win(self.parent)
     end
-    vim.api.nvim_win_close(self.win, {})
+    if #vim.api.nvim_list_wins() > 2 then
+      vim.api.nvim_win_close(self.win, {})
+    else
+      vim.cmd('quit')
+    end
   end
   if vim.api.nvim_buf_is_valid(self.buf) then
     vim.api.nvim_buf_delete(self.buf, {})
   end
 end
-
 function View.create(opts)
   opts = opts or {}
   if opts.win then