Repository navigation
fix(wt create): keep worktree when init script fails; add typed exit code - #7
Merged
Merged
Conversation
When the worktree init script (default `fab sync`, overridable via WORKTREE_INIT_SCRIPT) exits non-zero, `wt create` previously executed the rollback — destroying the just-created worktree, branch, and any fetched refs — and exited with the generic ExitGeneralError (1). The banner pointed nowhere useful and operators could not distinguish init failure from other generic errors. This change treats init-script failure as a kept-worktree event. All five interlocking DX problems land together so the failure path is internally consistent: - create.go disarms the rollback on init failure (both auto-init and prompted branches). The worktree, branch, and fetched refs survive. - New structured banner prints the extracted exit status, absolute worktree path, retry hint (`cd <path> && wt init`), and remove hint (`wt delete <name>`). - New typed exit code ExitInitFailed = 7 (appended after ExitTmuxWindowError = 6 — no renumbering). - Unified resolver: ResolveInitInvocation + InitNotFound typed struct replace duplicated lookup logic between `wt init` and `wt create`. Both call sites now print the same verbose not-found warning. - SIGINT during init kills only the init child via reinstalled handler (Option B) + Setpgid process-group signaling, then routes through the kept-worktree code path. Phase-A (git ops) SIGINT still rolls back as before. - OpenInApp gains a WT_TEST_NO_LAUNCH=1 test seam (default ON in runWt) so test runs no longer leak real VSCode/Cursor/iTerm windows. This is a behavior-changing default fix — no opt-in flag, no backward-compat shim. Treated as a bug, not a configurable preference. Spec: docs/specs/init-protocol.md (Script failure semantics rewritten) Memory: docs/memory/wt-cli/init-failure-contract.md (new)
There was a problem hiding this comment.
Pull request overview
This PR changes wt create init-failure handling so successful git worktree creation is preserved when the user-supplied init script fails, while adding clearer diagnostics, a typed exit code, shared init resolution, and safer test behavior around external app launches.
Changes:
- Keeps newly created worktrees/branches on init-script failure and exits with
ExitInitFailed = 7. - Adds a shared
ResolveInitInvocationpath forwt initandwt create, plus structured not-found warnings and an init-failure banner. - Adds SIGINT handling for the init phase, integration/unit coverage, documentation, memory notes, and
WT_TEST_NO_LAUNCHtest isolation.
Reviewed changes
Copilot reviewed 26 out of 26 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
src/internal/worktree/init.go |
Adds shared init resolver and typed not-found rendering. |
src/internal/worktree/init_windows.go |
Adds Windows no-op process-group helper. |
src/internal/worktree/init_unix.go |
Adds Unix Setpgid setup for init child process groups. |
src/internal/worktree/init_test.go |
Adds resolver and warning tests. |
src/internal/worktree/errors.go |
Adds ExitInitFailed and init-failure banner. |
src/internal/worktree/errors_test.go |
Adds banner and exit-code tests. |
src/internal/worktree/crud.go |
Refactors setup execution through the shared resolver and observer hook. |
src/internal/worktree/apps.go |
Adds WT_TEST_NO_LAUNCH guard for tests. |
src/internal/worktree/apps_test.go |
Tests the no-launch seam. |
src/cmd/wt/testutil_test.go |
Defaults test runs to no-launch mode. |
src/cmd/wt/signal_windows.go |
Adds Windows no-op signal helper. |
src/cmd/wt/signal_unix.go |
Adds Unix process-group SIGINT delivery. |
src/cmd/wt/open_test.go |
Updates default-app expectations for no-launch mode. |
src/cmd/wt/integration_test.go |
Adds init-failure and SIGINT integration coverage. |
src/cmd/wt/init.go |
Switches wt init to the shared resolver. |
src/cmd/wt/init_test.go |
Adds canonical warning consistency tests. |
src/cmd/wt/create.go |
Keeps worktree on init failure and installs init-phase signal handling. |
src/cmd/wt/create_test.go |
Adds create init-failure tests and no-launch assertions. |
docs/specs/init-protocol.md |
Documents shared resolution and new failure semantics. |
docs/memory/index.md |
Adds wt-cli memory index entry. |
docs/memory/wt-cli/init-failure-contract.md |
Captures the new init-failure behavior contract. |
fab/changes/260516-g5e7-wt-create-init-failure-dx/intake.md |
Adds change intake artifact. |
fab/changes/260516-g5e7-wt-create-init-failure-dx/spec.md |
Adds implementation spec artifact. |
fab/changes/260516-g5e7-wt-create-init-failure-dx/plan.md |
Adds task and acceptance plan artifact. |
fab/changes/260516-g5e7-wt-create-init-failure-dx/.status.yaml |
Adds change status metadata. |
fab/changes/260516-g5e7-wt-create-init-failure-dx/.history.jsonl |
Adds change history metadata. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+50
to
+52
| sysAttr, ok := any(cmd.SysProcAttr).(*syscall.SysProcAttr) | ||
| if !ok || !sysAttr.Setpgid { | ||
| t.Error("expected SysProcAttr.Setpgid = true on Unix") |
| fmt.Fprintf(os.Stderr, " %s%s%s %s %s\n", | ||
| ColorBold, bannerLabelWorktree, ColorReset, wtPath, bannerKeptNote) | ||
| fmt.Fprintf(os.Stderr, " %s%s%s cd %s && wt init\n", | ||
| ColorBold, bannerLabelRetry, ColorReset, wtPath) |
Comment on lines
+273
to
+277
| mode := "force" | ||
| if !(nonInteractive || branchArg == "") { | ||
| mode = "" // prompt for init in interactive existing-branch flow | ||
| } | ||
| if err := wt.RunWorktreeSetupWithObserver(wtPath, mode, initScript, ctx.RepoRoot, captureInit); err != nil { |
Comment on lines
+310
to
+312
| // Put the wt process in its own group so killing the group reaps any | ||
| // orphans if the test fails partway. | ||
| cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
Comment on lines
+52
to
+53
| cmd := exec.Command("sh", "-c", fmt.Sprintf("exit %d", status)) | ||
| err := cmd.Run() |
Comment on lines
+122
to
+123
| (warn-but-continue) rather than a gate. `wt init` also exits with | ||
| `ExitInitFailed` when the init script it located exits non-zero. |
Six findings from Copilot review on PR #7: 1. Windows compile break (init_test.go, integration_test.go): syscall.SysProcAttr.Setpgid does not exist on Windows; the runtime.GOOS guard fires too late, after compilation has already failed. Extracted Setpgid assertions and the SIGINT integration test into build-tagged files (init_unix_test.go, init_windows_test.go, integration_sigint_unix_test.go). 2. Banner retry hint not shell-safe: cd <path> && wt init broke when the worktree path contained spaces ('/tmp/my repo/wt' -> /tmp/my). Now wrapped in single quotes via shellQuoteSingle for both path and name. Added regression test TestPrintInitFailureBanner_PathWithSpaces. 3. SIGINT handler installed before interactive confirmation prompt: in the interactive existing-branch flow, RunWorktreeSetup's ConfirmYesNo ran AFTER the signal swap, so Ctrl-C during the prompt was consumed by the init handler with no init child to target (deadlock). Hoisted the prompt up to cmd/wt/create.go so it runs under the original rollback handler; signal swap happens only after the prompt is accepted, with no I/O between. Dropped the now-unused 'mode' parameter from RunWorktreeSetup{,WithObserver}. 4. errors_test.go's makeExitError depended on 'sh' being on PATH, making the test fail on Windows / minimal containers. Added a t.Skipf when exec.LookPath("sh") fails — the seam being tested (banner shape) is OS-agnostic; only the fixture needs sh. 5. init-protocol.md documented wt init as exiting ExitInitFailed but the implementation returned an error to Cobra which mapped to ExitGeneralError = 1. Made runInitScript exit os.Exit(ExitInitFailed) directly on init-script failure, matching the documented contract and wt create's behavior. 6. Updated memory file docs/memory/wt-cli/init-failure-contract.md to reflect the prompt hoisting and wt init exit-code change. All tests pass on Unix; Windows test compile is clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Meta
Pipeline: intake ✓ → spec ✓ → apply ✓ → review ✓ → hydrate ✓ → ship → review-pr
Impact: +984/−70 code (excluding
fab/,docs/) · +2137/−76 totalSummary
Today, when
wt create <branch>runs the init script and the script exits non-zero, the user loses their worktree, their branch, and any fetched refs — and gets a generic two-line error pointing them nowhere. All git operations succeeded; only the user-supplied init script failed. This change treats init-script failure as a kept-worktree event with a structured banner, a typed exit code, a unified resolver, and SIGINT semantics that only stop the init child. No opt-in flag, no backward-compat shim — the prior rollback-on-init-failure is treated as a bug, not a configurable preference.Changes
src/cmd/wt/create.go).cd <path> && wt init), and remove hint (wt delete <name>).ResolveInitInvocation+InitNotFoundtyped struct replace duplicated lookup logic betweenwt initandwt create. Same verbose not-found warning at both call sites.ExitInitFailed = 7— appended afterExitTmuxWindowError = 6; no renumbering. Operators can now distinguish "worktree exists, init didn't complete" from other failures.Also bundled:
WT_TEST_NO_LAUNCH=1test seam inOpenInApp(default ON inrunWt) to prevent test runs from leaking real VSCode/Cursor/iTerm windows.Artifacts