diff --git a/lua/codediff/ui/view/side_by_side/update.lua b/lua/codediff/ui/view/side_by_side/update.lua index eaaad89d..be8f72a6 100644 --- a/lua/codediff/ui/view/side_by_side/update.lua +++ b/lua/codediff/ui/view/side_by_side/update.lua @@ -197,6 +197,15 @@ function M.update(tabpage, session_config, auto_scroll_to_first_hunk) modified = modified_is_virtual and modified_info.needs_edit, } + -- A scheduled render can run while M.update is still loading the other side: + -- vim.wait inside an LspAttach handler (e.g. yaml-companion's request_sync) + -- during open_real_file's :edit pumps the vim.schedule queue, so the other + -- side's bufnr may still be nil here (see #592). nvim_buf_is_valid(nil) + -- throws instead of returning false, and dropping the render would leave + -- the view unrendered because readiness fires only once. Remember the + -- attempt instead; M.update re-arms it once both sides are loaded. + local render_deferred = false + local render_everything = function() if not current() then return @@ -206,7 +215,12 @@ function M.update(tabpage, session_config, auto_scroll_to_first_hunk) return end - -- Guard: Check if buffers are still valid + -- Guard: Check if buffers are still valid. A nil bufnr means the render + -- ran re-entrantly before M.update assigned it; defer, don't drop. + if original_info.bufnr == nil or modified_info.bufnr == nil then + render_deferred = true + return + end if not vim.api.nvim_buf_is_valid(original_info.bufnr) or not vim.api.nvim_buf_is_valid(modified_info.bufnr) then return end @@ -289,6 +303,12 @@ function M.update(tabpage, session_config, auto_scroll_to_first_hunk) reload_side(original_win, original_info, original_is_virtual) reload_side(modified_win, modified_info, modified_is_virtual) + -- A render that ran re-entrantly while a side was still loading (see above) + -- was deferred instead of dropped; re-arm it now that both bufnrs exist. + if render_deferred then + vim.schedule(render_everything) + end + welcome_window.sync(original_win) welcome_window.sync(modified_win) diff --git a/tests/integration/ui/view/update_nil_bufnr_spec.lua b/tests/integration/ui/view/update_nil_bufnr_spec.lua new file mode 100644 index 00000000..36f5169f --- /dev/null +++ b/tests/integration/ui/view/update_nil_bufnr_spec.lua @@ -0,0 +1,99 @@ +-- Regression test for #592: a scheduled render can run re-entrantly while +-- show() is still loading the other side. When the virtual side's content +-- arrives from the (warm) git cache, readiness queues render_everything with +-- vim.schedule; a vim.wait inside an LspAttach handler during the real +-- side's :edit (yaml-companion's request_sync does exactly this) pumps that +-- queue before open_real_file has returned, so modified_info.bufnr is still +-- nil and nvim_buf_is_valid(nil) throws at update.lua:210. The view must +-- render anyway once both sides are loaded, not crash and stay unrendered. +-- +-- The vim.wait in the FileType autocmd below stands in for yaml-companion: +-- no real LSP server is needed, only a handler that pumps the event loop +-- while :edit is running. + +local support = require("tests.support") +local path = require("codediff.core.path") +local view = require("codediff.ui.view") +local lifecycle = require("codediff.ui.lifecycle") + +describe("side-by-side update with a nil bufnr (#592)", function() + local repo, sha, tab + + local function config_for(file) + return { + git_root = repo.dir, + original = path.make_ref(file, repo.dir), + modified = path.make_ref(file, repo.dir), + original_revision = sha, + modified_revision = "WORKING", + } + end + + local function diff_ready() + local session = tab and lifecycle.get_session(tab) + local result = session and session.stored_diff_result + return result ~= nil and result.changes ~= nil and #result.changes > 0 + end + + before_each(function() + require("codediff").setup({ diff = { layout = "side-by-side" } }) + support.ensure_plugin_loaded() + repo = support.create_temp_git_repo() + repo.write_file("config.yaml", { "key: one", "list:", " - a", " - b" }) + repo.write_file("other.yaml", { "name: one", "v: 1" }) + sha = repo.commit("add yamls") + repo.write_file("config.yaml", { "key: two", "list:", " - a", " - b", " - c" }) + repo.write_file("other.yaml", { "name: two", "v: 2" }) + end) + + after_each(function() + pcall(vim.api.nvim_clear_autocmds, { group = "CodeDiff592Repro" }) + while vim.fn.tabpagenr("$") > 1 do + vim.cmd("tabclose!") + end + if repo then + repo.cleanup() + repo = nil + end + tab = nil + end) + + it("renders the diff instead of crashing when a scheduled render runs while the real side is still loading", function() + -- Warm the git content cache for other.yaml, as on a refresh: the crash + -- needs the virtual side's load callback to run synchronously. + local warmed = false + require("codediff.core.git.content").get_file_content(sha, repo.dir, "other.yaml", function(err, lines) + warmed = err == nil and lines ~= nil + end) + vim.wait(3000, function() + return warmed + end) + assert.is_true(warmed, "git content cache should warm for other.yaml") + + -- Control case: an ordinary view renders normally. + view.create(config_for("config.yaml")) + tab = vim.api.nvim_get_current_tabpage() + vim.wait(3000, diff_ready) + assert.is_true(diff_ready(), "control view should render its diff") + + -- Stand-in for yaml-companion's LspAttach handler: request_sync pumps + -- the event loop with vim.wait while :edit is running. + vim.api.nvim_create_augroup("CodeDiff592Repro", { clear = true }) + vim.api.nvim_create_autocmd("FileType", { + group = "CodeDiff592Repro", + pattern = "yaml", + callback = function() + vim.wait(50) + end, + }) + + -- The real side's buffer was never loaded, so this update must :edit it + -- while the virtual side's readiness render is already queued. + view.update(tab, config_for("other.yaml")) + vim.wait(3000, diff_ready) + local session = lifecycle.get_session(tab) + assert.is_not_nil(session, "session should survive the update") + assert.is_not_nil(session.stored_diff_result, "update should render a diff instead of crashing on a nil bufnr") + assert.equal(1, #session.stored_diff_result.changes, "update should render the other.yaml change") + end) +end)