Skip to content

fix(files): stop nvim.dir from showing if files is default explorer#2493

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

fix(files): stop nvim.dir from showing if files is default explorer#2493
abeldekat wants to merge 1 commit into
nvim-mini:mainfrom
abeldekat:mini_fix_files_default

Conversation

@abeldekat

@abeldekat abeldekat commented Jul 1, 2026

Copy link
Copy Markdown
Member

Details:

  • See PR 40507 in neovim/neovim

Tests that failed:

  • Default explorer | works on startup:
  • Default explorer | works in :edit .`
  • Default explorer | works in :tabfind .`
  • Default explorer | handles close without opening file

Surprisingly, test Default explorer works in :vsplit did not fail. Probably because of the vsplit...)

Before the fix, vim.fn.getbufinfo(1):

    ...
    variables = {                                                                                                                                                                            
      changedtick = 4,                                                                                                                                                                       
      current_syntax = "directory",                                                                                                                                                          
      minifiles_processed_dir = true,                                                                                                                                                        
      nvim_dir = "/home/user/.config/repro"                                                                                                                                                 
    }, 
    ...       

After:

    ...
    variables = {                                                                                                                                                                            
      changedtick = 3,                                                                                                                                                                       
      minifiles_processed_dir = true                                                                                                                                                         
    }, 
    ... 

@abeldekat

Copy link
Copy Markdown
Member Author

In my config, <C-s> is revealing....)

1782899271

<C-s>....

1782899454

@barrettruth

Copy link
Copy Markdown

I am skeptical of this. dir.lua will change shape a lot in the upcoming months, and the (in)ability to easily disable it will likely be fixed. for now, we're going to restore loading the plugin guard and disabling with vim.g like other (neo)vim plugins.

I'd wait for that to merge and simply set the flag instead.

@abeldekat

Copy link
Copy Markdown
Member Author

I'd wait for that to merge and simply set the flag instead.

Is mini.files able to set that flag reliably? The user might load mini.files later...

Previously, nvim.dir registered its callbacks as part of the FileExplorer group. That was working fine, as the group is cleared by mini.files

@barrettruth

barrettruth commented Jul 1, 2026

Copy link
Copy Markdown

I apologize for my short-sightedness. Basically, we're trying to figure out a new way to handle builtin neovim plugins more ergonomically and dir.lua is kind of the guinea pig.

The FileExplorer group will likely be removed soon. None of this surface is stable.

FileExplorer is inherited from vim :browse which is entirely out of date and I'm about to work on this soon. I would not rely on it.

Is mini.files able to set that flag reliably? The user might load mini.files later...

I believe so? If you're talking about "dynamically" disabling dir.lua after startup, that should work...

@echasnovski

Copy link
Copy Markdown
Member

Is mini.files able to set that flag reliably? The user might load mini.files later...

I believe so? If you're talking about "dynamically" disabling dir.lua after startup, that should work...

The to-be-maybe-restored documentation about g:loaded_nvim_dir_plugin says this:

To disable the built-in directory browser, set this before startup: >lua
vim.g.loaded_nvim_dir_plugin = 1
<

I.e. this will only work if 'mini.files' is loaded during startup. This might not be the case if it is lazy loaded (i.e. after startup).

What is needed for 'mini.files' is the way to disable 'dir.lua' from hijacking directory buffers. The netrw went the route of "delete this autocommand which we promise will remain named like this forever". This worked fine and might still work fine for 'dir.lua'.

@barrettruth

Copy link
Copy Markdown

Working on a solution for this now. I will comment on this thread when it's done. Disabling nvim.dir will be simple.

Thanks for your patience.

echasnovski added a commit that referenced this pull request Jul 2, 2026
Details:
- There is some work being done upstream on better default explorer
  (instead of `netrw`). Reacting to each its change is a bit too much
  for now. Temporarily disable tests for 'mini.files' as default
  explorer, but check on the upstream progress regularly to adjust
  'mini.files' behavior and re-enable tests as soon as reasonably
  possible.

Related to #2493
@echasnovski

Copy link
Copy Markdown
Member

I've temporarily disabled the default explorer 'mini.files' tests on Nightly. Hopefully for not too long.

@echasnovski

Copy link
Copy Markdown
Member

@barrettruth, there seems to be more than one back-and-fourth upstream PRs regarding this issue. Is it resolved yet?

@barrettruth

barrettruth commented Jul 11, 2026

Copy link
Copy Markdown

After neovim/neovim#40529, overriding FileType directory is all y'all should need to do. Note that the MiniFiles.open call must be synchronous. Maybe something like this worked for me on a local build off of the aforementioned pr...

if config.options.use_as_default_explorer then
  vim.api.nvim_create_autocmd('FileType', {
    group = gr,
    pattern = 'directory',
    nested = true,
    callback = function(args)
      vim.bo[args.buf].bufhidden = 'wipe'
      vim.b[args.buf].minifiles_processed_dir = true
      MiniFiles.open(vim.api.nvim_buf_get_name(args.buf), false)
      vim.schedule(function()
        vim.api.nvim_set_current_win(MiniFiles.get_explorer_state().windows[1].win_id)
      end)
    end,
  })
end

Stay tuned - just leaving this here as a progress update.

@echasnovski

Copy link
Copy Markdown
Member

Maybe something like this worked for me on a local build off of the aforementioned pr...

To be clear, I am not asking to tweak 'mini.files'.

The more appropriate question for dir.lua itself here is to explicitly document how users (and hence plugins) can disable dir.lua handling the filetype='directory' buffer. Both before and after dir.lua has set up its behavior to react to directory buffers.

The current approach of silently deleting explicitly documented augroup is fine and the same can be done for dir.lua (as is in this PR). It is the matter of making one's mind, commit to and explicitly document it.

@barrettruth

Copy link
Copy Markdown

Disable netrw & dir.lua and everything works fine right now on master:

vim.g.loaded_nvim_dir_plugin = 1
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
require('mini.files').setup({ options = { use_as_default_explorer = true } })

This is pretty clear in the documentation now, looking at it from an objective context rather than with all this poorly-managed chaos I've created XD. Sorry for the confusion.

Just to give you more context, the "right" way to handle this will be to override dir.lua and hook into filetype=directory.

This is because the idea of "is this buffer a directory" is being lifted off of mini.files and other file managers so as to simplify them.

@echasnovski

Copy link
Copy Markdown
Member

Disable netrw & dir.lua and everything works fine right now on master:

vim.g.loaded_nvim_dir_plugin = 1
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
require('mini.files').setup({ options = { use_as_default_explorer = true } })

This only works if this code is executed during startup, but does not work if executed after startup (as it is documented). So with the following 'init.lua' ...

vim.schedule(function() vim.g.loaded_nvim_dir_plugin = 1 end)

... starting nvim and executing :e . will still open dir.lua buffer.

The reason this is needed is because I don't want users to have to manually set some vim.g variables separately from enabling 'mini.files', which can be and usually is loaded lazily (i.e. after startup).

@abeldekat

Copy link
Copy Markdown
Member Author

@echasnovski,

Regarding this comment:

  1. delete the nvim.dir augroup
  2. handle FileType directory

I think it's very nice that in the new situation mini.files does not need a global BufEnter autocommand anymore.

The code below is tested on current nightly and v0.12.4:

MiniFiles using FileType directory
diff --git a/lua/mini/files.lua b/lua/mini/files.lua
index 4728a338..68e49586 100644
--- a/lua/mini/files.lua
+++ b/lua/mini/files.lua
@@ -1366,19 +1366,35 @@ H.create_autocommands = function(config)
     vim.api.nvim_create_autocmd(event, { group = gr, pattern = pattern, callback = callback, desc = desc })
   end
 
-  if config.options.use_as_default_explorer then
-    -- Stop 'netrw' from showing. Needs `VimEnter` event autocommand if
-    -- this is called prior 'netrw' is set up
-    vim.cmd('silent! autocmd! FileExplorer *')
-    vim.cmd('autocmd VimEnter * ++once silent! autocmd! FileExplorer *')
-
-    -- - Use `nested` to allow other events (`BufWinEnter` for 'mini.clue')
-    local opts = { nested = true, group = gr, callback = H.track_dir_edit, desc = 'Track directory edit' }
-    vim.api.nvim_create_autocmd('BufEnter', opts)
-  end
-
   au('VimResized', '*', MiniFiles.refresh, 'Refresh on resize')
   au('ColorScheme', '*', H.create_default_hl, 'Ensure colors')
+
+  if not config.options.use_as_default_explorer then return end
+
+  local set_as_default_explorer = vim.fn.has('nvim-0.13') == 0 and H.replace_netrw or H.replace_nvim_dir
+  set_as_default_explorer(gr)
+end
+
+-- TODO: Remove when support for nvim-v0.12 is dropped
+H.replace_netrw = function(gr)
+  -- Stop 'netrw' from showing. Needs `VimEnter` event autocommand if
+  -- this is called prior 'netrw' is set up
+  vim.cmd('silent! autocmd! FileExplorer *')
+  vim.cmd('autocmd VimEnter * ++once silent! autocmd! FileExplorer *')
+
+  -- - Use `nested` to allow other events (`BufWinEnter` for 'mini.clue')
+  local opts = { nested = true, group = gr, callback = H.track_dir_edit, desc = 'Track directory edit' }
+  vim.api.nvim_create_autocmd('BufEnter', opts)
+end
+
+H.replace_nvim_dir = function(gr)
+  -- Stop 'nvim.dir' from showing. Needs `VimEnter` event autocommand if
+  -- this is called prior 'nvim.dir' is set up
+  vim.cmd('silent! autocmd! nvim.dir *')
+  vim.cmd('autocmd VimEnter * ++once silent! autocmd! nvim.dir *')
+
+  local opts = { pattern = 'directory', group = gr, callback = H.track_dir_edit_by_ft, desc = 'Track directory edit' }
+  vim.api.nvim_create_autocmd('FileType', opts)
 end
 
 --stylua: ignore
