Skip to content
Source
Sign inSign up
pingdotgg/t3code

[Bug]: Removing a worktree on Windows follows directory junctions and deletes the files they point to #14990

ClosedAngeloTadeucci(GitHub profile, opens in a new tab) opened this issue 1 comment

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:

  1. Add a runOnWorktreeCreate action 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.
  2. Start a thread in a new worktree.
  3. 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.

  1. 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 runs git worktree remove, adding --force when asked. Nothing unlinks junctions first. It runs with force: true from 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 with force: 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(), which git worktree remove uses, and deletes what they point to. Directory symlinks are only unlinked. Upstream fixed this in git-for-windows/git#6151, so git worktree remove no longer goes through junctions. Your 2.47.1 is in the affected range. T3 doesn't bundle Git and uses whatever git is on PATH, so anyone on an older Git for Windows is still exposed.

    Two corrections to the report:

    • Closed PR #12604 never landed. It was closed without merging, and the ignored-file guard is still in storageCleanup.ts. #13836 is still open. A gitignored junction outside node_modules/ still blocks automatic cleanup.
    • That guard deliberately allows node_modules/. So a junction at node_modules, which is the usual way to share an install on Windows, doesn't block cleanup, and git worktree remove follows it with or without --force. That can delete the main checkout's node_modules without 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 entry lstat reports as a symbolic link (junctions report that way) without recursing, then call git 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 rmdir without /s or [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 a node_modules junction.

  2. juliusmarminge added labelsbug

  3. juliusmarminge added labelsupstream

  4. juliusmarminge added labelsvia-triage

  5. juliusmarminge closed this

Sign in to join this conversation.