From a54a7f6a78ab0694a0cedf696bfbe4f65468164e Mon Sep 17 00:00:00 2001 From: Barrett Ruth <62671086+barrettruth@users.noreply.github.com> Date: Thu, 9 Jul 2026 14:41:09 -0500 Subject: [PATCH] fix(dir): preserve alternate buffer #40659 --- runtime/lua/nvim/dir.lua | 9 ++++++++- test/functional/plugin/dir_spec.lua | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/runtime/lua/nvim/dir.lua b/runtime/lua/nvim/dir.lua index cdc8713c77..2a36f6cab2 100644 --- a/runtime/lua/nvim/dir.lua +++ b/runtime/lua/nvim/dir.lua @@ -85,7 +85,14 @@ local function render(buf, dir) then return false end - api.nvim_buf_set_name(buf, dir) + api.nvim_buf_call(buf, function() + api.nvim_cmd({ + cmd = 'file', + args = { dir }, + mods = { keepalt = true }, + magic = { file = false, bar = false }, + }, {}) + end) if not api.nvim_buf_is_valid(buf) then return false end diff --git a/test/functional/plugin/dir_spec.lua b/test/functional/plugin/dir_spec.lua index 04dcd7616f..f9c96aa0bf 100644 --- a/test/functional/plugin/dir_spec.lua +++ b/test/functional/plugin/dir_spec.lua @@ -164,6 +164,19 @@ describe('nvim.dir', function() eq(false, exec_lua([[return package.loaded['nvim.dir'] ~= nil]])) end) + it('preserves alternate buffer when opening a parent directory', function() + make_fixture() + n.clear({ args_rm = { '-u', '--cmd' } }) + + edit(file) + feed('-') + poke_eventloop() + + assert_directory(root) + -- Keep the alternate buffer on the file we navigated up from. + eq(file, api.nvim_buf_get_name(fn.bufnr('#'))) + end) + it('uses an absolute buffer name for a relative startup directory argument', function() make_fixture() local cwd = assert(vim.uv.cwd())