nvim-neo-tree / neo-tree.nvim

Neovim plugin to manage the file system and other tree like structures.
MIT License
3.92k stars 226 forks source link

BUG: neotree directory change error after auto-closing a tab #1067

Closed Emm54321 closed 2 days ago

Emm54321 commented 1 year ago

Did you check docs and existing issues?

Neovim Version (nvim -v)

v0.10.0-dev-679+gb60a2ab4c

Operating System / Version

debian sid

Describe the Bug

I get the following error after deleting a buffer in a tab from a TermClose autocmd when autochdir is set: [Neo-tree ERROR] Error in event handler for event vim_dir_changed[buffers.vim_dir_changed]: ...nvim/lazy/neo-tree.nvim/lua/neo-tree/sources/manager.lua:296: Invalid tabpage id: 2

Screenshots, Traceback

No response

Steps to Reproduce

  1. mkdir dir && touch file1 && touch dir/file2
  2. nvim -u repro.lua
  3. :Neotree
  4. :edit file1
  5. :edit dir/file2
  6. :tabnew term://bash
  7. enter 'exit' in the shell, which closes its tab due to the autocmd in repro.lua
  8. use :bn once or twice to switch between file buffers

Expected Behavior

No error, directory changed normally.

Your Configuration

-- 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",
  -- add any other plugins here
}

local neotree_config = {
  "nvim-neo-tree/neo-tree.nvim",
  dependencies = { "MunifTanjim/nui.nvim", "nvim-tree/nvim-web-devicons", "nvim-lua/plenary.nvim" },
  cmd = { "Neotree" },
  keys = {
    { "<Leader>e", "<Cmd>Neotree<CR>" }, -- change or remove this line if relevant.
  },
  opts = {
    -- Your config here
    -- ...
  },
}

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

vim.cmd.colorscheme("tokyonight")
-- add anything else here
vim.o.autochdir = true
vim.cmd([[au TermClose * bdelete]])
instableorder commented 9 months ago

I'm getting the same error with a slightly different use-case. This is the most minimal example I could come up with:

The error only appears if the neotree pane has focus when the tab is closed. If the focus is on the empty pane, there's no error (i.e. Neotree show instead of Neotree toggle). A bit further digging revealed that the TabClosed autocmd is not triggered when the error appears, so the cleanup that comes with that won't happen, hence the error.

Neovim version: 0.9.5 Configuration:

local lazypath = vim.fn.stdpath("data") .. "/lazy/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", "--branch=stable", lazypath })

end
vim.opt.rtp:prepend(lazypath)

-- require 'config.loadplugins'
require("lazy").setup({
  {
    'nvim-neo-tree/neo-tree.nvim',
    branch = 'v3.x',
    dependencies = {
      "nvim-lua/plenary.nvim",
      "nvim-tree/nvim-web-devicons",
      "MunifTanjim/nui.nvim"
    },
  },
})
andrevmatos commented 1 month ago

I'm also being hit by this one.

For me, it's caused by a vim.cmd.bdelete I was using in an autocmd TermClosed when lazygit term exited (to clear the buffer without the exit 0 msg), caused TabClose event to not be fired (even though the tab is gone when the buffer is deleted).

The fix for me was to replace the bdelete with vim.api.nvim_feedkeys(vim.keycode("<CR>"), "n", false), which also dismissed that message but allowed the TabClose event to be fired and neo-tree state be cleared.

If you can't adjust your intended behavior like that, alternatively one can add the require("neo-tree.sources.manager").dispose_invalid_tabs() near/after you deleted the buffer. This should also make sure neo-tree state (at least for this) remains consistent after a tab is closed even without the TabClose event.