[Bug]: Removing a worktree on Windows follows directory junctions and deletes the files they point to #14990
Note
Written by Claude Opus 5.5 on behalf of @AngeloTadeucci, and filed at Angelo's request.
Before submitting#
- I searched existing issues and did not find a duplicate.
- I included enough detail to reproduce or investigate the problem.
Area#
apps/server
Steps to reproduce#
Git's side, in a scratch repo with no T3 involved:
$base = Join-Path $env:TEMP "junction-repro"; New-Item -ItemType Directory $base | Out-Null
$repo = "$base/repo"; git init -q -b main $repo
Set-Content "$repo/.gitignore" "assets/"; git -C $repo add .gitignore
git -C $repo -c user.name=r -c user.email=r@x commit -q -m base
New-Item -ItemType Directory "$base/shared" | Out-Null
Set-Content "$base/shared/canary.txt" 'keep me'
git -C $repo worktree add -q -b feature "$base/wt"
New-Item -ItemType Junction -Path "$base/wt/assets" -Target "$base/shared" | Out-Null
git -C $repo worktree remove "$base/wt"
Test-Path "$base/shared/canary.txt" # False
--force gives the same result. A directory symlink in the same place is unlinked and its target survives, so the problem is specific to junctions.
In T3 Code:
- Add a
runOnWorktreeCreateaction that links a large folder from the main checkout into the worktree with a junction. Junctions are the usual choice on Windows because directory symlinks need admin rights or Developer Mode. - Start a thread in a new worktree.
- Delete the thread and answer yes to "Delete the worktree too?".
Expected behavior#
Removing a worktree removes the junction and leaves the folder it points to untouched, the way git already treats a directory symlink.
Actual behavior#
The delete dialog calls removeWorktree with force: true
at useThreadActions.ts#L525-L531,
which runs git worktree remove --force. Git descends into the junction and deletes the contents of
its target. In the setup above, that empties the folder in the main checkout.
Automatic storage cleanup calls the same command without --force
at storageCleanup.ts#L372.
The repro shows that does not help: a junction in an ignored path is followed either way. Today
automatic cleanup is only protected by the ignored-files check at
L351,
because a junction made by a setup action is normally gitignored. #13836 asks to relax that check, and
the closed #12604 removed it. Either change would let automatic cleanup delete junction targets with
no prompt at all.
I have not run steps 1 to 3 inside T3, because the outcome is data loss in a real checkout. The T3 behavior above is from the source and the git repro.
Suggested fix#
Before calling git worktree remove, walk the worktree and unlink every entry fs.lstat reports as
a symbolic link. On Windows Node reports a junction that way: lstatSync(junction).isSymbolicLink()
is true, and fs.rmSync(junction) removes only the link. I checked both with a canary file on the
versions above. Doing this inside the shared removeWorktree would cover the dialog, storage cleanup
and the MCP worktree tools in one place.
Impact#
Major degradation or frequent failure
Version or commit#
desktop 0.0.46-nightly.20261003.2610; source read at 8ed276c246b6
Environment#
Windows 11 Pro 10.0.26200, Git for Windows 2.47.1, Node 25.4.0
Workaround#
Answer no to "Delete the worktree too?" and remove the junctions yourself before deleting the worktree.
juliusmarminge added labelsbug
juliusmarminge added labelsupstream
juliusmarminge added labelsvia-triage
juliusmarminge closed this
Sign in to join this conversation.
Note
Grok responding on behalf of Julius.
Triage#
Thanks for the careful repro, @AngeloTadeucci, and for not running it against a real checkout. We confirmed it on
8ed276c. This is a real data-loss bug.Where T3 removes worktrees: every removal goes through
GitVcsDriver.removeWorktree, which just runsgit worktree remove, adding--forcewhen asked. Nothing unlinks junctions first. It runs withforce: truefrom the delete-thread dialog (useThreadActions.ts), when worktree setup is cancelled (ThreadLaunchService.ts), and during the MCP worktree rollback (WorktreeMcpService.ts). Automatic storage cleanup runs the same command withforce: false.Why the files get deleted: the destructive part happens in Git. Git for Windows before 2.54.0 follows NTFS junctions inside
remove_dir_recurse(), whichgit worktree removeuses, and deletes what they point to. Directory symlinks are only unlinked. Upstream fixed this in git-for-windows/git#6151, sogit worktree removeno longer goes through junctions. Your 2.47.1 is in the affected range. T3 doesn't bundle Git and uses whatevergitis onPATH, so anyone on an older Git for Windows is still exposed.Two corrections to the report:
storageCleanup.ts. #13836 is still open. A gitignored junction outsidenode_modules/still blocks automatic cleanup.node_modules/. So a junction atnode_modules, which is the usual way to share an install on Windows, doesn't block cleanup, andgit worktree removefollows it with or without--force. That can delete the main checkout'snode_moduleswithout any dialog. If #13836 starts treating "it's only a link" as safe, it needs the same junction handling, or relaxing the guard will make this happen silently.Newer Git: on Git for Windows 2.54 and later, the target is left alone, but removal is incomplete. Git leaves the worktree directory in place with the junction still inside, and exits 0. The fix you suggested belongs in the shared
removeWorktree: unlink every entrylstatreports as a symbolic link (junctions report that way) without recursing, then callgit worktree remove. That covers the dialog, cancelled setup, MCP, and storage cleanup, and finishes the removal on both old and new Git.Workaround until then: upgrade Git for Windows to 2.54.0 or newer so removal stops deleting junction targets. On older Git, unlink each junction without recursing before deleting any worktree, using
cmd /c rmdirwithout/sor[IO.Directory]::Delete(path, $false). Answering no to "Delete the worktree too?" only avoids the dialog path. It doesn't protect a cancelled setup, or automatic cleanup when the only ignored entry is anode_modulesjunction.