@@ -1413,20 +1429,28 @@ H.normalize_opts = function(explorer_opts, opts)
 end
 
 -- Autocommands ---------------------------------------------------------------
+H.track_dir_edit_by_ft = function(data)
+  local buf_id = data.buf
+
+  -- Make directory buffer disappear when it is not needed
+  vim.bo[buf_id].bufhidden = 'wipe'
+
+  vim.schedule(function()
+    -- Open directory without history
+    MiniFiles.open(data.file, false)
+    -- Smartly delete directory buffer if already visited
+    local opts = { once = true, nested = true, buf = buf_id, callback = H.wipe_directory_buffer }
+    vim.api.nvim_create_autocmd('BufEnter', opts)
+  end)
+end
+
+-- TODO: Remove when support for nvim-v0.12 is dropped
 H.track_dir_edit = function(data)
   -- Make early returns
   if vim.api.nvim_get_current_buf() ~= data.buf then return end
 
-  if vim.b.minifiles_processed_dir then
-    -- Smartly delete directory buffer if already visited
-    local alt_buf = vim.fn.bufnr('#')
-    -- - Setting alternative buffer is enough for the "directory buffer" to be
-    -- wiped out, as it has `bufhidden=wipe`. Forcing delete after showing alt
-    -- buffer might result in hard-to-track errors (like when opening directory
-    -- in 'mini.pick' when there is an altbuf for the buffer in target window).
-    if alt_buf ~= data.buf and vim.fn.buflisted(alt_buf) == 1 then return vim.api.nvim_win_set_buf(0, alt_buf) end
-    return vim.api.nvim_buf_delete(data.buf, { force = true })
-  end
+  -- Smartly delete directory buffer if already visited
+  if vim.b.minifiles_processed_dir then return H.wipe_directory_buffer(data) end
 
   local path = vim.api.nvim_buf_get_name(0)
   if vim.fn.isdirectory(path) ~= 1 then return end
@@ -1439,6 +1463,16 @@ H.track_dir_edit = function(data)
   vim.schedule(function() MiniFiles.open(path, false) end)
 end
 
+H.wipe_directory_buffer = function(data)
+  local alt_buf = vim.fn.bufnr('#')
+  -- - Setting alternative buffer is enough for the "directory buffer" to be
+  -- wiped out, as it has `bufhidden=wipe`. Forcing delete after showing alt
+  -- buffer might result in hard-to-track errors (like when opening directory
+  -- in 'mini.pick' when there is an altbuf for the buffer in target window).
+  if alt_buf ~= data.buf and vim.fn.buflisted(alt_buf) == 1 then return vim.api.nvim_win_set_buf(0, alt_buf) end
+  return vim.api.nvim_buf_delete(data.buf, { force = true })
+end
+
 -- Explorers ------------------------------------------------------------------
 ---@class Explorer
 ---
diff --git a/tests/test_files.lua b/tests/test_files.lua
index c18eb85e..f90814b4 100644
--- a/tests/test_files.lua
+++ b/tests/test_files.lua
@@ -6289,15 +6289,7 @@ T['LSP']['respects `options.lsp_timeout`'] = function()
   eq(child.lua_get('_G.lsp_requests'), {})
 end
 
-T['Default explorer'] = new_set({
-  hooks = {
-    pre_case = function()
-      if child.fn.has('nvim-0.13') == 1 then
-        MiniTest.skip('Default explorer is being reworked on Nightly, so tests are not stable')
-      end
-    end,
-  },
-})
+T['Default explorer'] = new_set()
 
 T['Default explorer']['works on startup'] = function()
   vim.loop.os_setenv('USE_AS_DEFAULT_EXPLORER', 'true')

@echasnovski

Copy link
Copy Markdown
Member

I think it's very nice that in the new situation mini.files does not need a global BufEnter autocommand anymore.

It is nice, but it is also too early to take advantage of that. The BufEnter works on all supported versions, albeit Filetype directory adds less overhead. I think avoiding extra code as much as possible has more priority in an already very big 'mini.files' module.

Coincidentally, I am just finishing dealing with this locally.

echasnovski added a commit that referenced this pull request Jul 13, 2026
Resolve #2493

Co-authored-by: abeldekat <58370433+abeldekat@users.noreply.github.com>
@abeldekat abeldekat deleted the mini_fix_files_default branch July 13, 2026 16:51
@echasnovski

Copy link
Copy Markdown
Member

I've decided to go ahead and apply the similar in spirit change and enabled the tests on Nightly. Hopefully, the "delete nvim.dir augroup to disable it taking over directory buffer" suggestion will be stable enough.

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.

3 participants