Skip to content

fix: hijack directory buffers that were added before setup - #792

Open
ybai08 wants to merge 1 commit into
stevearc:masterfrom
ybai08:fix/hijack-preexisting-dir-buffers
Open

ybai08 wants to merge 1 commit into
stevearc:masterfrom
ybai08:fix/hijack-preexisting-dir-buffers

Conversation

@ybai08

@ybai08 ybai08 commented Oct 17, 2026

Copy link
Copy Markdown

Resolves #752

Problem

Oil takes over directory buffers from its BufAdd autocmd, and setup() does a one-off check of the current buffer. Neovim creates a buffer for every argument before it loads plugins, so those buffers never trigger the autocmd. Only the current one is handled in setup(). With nvim -O file.txt dir/, nvim -O dir1/ dir2/, or nvim file.txt dir/ followed by :next, any directory argument other than the first opens as a blank buffer whose filetype is empty.

Solution

setup() now also runs maybe_hijack_directory_buffer() on every listed buffer that isn't loaded yet. Each of those buffers is renamed to its oil:// url, so the existing BufReadCmd loads it when it is first entered. That is the same path a directory buffer added after setup() takes. The current-buffer logic is unchanged.

Only unloaded buffers are touched. A directory buffer that is already loaded (for example, oil lazy-loaded after VimEnter while a directory is open in a split) would also need load_oil_buffer() called outside its window. I left that case out to keep this change small.

Testing

  • Added a regression test in tests/regression_spec.lua. It adds a directory buffer with noautocmd badd, then calls setup(), then checks that the buffer is renamed to oil:// and lists the directory's contents once it is entered. The test fails on master (the name is not renamed) and passes with this change.
  • ./run_tests.sh on macOS with Neovim v0.12.6: 109 passed, 0 failed, 0 errors (master without the new test: 108 passed).
  • stylua --check lua tests (StyLua 2.6.0; CI pins v2.4.1), luacheck lua tests (0 warnings / 0 errors), and scripts/nvim-typecheck-action/typecheck.sh --workdir scripts/nvim-typecheck-action lua (lua-language-server 3.19.2, no problems) all pass.
  • Checked by hand with headless nvim -u <minimal init> runs. -O dir1/ dir2/, -O file.txt dir2/, -p file.txt dir1/ dir2/, and file.txt dir2/ +next now open every directory in oil. On master the second directory was blank.
  • Not run: the Neovim v0.10.4 / v0.11.0 CI matrix, and the doc lint step (scripts/main.py lint). No docs changed.

Directory buffers are hijacked from a BufAdd autocmd, plus a one-off
check of the current buffer in setup(). Buffers that already exist when
setup() runs never trigger that autocmd. When nvim is started with
several arguments, e.g. `nvim -O file.txt dir/`, every argument buffer
is created before plugins are loaded, so any directory buffer that is
not the current one is left alone and opens as an empty buffer.

In setup(), also hijack listed buffers that are not loaded yet. They
are renamed to their oil:// url, so BufReadCmd loads them when they are
first entered, just like a directory buffer added after setup.

Resolves stevearc#752
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.

bug: Opening multiple buffers at the same time (i.e., with -o or -O) fails to open oil in the second buffer

1 participant