Steps to reproduce
:mksession test.vim
:source test.vim
Current result
Session loaded with the following error:
|| Error detected while processing SessionLoadPost Autocommands for "*":
|| E5108: Error executing lua ...im/pack/plugins/opt/nvim-tree.lua/lua/nvim-tree/view.lua:92: Vim:E813: Cannot close autocmd window
Expected result
Session loaded without any errors.
I use :mksession myself, and I never encounter this problem. This only happens when:
I don't think it's reasonable to put the burden of handling all strange oddities that can occur when sourcing a session while you already have buffers open, on NvimTree.
To cleanly source a session when you have buffers open you should first wipe all existing buffers. A way to do this is to use something like this:
function! WipeAll()
let i = 0
let n = bufnr("$")
while i < n
let i = i + 1
if bufexists(i)
execute("bw " . i)
endif
endwhile
endfunction
We could wrap the call to nvim_win_close in a pcall to swallow the error, but loading a session with an nvimtree buffer while an nvimtree buffer already exists would still throw E95: Buffer with this name already exists.
An NvimTree buffer has been saved in the session
You try to source the session while an NvimTree buffer already exists.
No, I can reproduce it with an empty buffer just after start... Are you loading NvimTree dynamically?
Really? That's surprising to me, and I can't reproduce that on my end. No, I am not lazy loading nvimtree.
Is it possible that the session is being sourced more than once?
Is it possible that the session is being sourced more than once?
No, I just start Neovim and enter these commands right away... Will try to debug what can cause the issue.
Never mind, I believe I understand. NvimTree creates its buffer on init, even before opening it. So manually sourcing a session will make nvimtree try to close any pre-existing tree windows. But since the tree wasn't open, vim will open it in an autocmd window. These are special windows that can't be closed.
I wasn't seeing this behavior because I automatically source my sessions from init.vim and that happens before nvimtree has initialized its buffer.
Oh, got it.
How we can handle it?
No great idea off the top of my head, unfortunately. And I don't have time to look into it right now. But I believe I have time to look into this tomorrow.
- An NvimTree buffer has been saved in the session
This is not an issue, session loads correctly in this case, just without NvimTree buffer.
- You try to source the session while an NvimTree buffer already exists.
But this causes the issue.
To cleanly source a session when you have buffers open you should first wipe all existing buffers. A way to do this is to use something like this:
Thank you for the workaround. I decided to write a small session wrapper that wipes all bufers before start a new session with other useful automation.
Most helpful comment
No great idea off the top of my head, unfortunately. And I don't have time to look into it right now. But I believe I have time to look into this tomorrow.