Skip to content

fix(cleanup-worktree): aborts mid-way when invoked from inside the worktree being deleted #575

Closed

Description

@admarble

Summary

templates/scripts/cleanup-worktree.sh aborts after git worktree remove if the caller's cwd was inside the worktree being deleted. The remaining steps (delete local branch, delete remote branch, update main) silently fail because the shell's cwd no longer exists.

Repro

cd ../worktrees/feature/<N>-...     # enter the worktree
./scripts/dev/cleanup-worktree.sh feature/<N>-...

Output (real, from #551 fullsolve cleanup on 2026-05-03):

📂 Removing worktree...
🌿 Deleting local branch...
☁️  Deleting remote branch...
📥 Updating main branch...
fatal: Unable to read current working directory: No such file or directory

After this, the local branch and remote branch are still present (the git branch -D and git push origin --delete calls fail because the underlying git couldn't read cwd, and 2>/dev/null || true silently swallowed the error). User must manually:

git -C <main-repo> branch -D feature/<N>-...
git -C <main-repo> push origin --delete feature/<N>-...

Root Cause

templates/scripts/cleanup-worktree.sh line 76 (git worktree remove "$WORKTREE_PATH" --force) removes the directory the shell is currently in. Subsequent git calls on lines 80, 84, 88 inherit a now-broken cwd. With set -e at line 7, the script exits as soon as any git command fails — but only the silently-suppressed 2>/dev/null || true ones don't propagate, while git checkout main on line 88 (no error suppression) does abort.

Net result: lines 80 and 84 print their banner but the underlying commands fail; line 88 surfaces the cwd error and the script exits.

Proposed Fix

Capture the main worktree path at the start and cd there before the destructive git worktree remove:

 BRANCH_NAME=$1
+
+# Resolve main worktree (first entry in `git worktree list`) so all subsequent
+# git commands run from a stable cwd even after the target worktree is deleted.
+MAIN_WORKTREE=$(git worktree list | head -1 | awk '{print $1}')
 ...
 # Remove worktree
 echo -e "${BLUE}📂 Removing worktree...${NC}"
+cd "$MAIN_WORKTREE"
 git worktree remove "$WORKTREE_PATH" --force

Alternatively, reorder the script so worktree removal is the last step (delete branches and update main first, then git worktree remove). This is cleaner but changes user-visible ordering.

Notes

  • templates/scripts/cleanup-worktree.sh is the canonical source per reference_scripts_layout.md. scripts/dev/cleanup-worktree.sh is a symlink and scripts/dev/ is gitignored — only templates/scripts/ needs to be patched.
  • Adding cd "$MAIN_WORKTREE" is also defensive against an alternative repro: caller in any sub-directory of the worktree (not just the worktree root).

Acceptance Criteria

  • AC-1: Running cleanup-worktree.sh <branch> from inside the worktree being deleted completes all 5 steps (worktree remove, local branch delete, remote branch delete, checkout main, fetch+pull) without Unable to read current working directory errors.
  • AC-2: After the script exits, git worktree list does not include the deleted worktree, and git branch --list <branch> and git ls-remote origin <branch> both return empty.
  • AC-3: Behavior unchanged when called from the main repo root (existing happy path).

Activity

  1. added
    bug
    enhancement
    on May 4, 2026
  2. admarble commented on May 6, 2026

    @admarble
    ContributorAuthor

    Plan Complete

    AC Checklist

    • AC-1: Script completes all 5 cleanup steps when invoked from inside the worktree being deleted (no Unable to read current working directory error)
    • AC-2: Post-run, git worktree list excludes the deleted path; git branch --list <branch> and git ls-remote origin <branch> both empty
    • AC-3: Existing happy path unchanged when called from main repo root

    Verification

    AC Method Test Scenario
    AC-1 Manual / shell cd <worktree>; cleanup-worktree.sh <branch> — all 5 steps succeed
    AC-2 Shell assertion git worktree list / git branch --list / git ls-remote all confirm clean state
    AC-3 Manual / shell cd <main-repo>; cleanup-worktree.sh <branch> — output and behavior unchanged

    Implementation Plan

    Phase 1: Patch templates/scripts/cleanup-worktree.sh

    1. Capture main worktree path via git worktree list --porcelain after line 16.
    2. cd "\$MAIN_WORKTREE" before line 76 (git worktree remove ... --force).
    3. scripts/cleanup-worktree.sh is a symlink → no second edit needed.

    Phase 2: Verify via bash -n syntax check + read-back.

    Design Decisions

    • Porcelain over head|awk: robust against paths with spaces, documented machine-readable interface.
    • Capture early, cd late: keeps user-visible step order identical to AC-3.
    • No new test harness: no existing tests for these scripts; AC-1 repro is the test.

    Recommended Workflow

    spec → exec → qa (no /test, quality loop disabled).

    Ready to implement.

  3. added
    planned
    on May 6, 2026
  4. admarble commented on May 6, 2026

    @admarble
    ContributorAuthor

    QA Review

    AC Coverage (live-verified)

    AC Status Evidence
    AC-1 (cwd inside deleted worktree) MET cd ../worktrees/test-cleanup-575 && cleanup-worktree.sh test-cleanup-575 → exit 0, all 5 steps complete, no Unable to read current working directory error
    AC-2 (post-run clean state) MET git worktree list | grep -c test-cleanup-575 = 0; git branch --list test-cleanup-575 empty; git ls-remote origin test-cleanup-575 empty
    AC-3 (happy path unchanged) MET cleanup-worktree.sh test-cleanup-575b → exit 0, output identical to pre-patch

    Quality Metrics

    • Files changed: 1 (templates/scripts/cleanup-worktree.sh)
    • Diff: +9 lines, −0 lines
    • Type issues: N/A (shell script)
    • Deleted tests: 0

    Code Review

    • Capture MAIN_WORKTREE early via git worktree list --porcelain (machine-readable, not the formatted form).
    • cd "$MAIN_WORKTREE" happens between the "Removing worktree..." banner and the destructive git worktree remove, preserving the user-visible step ordering for AC-3.
    • Comments explain why (the destructive remove invalidates cwd) — not what.
    • set -e ensures hard fail if cd ever fails. MAIN_WORKTREE is guaranteed non-empty by line 84 since git worktree list already executed successfully on lines 29/34.

    Self-Evaluation

    • Verified working: Yes — ran the exact issue-body repro live, twice (cwd inside / cwd at main).
    • Test efficacy: High — AC-1 repro reproduces the original bug pre-patch; passes post-patch.
    • Likely failure mode: paths containing whitespace would break awk '$2'. Pre-existing limitation in line 34 (worktree-path lookup), not introduced here. Sequant's own worktree naming convention never produces such paths.
    • Verdict confidence: High.

    Verdict: READY_FOR_MERGE

  5. admarble commented on May 6, 2026

    @admarble
    ContributorAuthor

    /fullsolve Complete

    Issue: #575
    Status: READY_FOR_MERGE
    PR: #579

    Summary

    • AC: 3/3 met (live-verified with repro of the exact issue-body failure path)
    • Tests: shell-syntax check + 2 live repros (AC-1 + AC-3) — all pass
    • Files changed: 1 (templates/scripts/cleanup-worktree.sh, +9/-0)

    Iterations

    • Spec: 1
    • Exec: 1
    • QA: 1 (no /loop)

    Risk Assessment

    • Likely failure mode: worktree paths with whitespace would still break awk '$2'; pre-existing limitation on line 34, not introduced here. Sequant's own worktree naming never produces such paths.
    • Not tested: automated regression harness for templates/scripts/*.sh — repo has none. AC-1 repro is the test.

    Ready for human review and merge.

  6. added 3 commits that reference this issue on May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugenhancementplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions