Fugitive breaks when `~/bin` is a(n absorbed) submodule of `~`
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) /binis 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 ofcore.worktreein/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().
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
~/devis a git repo~/dev/lib/invokeris a submodule (absorbed, gitdir at~/.git/modules/lib/invoker)- Submodule config has
worktree = ../../../../lib/invoker /libis a symlink to/usr/lib(standard on many Linux systems)
Steps to reproduce:
- Open Neovim from any directory
- Use a file explorer plugin (nvim-tree, NERDTree, etc.) that changes cwd when navigating
- Navigate to and open a file in the submodule
- 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).