[vim/vim] BUG: win_execute() clobbers :lcd override of 'autochdir' (PR #19343)

0 views
Skip to first unread message

Ingo Karkat

unread,
Feb 6, 2026, 4:05:50 AM (2 days ago) Feb 6
to vim/vim, Subscribed

This problem has been in Vim since the introduction of win_execute(), but it's only encountered when 'autochdir' is set (which most users don't) and you do a local override of the working directory. Unfortunately, I've been encountering that frequently, because my configuration switches to the parent directory for editing Git commit messages (so I can directly reference files from the project root instead of the useless .git/ subdir where the message is created in), and I use several custom completions that inspect other buffers via win_execute(). If you :wq after a completion, the COMMIT_EDITMSG file is written to the project root, and the Git command aborts or uses stale data! I've finally dug into this, and found a simple fix for this cornercase, that I've successfully been using for the past week.

The problem is caused by a bad interaction of the 'autochdir' behavior, overriding of the current directory via :lchdir, and the temporary window switching done by win_execute(), manifesting when e.g. a custom completion inspects other buffers.

  1. In the initial state after the :lcd .. we have curbuf->b_fname = "Xsubdir/file".
  2. do_autochdir() is invoked, temporarily undoing the :lcd .., changing back into the Xsubdir/ subdirectory.
  3. win_execute() switches windows, triggering win_enter_ext()win_fix_current_dir()shorten_fnames(TRUE)
  4. shorten_fnames() processes all buffers
  5. shorten_buf_fname() makes the filename relative to the current (wrong) directory; b_fname becomes file instead of Xsubdir/file
  6. Directory restoration correctly restores working directory via mch_chdir() (skipping a second do_autochdir() invocation because apply_acd is FALSE), but b_fname remains corrupted, with the Xsubdir/ part missing.
  7. expand('%:p') (and commands like :write) continue to use the corrupted filename, resolving to a wrong path that's missing the Xsubdir/ part.

To fix the problem the short filename is saved if its in effect (i.e. pointed to by curbuf->b_fname) and 'autochdir' happened. It's then restored in case of a local cwd override. The conditions limit this workaround to when 'autochdir' is active and overridden by a :lchdir.

The added test reproduces the problem and verifies the fix with a minimal setup using just one buffer and two split windows.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/19343

Commit Summary

  • 8973451 BUG: Tests: Temporary override of 'autochdir' via :lcd isn't clobbered by win_execute() in a split window
  • 0970a0d BUG: win_execute() clobbers :lcd override of 'autochdir'

File Changes

(2 files)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19343@github.com>

zeertzjq

unread,
Feb 6, 2026, 4:22:51 AM (2 days ago) Feb 6
to vim/vim, Subscribed

@zeertzjq commented on this pull request.


In src/evalwindow.c:

> @@ -732,6 +732,7 @@ f_win_execute(typval_T *argvars, typval_T *rettv)
 # ifdef FEAT_AUTOCHDIR
     char_u	autocwd[MAXPATHL];
     int	apply_acd = FALSE;
+    char_u	*save_sfname;
⬇️ Suggested change
-    char_u	*save_sfname;
+    char_u	*save_sfname = NULL;


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19343/review/3761756483@github.com>

Ingo Karkat

unread,
Feb 6, 2026, 4:24:47 AM (2 days ago) Feb 6
to vim/vim, Push

@inkarkat pushed 1 commit.

  • 8011f85 Review: Incorporate feedback


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19343/before/0970a0de090c731075bc3394611f36ab73c79fe7/after/8011f8569c791adc1de9b5a985690acdecfcab9b@github.com>

Christian Brabandt

unread,
Feb 6, 2026, 4:35:00 AM (2 days ago) Feb 6
to vim/vim, Subscribed
chrisbra left a comment (vim/vim#19343)

Thanks, hm, doesn't that leak memory and you need to free curbuf->b_sfname before re-assigning it?


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19343/c3859104325@github.com>

Ingo Karkat

unread,
Feb 6, 2026, 5:08:54 AM (2 days ago) Feb 6
to vim/vim, Push

@inkarkat pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19343/before/8011f8569c791adc1de9b5a985690acdecfcab9b/after/a5cb890a550cfc1468f7f52238cd3de1a305fcb6@github.com>

Christian Brabandt

unread,
Feb 7, 2026, 5:33:23 AM (yesterday) Feb 7
to vim/vim, Subscribed
chrisbra left a comment (vim/vim#19343)

thanks


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19343/c3864114541@github.com>

Christian Brabandt

unread,
Feb 7, 2026, 5:47:32 AM (yesterday) Feb 7
to vim/vim, Subscribed

Closed #19343 via abb4d74.


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19343/issue_event/22608258316@github.com>

Reply all
Reply to author
Forward
0 new messages