Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check tree_win exists before closing #2904

Closed
wants to merge 2 commits into from

Conversation

kylebeede
Copy link
Contributor

Description

Fixes Vim:E444: Cannot close last window error when after running :NvimTreeClose

Minimal config

vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.cmd([[set packpath=/tmp/nvt-min/site]])
local package_root = '/tmp/nvt-min/site/pack'
local install_path = package_root .. '/packer/start/packer.nvim'
local function load_plugins()
    require('packer').startup({
        {
            'wbthomason/packer.nvim',
            'nvim-tree/nvim-tree.lua',
            'nvim-tree/nvim-web-devicons',
        },
        config = {
            package_root = package_root,
            compile_path = install_path .. '/plugin/packer_compiled.lua',
            display = { non_interactive = true },
        },
    })
end
if vim.fn.isdirectory(install_path) == 0 then
    print('Installing nvim-tree and dependencies.')
    vim.fn.system({ 'git', 'clone', '--depth=1', 'https://github.com/wbthomason/packer.nvim', install_path })
end
load_plugins()
require('packer').sync()
vim.cmd([[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]])
vim.opt.termguicolors = true
vim.opt.cursorline = true

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
    require('nvim-tree').setup({
        view = {
            float = {
                enable = true,
            },
        },
    })
end

Steps to reproduce

  1. nvim .
  2. :NvimTreeOpen
  3. :NvimTreeClose

Current behavior

nvim-tree closes with the following error:

Error executing Lua callback: ...e/pack/packer/start/nvim-tree.lua/lua/nvim-tree/view.lua:230: Vim:E444: Cannot close last window
stack traceback:
        [C]: in function 'nvim_win_close'
        ...e/pack/packer/start/nvim-tree.lua/lua/nvim-tree/view.lua:230: in function 'close'
        ...e/pack/packer/start/nvim-tree.lua/lua/nvim-tree/view.lua:239: in function 'close_this_tab_only'
        ...e/pack/packer/start/nvim-tree.lua/lua/nvim-tree/view.lua:252: in function 'close'
        ...ck/packer/start/nvim-tree.lua/lua/nvim-tree/commands.lua:25: in function <...ck/packer/start/nvim-tree.lua/lua/nvim-tree/commands.lua:24>

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

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

This codepath is very fragile and will be rewritten shortly #2255

There is a change in functionality: the current window (0) will not be closed when tree_win is not available/incorrect, which is it in many cases.

We are going to have to be defensive about this one, making no changes to existing codepaths.

Options:

  1. Check that this is not the last window: only one window open in only one tab.
  2. Make the call safely via a pcall: log a DEBUG message and exit early.

1 may have side-effects, hence we should go with 2.

@alex-courtis
Copy link
Member

Many thanks for isolating this one; issues around there are often very difficult to replicate.

@kylebeede
Copy link
Contributor Author

kylebeede commented Sep 14, 2024

There is a change in functionality: the current window (0) will not be closed when tree_win is not available/incorrect, which is it in many cases.

Thanks @alex-courtis, I didn't realize this - sorry about that.

This codepath is very fragile and will be rewritten shortly #2255

Make the call safely via a pcall: log a DEBUG message and exit early.

I can make this change unless you think it's best to wait for the re-write. I don't see a debug type in nvim-tree.log.types, though.


EDIT: took a crack at it in #2913

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.

2 participants