Skip to content

fix: defer TabClosed cleanup to prevent window-switching error#341

Open
idmyn wants to merge 1 commit intoesmuellert:mainfrom
idmyn:main
Open

fix: defer TabClosed cleanup to prevent window-switching error#341
idmyn wants to merge 1 commit intoesmuellert:mainfrom
idmyn:main

Conversation

@idmyn
Copy link

@idmyn idmyn commented Mar 21, 2026

Hi there - very nice plugin!

I hit this error when running open_in_prev_tab with close_on_open_in_prev_tab = true

E5108:%20Error%20executing%20lua%3A%20vim/_editor.lua:0:%20nvim_exec2()%5B1%5D..TabClosed%20Autocommands%20for%20%22*%22:%20Vim(append):Error%20executing%20lua%20callback:%20...zy/codediff.nvim/lua/codediff/ui/view/welcome_window.lua:35:%20Problem%20w%0Ahile%20switching%20windows%0Astack%20traceback:%0A%20%20%20%20%20%20%20%20%5BC%5D:%20in%20function%20'__newindex'%0A%20%20%20%20%20%20%20%20...zy/codediff.nvim/lua/codediff/ui/view/welcome_window.lua:35:%20in%20function%20'apply_opts'%0A%20%20%20%20%20%20%20%20...zy/codediff.nvim/lua/codediff/ui/view/welcome_window.lua:90:%20in%20function%20'apply_normal'%0A%20%20%20%20%20%20%20%20...lazy/codediff.nvim/lua/codediff/ui/lifecycle/cleanup.lua:100:%20in%20function%20'cleanup_diff'%0A%20%20%20%20%20%20%20%20...lazy/codediff.nvim/lua/codediff/ui/lifecycle/cleanup.lua:189:%20in%20function%20%3C...lazy/codediff.nvim/lua/codediff/ui/lifecycle/cleanup.lua:178%3E%0A%20%20%20%20%20%20%20%20%5BC%5D:%20in%20function%20'nvim_exec2'%0A%20%20%20%20%20%20%20%20vim/_editor.lua:%20in%20function%20'cmd'%0A%20%20%20%20%20%20%20%20...nvim/lazy/codediff.nvim/lua/codediff/ui/view/keymaps.lua:352:%20in%20function%20%3C...nvim/lazy/codediff.nvim/lua/codediff/ui/view/keymaps.lua:263%3E%0Astack%20traceback:%0A%20%20%20%20%20%20%20%20%5BC%5D:%20in%20function%20'nvim_exec2'%0A%20%20%20%20%20%20%20%20vim/_editor.lua:%20in%20function%20'cmd'%0A%20%20%20%20%20%20%20%20...nvim/lazy/codediff.nvim/lua/codediff/ui/view/keymaps.lua:352:%20in%20function%20%3C...nvim/lazy/codediff.nvim/lua/codediff/ui/view/keymaps.lua:263%3E%0APress%20ENTER%20or%20type%20command%20to%20continue

And amp fixed it with this diff. It solved the problem for but I'm new to nvim/lua so I have absolutely no idea if it's a sensible fix 😅

But opening a PR in case it helps others!

Amp explained it like this:

Problem

Using open_in_prev_tab with close_on_open_in_prev_tab = true triggers
E5108: "Problem while switching windows".

The TabClosed autocmd runs cleanup_diff synchronously, which eventually
calls vim.wo[winid][name] = value in welcome_window.apply_opts. Setting
window-local options requires Neovim to switch windows internally, which is
forbidden during a TabClosed event.

Fix

Wrap the TabClosed callback body in vim.schedule() to defer cleanup
until after the event loop settles. This matches the pattern already used
by the WinClosed handler in the same file.

The TabClosed autocmd runs cleanup_diff synchronously, which calls
welcome_window.apply_opts, which sets window options via vim.wo.
This requires switching windows internally, which is forbidden during
TabClosed. Wrapping in vim.schedule defers the cleanup until after
the event, matching the existing pattern used by the WinClosed handler.

Fixes "Problem while switching windows" error when using
open_in_prev_tab with close_on_open_in_prev_tab enabled.
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.

1 participant