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: UNKNOWN PLUGIN error resulting from unloaded buffers #931

Merged

Conversation

prettymuchbryce
Copy link
Contributor

Problem

Currently when checking if a buffer is valid, we use the function 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.

@prettymuchbryce prettymuchbryce changed the title Fix UNKNOWN PLUGIN error resulting from unloaded buffers [fix] UNKNOWN PLUGIN error resulting from unloaded buffers Jun 24, 2024
@prettymuchbryce prettymuchbryce changed the title [fix] UNKNOWN PLUGIN error resulting from unloaded buffers fix: UNKNOWN PLUGIN error resulting from unloaded buffers Jun 24, 2024
Copy link
Owner

@akinsho akinsho left a comment

Choose a reason for hiding this comment

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

Thanks for taking another look at this. I think this looks good in general, left some comments about tweaking the implementation to avoid extra looping/nicer (IMO) semantics

lua/bufferline/buffers.lua Outdated Show resolved Hide resolved
---@return integer[]
function M.get_valid_buffers() return vim.tbl_filter(M.is_valid, vim.api.nvim_list_bufs()) end
---@return table[]
function M.get_valid_buffers() return vim.tbl_filter(M.is_valid, vim.fn.getbufinfo()) end
Copy link
Owner

Choose a reason for hiding this comment

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

I think you could use vim.iter here like

local bufs = vim.iter(vim.api.nvim_list_bufs()):filter(vim.api.nvim_buf_is_loaded)

or to maintain backwards compat for people who haven't migrated could do a loop and rather than filter map vim.fn.getbufinfo() and only add the buffer number to the resulting list if it is valid

@akinsho
Copy link
Owner

akinsho commented Jun 24, 2024

LGTM thanks for tweaking that 👍🏾

@akinsho akinsho merged commit 1662fed into akinsho:main Jun 24, 2024
1 check passed
sstallion pushed a commit to sstallion/bufferline.nvim that referenced this pull request Jul 3, 2024
* Fix UNKNOWN PLUGIN error resulting from unloaded buffers

* Maintain get_valid_buffers signature
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