fix(buffer): name becomes empty when it is the cwd #40980

Problem: Shortening the name of a buffer whose full name is the current
directory + path sep yields empty string -> buf unnamed.
This doesn't interact well with things like `:mksession`, which
for example could `:badd` with no arg -> E471.

Solution: Keep the full path when shortening yields an empty name, as
`path_try_shorten_fname()` already does.
This commit is contained in:
Barrett Ruth
2026-07-26 04:36:03 -05:00
committed by GitHub
parent 62c2538d77
commit 3d5de7fd11
2 changed files with 18 additions and 3 deletions

View File

@@ -2378,11 +2378,10 @@ void shorten_buf_fname(buf_T *buf, char *dirname, int force)
XFREE_CLEAR(buf->b_sfname);
}
char *p = path_shorten_fname(buf->b_ffname, dirname);
if (p != NULL) {
if (p != NULL && *p != NUL) {
buf->b_sfname = xstrdup(p);
buf->b_fname = buf->b_sfname;
}
if (p == NULL) {
} else {
buf->b_fname = buf->b_ffname;
}
}

View File

@@ -188,6 +188,22 @@ describe(':mksession', function()
eq(expected, api.nvim_buf_get_name(0))
end)
it('restores a directory buffer for the CWD #40939', function()
local cwd_dir = t.fix_slashes(fn.getcwd())
local expected = cwd_dir .. '/'
command('set sessionoptions=buffers,curdir')
command('edit ' .. cwd_dir)
command('cd ' .. cwd_dir)
neq('', fn.bufname('%'))
eq(expected, api.nvim_buf_get_name(0))
command('mksession ' .. session_file)
command('%bwipeout!')
command('source ' .. session_file)
eq(expected, api.nvim_buf_get_name(0))
end)
it('restores CWD for :terminal buffers #11288', function()
local cwd_dir = fn.fnamemodify('.', ':p:~'):gsub([[/*$]], '')
local session_path = cwd_dir .. '/' .. session_file