Skip to content

fix(files): remove invalid window from explorer.windows#2438

Closed
abeldekat wants to merge 1 commit into
nvim-mini:mainfrom
abeldekat:fix_files_invalid_win_id
Closed

fix(files): remove invalid window from explorer.windows#2438
abeldekat wants to merge 1 commit into
nvim-mini:mainfrom
abeldekat:fix_files_invalid_win_id

Conversation

@abeldekat
Copy link
Copy Markdown
Member

Details:

  • When MiniFilesWindowUpdate uses get_explorer_state() with enabled preview, an error occurs after the user presses o, <Esc>, u.

The error:

Error in User Autocommands for "MiniFilesWindowUpdate":                                                                                                                                      
Lua callback: ...m-repro/site/pack/vnr/start/mini.nvim/lua/mini/files.lua:1105: Invalid window id: 1004                                                                                      
stack traceback:                                                                                                                                                                             
        [C]: in function 'nvim_win_get_buf'                                                                                                                                                  
        ...m-repro/site/pack/vnr/start/mini.nvim/lua/mini/files.lua:1105: in function 'get_explorer_state'                                                                                   
        /home/user/.config/nvim-repro/init.lua:12: in function </home/user/.config/nvim-repro/init.lua:12>                                                                                 
        [C]: in function 'nvim_exec_autocmds'                                                                                                                                                
        ...m-repro/site/pack/vnr/start/mini.nvim/lua/mini/files.lua:3096: in function 'trigger_event'                                                                                        
        ...m-repro/site/pack/vnr/start/mini.nvim/lua/mini/files.lua:1873: in function 'explorer_refresh_depth_window'                                                                        
        ...m-repro/site/pack/vnr/start/mini.nvim/lua/mini/files.lua:1566: in function 'explorer_refresh'                                                                                     
        ...m-repro/site/pack/vnr/start/mini.nvim/lua/mini/files.lua:2147: in function 'fn'                                                                                                   
        [string "vim/_core/editor"]:408: in function <[string "vim/_core/editor"]:407>    

Repro:

require('mini.files').setup({ windows = {  preview = true } })
vim.api.nvim_create_autocmd('User', {
  pattern = 'MiniFilesWindowUpdate',
  callback = function() MiniFiles.get_explorer_state() end,
})
  1. Open nvim
  2. Type :lua MiniFiles.open()
  3. Type o and press <Esc>.
  4. Type u

This happens because the preview buffer is deleted. However, the win_id is still present in explorer.windows.

@abeldekat abeldekat changed the title fix(files): remove invalid window from explorer.windows fix(files): remove invalid window from explorer.windows May 28, 2026
@abeldekat abeldekat force-pushed the fix_files_invalid_win_id branch from 0816617 to c69f96e Compare May 28, 2026 18:08
@abeldekat abeldekat requested a review from echasnovski May 28, 2026 18:08
Comment thread lua/mini/files.lua Outdated
Comment thread lua/mini/files.lua Outdated
H.opened_buffers[buf_id].win_id = nil
else
H.window_close()
explorer.windows[i] = nil
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In theory, this is not the cleanest assignment as it can introduce "holes" in the array (i.e. explorer.windows[2] = nil while explorer.windows[3] is not nil).

My first instinct tells me that the culprit is probably somewhere else.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

My first instinct tells me that the culprit is probably somewhere else.

Interesting.
When I comment this line, the error does not happen.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This behavior could potentially happen starting from this commit.
I think it wasn't noticed because the examples don't use get_explorer_state in MiniFilesWindowUpdate. I found it when playing with the code example from the custom side scrolling discussion

Copy link
Copy Markdown
Member Author

@abeldekat abeldekat May 29, 2026

Choose a reason for hiding this comment

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

In theory, this is not the cleanest assignment as it can introduce "holes" in the array (i.e. explorer.windows[2] = nil while explorer.windows[3] is not nil).

I fixed that.

@echasnovski
Copy link
Copy Markdown
Member

Thanks for the PR!

I can reproduce. And not only after u (which might indicate on some undo related issues), but also after moving cursor to the file that is present on disk.

@abeldekat abeldekat force-pushed the fix_files_invalid_win_id branch from c69f96e to 5118a3a Compare May 28, 2026 18:47
Details:
- When MiniFilesWindowUpdate uses `get_explorer_state()` with enabled
  preview, an error occurs after the user presses `o`, `<Esc>`, `u`.
@abeldekat abeldekat force-pushed the fix_files_invalid_win_id branch from 5118a3a to 94576dd Compare May 29, 2026 06:54
@echasnovski
Copy link
Copy Markdown
Member

After a more research, it looks like the culprit is that the events are triggered too early during the "explorer refresh" cycle. This makes code a bit cleaner, but has a downside of making get_explorer_state() act on not yet fully refreshed explorer state. The error also happens in MiniFilesWindowOpen event.

As usually triggering events when everything is up to date (be it "already up to date" for open/changed kind of events or "yet up to date" for close/changedPre kind of events) is a good idea, triggering both problematic events a bit later seems okay. This also shows that all the bookkeeping about the explorer state was correct.

Thanks again for finding this and making a PR!

@abeldekat abeldekat deleted the fix_files_invalid_win_id branch May 29, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants