Skip to content

wt list shows errors for worktrees being removed in background #452

@max-sixty

Description

@max-sixty

wt list shows errors for worktrees being removed in background

Problem

When running wt list immediately after wt remove, the removed worktree often still appears in the list with errors or stale data. This happens because:

  1. wt remove spawns background removal (with 1 second delay)
  2. User runs wt list before removal completes
  3. Git still reports the worktree, but git operations fail because the directory is being deleted

Example

$ wt remove
◎ Running pre-remove project:docs:
  lsof -ti :17911 | xargs kill 2>/dev/null || true
◎ Removing todos worktree & branch in background (no added changes on main, ⊂)
○ Switched to worktree for main @ ~/workspace/worktrunk
◎ Running post-switch user hook:
  echo | zellij pipe --name change-tab-name -- "$(jq -cn '{pane_id: $ENV.ZELLIJ_PANE_ID, name: " main"}')"

$ wt list
  Branch    Status        HEAD±    main↕  Path                   Remote⇅  URL                     Commit    Age   Message
@ main          ^|                        .                         |     :12107                  dcda7873  32m   adjust guidance...
+ todos         _                         ../worktrunk.todos              :17911                  d11fad1a  56y
...

○ Showing 5 worktrees, 1 with changes
▲ Some git operations failed:
  todos: commit-details (Failed to execute: git show -s --format=%ct d11fad1a...)
  todos: ahead-behind (Failed to execute: git rev-list --left-right --count main...d11fad1a...)
  todos: committed-trees-match (Failed to execute: git rev-parse d11fad1a...^{tree} main^{tree})
  todos: has-file-changes (Failed to execute: git diff --name-only main...todos)
  todos: is-ancestor (Failed to execute: git merge-base --is-ancestor d11fad1a... main)
  todos: working-tree-diff (Failed to execute: git status --porcelain)
  todos: merge-tree-conflicts (Failed to execute: git merge-tree --write-tree main d11fad1a...)
↳ To create a diagnostic file, re-run with --verbose

Note the 56y age — git operations are failing and returning garbage data.

Root cause

The background removal has a 1 second delay (to let shell cd complete), then runs:

  1. git worktree remove <path>
  2. git branch -D <branch> (if branch was merged)

During this window, git worktree list --porcelain still returns the worktree, but:

  • The directory may be partially deleted
  • Git commands targeting that worktree fail
  • The branch ref may already be deleted

Desired behavior

Worktrees with pending background removal should either:

  1. Not appear in wt list at all, OR
  2. Appear with a clear "pending removal" indicator (no errors, no stale data)

Possible solutions

Neither solution is great, but they're the simplest approaches.

Option A: Marker file in .git/wt-logs/

Write .git/wt-logs/{branch}-pending-removal before spawning background removal.
Delete after removal completes.

In wt list:

  • Check for marker file
  • Filter out or show with special indicator

Pros: Simple, doesn't touch git state
Cons: Another state file to manage; cleanup on failure

Option B: Check remove log recency

If .git/wt-logs/{branch}-remove.log exists and was modified within last 5 seconds, treat as pending removal.

Pros: No new files/state
Cons: Heuristic, race conditions possible

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions