fix(lsp): show_document() reports a failed buffer load #41731

Problem:
vim.lsp.util.show_document() can fail to load the target buffer, most
visibly with E325 when another process owns the file's swapfile and the
user declines to open it. The Vim error escapes show_document() and
surfaces inside whichever LSP handler called it, instead of being
reported as a failure to show the document.

Solution:
Load the buffer up front with vim.fn.bufload(), before any of that
state is touched, and report the error instead of raising.

AI-assisted
This commit is contained in:
Volodymyr Chernetskyi
2026-09-07 18:21:11 +02:00
committed by GitHub
parent 65ef6fdaee
commit ce5e230af8

View File

@@ -876,6 +876,13 @@ function M.show_document(location, position_encoding, opts)
end
local bufnr = vim.uri_to_bufnr(uri)
-- Return early if the buffer fails to load, to avoid partial setup.
local loaded, err = pcall(vim.fn.bufload, bufnr)
if not loaded then
vim.notify(tostring(err), vim.log.levels.ERROR)
return false
end
opts = opts or {}
local focus = vim.nonnil(opts.focus, true)
if focus then