vim-fugitive icon indicating copy to clipboard operation
vim-fugitive copied to clipboard

Fugitive breaks when `~/bin` is a(n absorbed) submodule of `~`

Open intelfx opened this issue 1 year ago • 1 comments

Prerequisites

  • ~ (e.g., /home/intelfx) is a git repo
  • ~/bin (/home/intelfx/bin) is a submodule
  • the submodule is absorbed (i.e., its gitdir is /home/intelfx/.git/modules/bin)
  • /bin is a symlink to /usr/bin

Bug

When Vim is launched with cwd = ~/bin (e.g., cd ~/bin; vim file.sh), attempting to invoke a :Git status yields:

fatal: cannot change to '/home/intelfx/usr/bin': No such file or directory

Analysis

We end up on https://github.com/tpope/vim-fugitive/blob/4a745ea72fa93bb15dd077109afbb3d1809383f2/plugin/fugitive.vim#L377 with

  • a:path = /home/intelfx/.git/modules/bin
  • dir = /home/intelfx/.git/modules/bin
  • getcwd() = /home/intelfx/bin
  • worktree = ../../../bin (which is exactly the value of core.worktree in /home/intelfx/.git/modules/bin/config)

The resolve() call uses cwd as origin, whereas it should be using dir. I won't send a patch because I'm not sure of all implications of the caching logic there, but it seems to me that the canonicalization of the worktree path on https://github.com/tpope/vim-fugitive/blob/4a745ea72fa93bb15dd077109afbb3d1809383f2/plugin/fugitive.vim#L382 happens too late; it needs to happen before the call to resolve().

intelfx avatar May 13 '25 23:05 intelfx

I encountered the same bug with a different trigger: changing the working directory within Neovim (via nvim-tree's change_dir option) causes fugitive to cache the wrong worktree path for submodules.

Setup

  • ~/dev is a git repo
  • ~/dev/lib/invoker is a submodule (absorbed, gitdir at ~/.git/modules/lib/invoker)
  • Submodule config has worktree = ../../../../lib/invoker
  • /lib is a symlink to /usr/lib (standard on many Linux systems)

Steps to reproduce:

  1. Open Neovim from any directory
  2. Use a file explorer plugin (nvim-tree, NERDTree, etc.) that changes cwd when navigating
  3. Navigate to and open a file in the submodule
  4. Run :echo FugitiveWorkTree()

Expected: /home/user/dev/lib/invoker Actual: /home/user/dev/usr/lib/invoker (note the spurious usr)

The relative path ../../../../lib/invoker gets resolved relative to whatever cwd was when fugitive first cached the repo. If cwd was at a depth where ../../../../lib traverses through root and hits the /lib → /usr/lib symlink, the path gets corrupted. Git itself reports the correct path:

$ cd ~/dev/lib/invoker && git rev-parse --show-toplevel
/home/user/dev/lib/invoker

Restarting Neovim from within the correct directory fixes it (until cwd changes again).

budmonde avatar Jan 27 '26 23:01 budmonde