Subject: bug#79126: vc-switch-working-tree does not work if there is no matching file

From: Eshel Yaron <[email-masked]>
Date: Wed, 30 Jul 2025 06:30:31 +0200

Hi Dmitry, Sean, Spencer,

First of all, thank you for adding these new worktree commands, I use
Git worktrees a lot and it's great to see VC supporting them.

Dmitry Gutov <[email-masked]> writes:

> If the current buffer does not visit a file (such as Dired, mentioned
> in a FIXME comment), or if the current file has no corresponding on
> the other worktree, we end up with an error.
>
> Like
>
>   file-relative-name: Wrong type argument: stringp, nil
>
> or
>
>   File ‘lisp/progmodes/abc.el’ not found in ‘~/vc/emacs-28/’

FWIW, while incorporating this new feature in my Emacs branch, I've made
a few experimental tweaks that eliminate these errors.  Namely, I added
a variable vc-switch-working-tree-function which vc-switch-working-tree
consults to do the actual switching.  By default this variable is set to
a function that works for file-visiting buffers (similarly to the
current implementation in master), but major modes can readily set this
variable buffer-locally to provide worktree-switching support also for
non-file buffers.  I used this indirection to support Dired buffers.

( The more fundamental change I made was to avoid the dependency of VC
  on project.el, which I find... suboptimal.  Instead I introduced hooks
  vc-delete/create-tree-hook which one can use, among other things, to
  keep project--list in sync with deleted/created worktrees.  This does
  come at the expense of effectively disabling the Mercurial support. )

If you want to take a look, you can find my tweaks here:

  http://git.eshelyaron.com/gitweb/?p=emacs.git;a=commitdiff;h=2b1383d647402df2b539f69cf73a12c0717b00f3

> I guess some of these could be improved in project-find-matching-file
> (support for more major modes?) but semantically it seems like
> vc-switch-working-tree should try harder. Falling back to visiting the
> worktree root in Dired, if it doesn't find a better choice.

That sounds like a good fallback, indeed.

Best regards,

Eshel