From 28a13b01c7d7ccfd50e02cb34f5d119b28173df9 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Mon, 18 May 2026 22:15:29 +0100 Subject: [PATCH] vc-refresh-state: Override default-directory for backend functions I ran into the issue described in the comment with the current code in project-find-file-in and project-find-dir, when using 'C-x p p' to switch between projects. * lisp/vc/vc-hooks.el (vc-refresh-state): When calling into the backend, override any let-bindings of default-directory. --- lisp/vc/vc-hooks.el | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lisp/vc/vc-hooks.el b/lisp/vc/vc-hooks.el index d0f292d2c9d..64ad4d5daec 100644 --- a/lisp/vc/vc-hooks.el +++ b/lisp/vc/vc-hooks.el @@ -955,10 +955,16 @@ In the latter case, VC mode is deactivated for this buffer." (cond ((setq backend (with-demoted-errors "VC refresh error: %S" (vc-backend buffer-file-name))) - ;; Let the backend setup any buffer-local things he needs. - (vc-call-backend backend 'find-file-hook) - ;; Compute the state and put it in the mode line. - (vc-mode-line buffer-file-name backend) + ;; When `auto-revert-handler' calls us then `default-directory' + ;; may be let-bound to something else for the purpose of some + ;; command that's currently doing some minibuffer prompting. + ;; Backend find-file-hook and mode-line-string functions should + ;; not need to be written so as to handle that possibility. + (let ((default-directory (buffer-local-toplevel-value 'default-directory))) + ;; Let the backend setup any buffer-local things it needs. + (vc-call-backend backend 'find-file-hook) + ;; Compute the state and put it in the mode line. + (vc-mode-line buffer-file-name backend)) (unless vc-make-backup-files ;; Use this variable, not make-backup-files, ;; because this is for things that depend on the file name.