fix(cwd): :bcd lost by nvim_win_set_buf, nvim_open_win #41218

Problem:
:bcd (buffer-local directory) is not preserved after
`nvim_open_win` or `nvim_win_set_buf`

Analysis:
set_curbuf() ends with update_cwd(), which falls back to
os_chdir(globaldir) when the target buffer has no b_localdir.

Solution:
Pass kCtxKeepCwd to ctx_switch().
This commit is contained in:
Erdiansyah authored and GitHub committed 2026-08-07 12:09:22 -04:00
1 parent fe3aa64945
commit 59597316a6
2 files changed
+24 -1

No files matched your search

+1 -1
View File
@@ -803,7 +803,7 @@ void win_set_buf(win_T *win, buf_T *buf, Error *err)
bool win_ok;
TRY_WRAP(err, {
win_ok = ctx_switch(&switchwin, win, tab, NULL, kCtxNoDisplay);
win_ok = ctx_switch(&switchwin, win, tab, NULL, kCtxNoDisplay | kCtxKeepCwd);
if (win_ok) {
const int save_acd = p_acd;
if (!switchwin.cs_same_win) {
+23
View File
@@ -562,6 +562,29 @@ describe('cd during temp context-switch', function()
cd_in_buf_call(hidden, 'tcd', tabdir)
eq({ 1, tabdir, tabdir }, { tlwd(), tcwd(), cwd() })
end)
it("nvim_open_win / nvim_win_set_buf keep the caller's cwd", function()
local bufdir = join(startdir, directories.buffer)
command('bcd ' .. bufdir)
eq(bufdir, cwd())
-- A transient switch to a scratch float must not reset the caller's cwd.
local float = call('nvim_open_win', call('nvim_create_buf', true, true), false, {
relative = 'editor',
width = 20,
height = 5,
row = 1,
col = 1,
})
eq(bufdir, cwd())
call('nvim_win_set_buf', float, call('nvim_create_buf', true, true))
eq(bufdir, cwd())
call('nvim_win_close', float, true)
eq(bufdir, cwd())
command('bcd!')
end)
end)
-- Test legal parameters for 'getcwd' and 'haslocaldir'