akinsho / bufferline.nvim

A snazzy bufferline for Neovim
GNU General Public License v3.0
3.54k stars 197 forks source link

fix: UNKNOWN PLUGIN error resulting from unloaded buffers #931

Closed prettymuchbryce closed 4 months ago

prettymuchbryce commented 4 months ago

Problem

Currently when checking if a buffer is valid, we use the function [nvim_buf_is_valid](https://neovim.io/doc/user/api.html#nvim_buf_is_valid()), however there is an edge case here described in the documentation where a buffer could have been unloaded, but still be "valid". If a buffer is unloaded and valid then referencing buf.buflisted could result in a (UNKNOWN PLUGIN): Error executing lua: attempt to call a number value error. I believe this issue may be a bug in Neovim itself and I have attempted to get more clarity on this here.

This issue reproduced in my config when bufferline attempted to reference the buf.buflisted property of unloaded nvim-tree buffer.

Note: A previous attempt at this fix (#928) attempted to address the issue by changing the call to nvim_buf_is_valid to nvim_buf_is_loaded. While this solves the problem defined above, it had the side effect of breaking users of various session plugins. This is because session plugins utilize unloaded buffers which are also listed (i.e. calling .buflisted on these buffers returns 1).

Solution

Instead of utilizing buf.buflisted, which can potentially result in UNKNOWN PLUGIN errors for certain unloaded buffers, we utilize vim.fn.getbufinfo which returns a list of info tables for all buffers. This table contains a property listed which is 0 if the buffer is unlisted, and 1 if the buffer is listed. This allows us to check whether the unloaded buffer is listed, without referencing buf.buflisted directly.

I have locally verified that this solution both resolves the UNKNOWN PLUGIN errors, and supports loading "hidden" session buffers via the posession plugin.

akinsho commented 4 months ago

LGTM thanks for tweaking that 👍🏾