Repository navigation
Run environment setup and teardown hooks on native Windows - #4658
Conversation
`.bb-env-setup.sh` and `.bb-env-teardown.sh` threw on Windows, so a repository with a setup hook could not provision a worktree there. Run the scripts with the bash that Git for Windows installs, resolved through `git var GIT_SHELL_PATH`, with that directory first on PATH so coreutils resolve. Stop the whole process tree on timeout or cancellation. Fail with a message naming Git for Windows when no bash is found. Co-Authored-By: Claude Code <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4a5642086c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| void stopProcessGroupLeaderFirst({ | ||
| child, | ||
| timeoutMs: 0, | ||
| killGraceMs: 0, | ||
| }).catch(() => undefined); |
There was a problem hiding this comment.
Await Windows process-tree termination before cleanup
When taskkill.exe fails or exceeds its internal timeout, stopWindowsProcessTree reports treeTermination: "unverified" after killing only the root process, but this fire-and-forget call ignores both that result and any rejection. Because isProcessGroupAlive always returns false on Windows, runLifecycleScript can then finish as soon as Bash closes, causing cancelEnvironmentHook to report terminated and allowing workspace cleanup while descendant processes remain active. Retain and await the stop promise, and treat unverified termination as a cleanup-blocking failure; this Windows failure path should also be covered by a test.
AGENTS.md reference: AGENTS.md:L49-L49
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4a5642086c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| windowsBashPath: | ||
| process.platform === "win32" ? await resolveWindowsBashPath(env) : null, |
There was a problem hiding this comment.
Recheck cancellation before spawning the Windows hook
If provisioning is cancelled while resolveWindowsBashPath is awaiting git var (which may run for up to 10 seconds), the earlier abort check has already passed and this code proceeds to spawn the setup script. The later args.signal?.aborted check only starts terminating it after launch, so a cancelled setup hook can briefly execute and produce side effects, while cancellation is also delayed by the uncancelled Git lookup. Pass the signal into the lookup and/or call throwIfProvisionAborted again before spawning the hook.
Useful? React with 👍 / 👎.
A cancelled or timed-out hook started the tree termination without waiting for it, so cancellation could report the hook as terminated while its descendants were still running. Await the stop, and report an unconfirmed termination as an unknown outcome so cleanup stays blocked. Pass the abort signal to the Git shell lookup and check it again before spawning. Co-Authored-By: Claude Code <[email protected]>
Git Bash starts programs such as sleep through a helper process that exits, so Windows no longer links them to the hook and a tree kill from the hook's root misses them. They kept the output pipes open and the cancelled hook never returned. The hook runner now asks Git Bash for the members of the hook's process group, stops each member's tree, repeats until the group is empty, and gives up with an unverified result when output stays open. Co-Authored-By: Claude Code <[email protected]>
Human comments
What was wrong
buildLifecycleScriptCommandthrewPOSIX shell setup scripts are not supported on Windowsfor every.bb-env-setup.shand.bb-env-teardown.sh. Setup failure fails provisioning, so on a native Windows host any repository that commits a setup hook could not start a worktree thread. Part of #1206.What changed
apps/host-daemon/src/environment-lifecycle-script.ts: on Windows the hook runs as<bash> <script>where bash is the one Git for Windows installs.resolveWindowsBashPathasksgit var GIT_SHELL_PATH(the same lookuprunShellPipelinealready uses), prefersbash.exein that directory, and falls back to the reported shell. That directory is put first on the hook'sPATH, because a non-login Git bash does not otherwise findhead,touch, orbashitself.sleepthrough a helper process that exits, so Windows no longer links them to the hook andtaskkill /Tfrom the hook's root misses them; they also keep the output pipes open, so the hook never returned. Newapps/host-daemon/src/git-bash-process-group.tsasks Git Bash for the members of the hook's process group (/proc/<pid>/pgidandwinpid), stops each member's tree withtaskkill, and repeats until the group is empty. The POSIX path still kills the process group.LifecycleScriptTerminationUnverifiedErrorandenvironment.hook.cancelanswersunknown, which the server already treats as cleanup-blocking. Before, the stop result was dropped and the cancel answeredterminated.packages/process-utils:terminateWindowsProcessTrees(the existingtaskkillcall, taking several roots) is exported for the above.<script> needs Git for Windows: …instead of the old unsupported-platform message.docs/worktrees.md,docs/platform-support.md, and thebb guide environmentstemplate describe the Windows behavior.macOS and Linux still run
env bash <script>; the command text and environment there are unchanged. No wire or CLI changes.How you verified
.bb-env-setup.sh(echo hook-ran-in $(pwd) with $(bash --version | head -1), thentouch hook-marker.txt) provisioned a worktree thread. The thread log showedhook-ran-in /c/bb-data/…/testrepo with GNU bash, version 5.3.15,.bb-env-setup.sh finished (159ms), andhook-marker.txtexisted in the worktree.PATHchange failed withhead: command not foundand exit code 127, which is what the directory prefix fixes.vitest run src/environment-lifecycle-script.test.tsinapps/host-daemonon Windows: 13 of 13 pass, including the setup and teardown timeout tests, the cancel test, and a new Windows-only test whose hook starts a backgroundsleep 120and a backgroundnodeprocess, is cancelled, and then finds neither process alive. Before the process-group change the timeout and cancel tests each ran into the 15 second test timeout, and asleep.exewhose parent process no longer existed was still running aftertaskkill /T /Freported success.sleep 300 &andsleep 300showed twosleep.exeprocesses;bb thread stopleft none, and nobash.exe, 800 ms later.turbo run lint typecheck test --filter=@bb/host-daemon --filter=@bb/process-utilspasses (10 of 10 tasks). The unit tests cover the Windows command, itsPATHprefix, the missing-bash message, the rule that only a confirmed stop is accepted, and that a cancel whose stop is unconfirmed answersunknown.Not verified: the teardown hook on Windows through the app (only through
runTeardownScriptin the tests above); a hook that leaves a background program running after its own shell exits within the first moments of the run, where the process group lookup may not have finished and the stop reports unverified. The Windows-only test runs only on a Windows machine; CI does not run the host daemon tests on Windows.🤖 Generated with Claude Code