Repository navigation
fix(hooks): render CodeBuddy's Windows hooks in POSIX, not cmd.exe - #989
Conversation
CodeBuddy runs a hook's `command` string through Git Bash on Windows — it dropped PowerShell/CMD for hooks in 2.52.4 and forces Git Bash since 2.55.1 — so the cmd-syntax commands added in Tencent#637 misbehave there: - `2>nul` is a file redirect for bash, not the null device, so every hook invocation leaves a 0-byte file named `nul` in the hook's working directory. PostToolUse (matcher `*`) fires after every tool call, so it reappears continuously in the project root, and a reserved device name cannot be removed with `del` or Explorer. - `set "PATH=…;%PATH%"` never prepends PATH under bash. - `|| exit /b 0` makes bash exit 2 on a failing dispatch, which CodeBuddy reads as `allowed:false` and would block a UserPromptSubmit with. The project gate is affected the same way: `cd| findstr … >nul` run by Git Bash errors on `findstr`, exits non-zero and leaves another `nul`. Render the POSIX wrapper form for CodeBuddy, which leaves the cmd renderer unused: drop it, and resolve CodeBuddy's shell to Git Bash the way WorkBuddy's resolver already asks for its bundled MSYS sh. Null when Git Bash is absent, which skips injection with the existing warning instead of installing hooks that could never run. Legacy cmd gates stay recognised on read, so entries written by 0.26.0 are replaced on the next pull rather than duplicated.
|
|
Confirmed — this is a real defect, not a false positive, and the coverage gap it points at was mine. I reproduced it before changing anything: The second half of your point is the one that stung: the PR replaced Fixed in
Two things worth knowing about the new test, in case they bite anyone else:
One thing worth flagging for scope: Not covered, and unchanged from before: WSL ( |
|
The gate tests `$PWD`, and an MSYS shell spells a drive-letter path `/c/proj`, never the `C:/proj` a Windows root resolves to. A gate rendered in the native spelling only therefore never matched, so every project-scoped team hook silently never ran. Render the root in both spellings: the native one, which is what earlier versions wrote and what the read side keeps recognising, so a re-render replaces those entries instead of stacking beside them, plus the MSYS one the runner's `$PWD` actually holds. Drive roots lose their trailing separator (`C:/` -> `/c`). A root without a drive letter (Linux, macOS) is unchanged and the gate still carries a single test there. Restore the execution coverage this PR had dropped. The cmd.exe gate used to be executed for real by `project gate — real cmd.exe execution (win32)`; replacing that with shape assertions is what let a gate that could never match pass. Run the rendered gate through the runner's shell — from inside and outside the project, over directory names carrying the characters that would split the command if the root were not quoted. Two environment notes on that test: it builds its directories outside `os.tmpdir()`, because MSYS mounts the Windows temp directory at `/tmp` and a root under it is reported as `/tmp/...`, where no native-path gate can match; and it spawns asynchronously, because a sandbox can fail the synchronous spawn with EBUSY, which would show up as an empty stdout — the signature of a gate that never matched.
01cb44e to
3bbe9d2
Compare
The earlier native drive-path mismatch is resolved. The PR description includes sufficient representative real-CLI verification. |
|
Heads-up on the one red check, so it is not misread as coming from this PR.
That single error is an unhandled rejection, and Vitest names its origin: A lock write racing the fixture teardown — Worth flagging that this matrix is not clean on A re-run of that job should clear it; I can't trigger one from a fork. Happy to rebase if |
…encent#989) * fix(hooks): render CodeBuddy's Windows hooks in POSIX, not cmd.exe CodeBuddy runs a hook's `command` string through Git Bash on Windows — it dropped PowerShell/CMD for hooks in 2.52.4 and forces Git Bash since 2.55.1 — so the cmd-syntax commands added in Tencent#637 misbehave there: - `2>nul` is a file redirect for bash, not the null device, so every hook invocation leaves a 0-byte file named `nul` in the hook's working directory. PostToolUse (matcher `*`) fires after every tool call, so it reappears continuously in the project root, and a reserved device name cannot be removed with `del` or Explorer. - `set "PATH=…;%PATH%"` never prepends PATH under bash. - `|| exit /b 0` makes bash exit 2 on a failing dispatch, which CodeBuddy reads as `allowed:false` and would block a UserPromptSubmit with. The project gate is affected the same way: `cd| findstr … >nul` run by Git Bash errors on `findstr`, exits non-zero and leaves another `nul`. Render the POSIX wrapper form for CodeBuddy, which leaves the cmd renderer unused: drop it, and resolve CodeBuddy's shell to Git Bash the way WorkBuddy's resolver already asks for its bundled MSYS sh. Null when Git Bash is absent, which skips injection with the existing warning instead of installing hooks that could never run. Legacy cmd gates stay recognised on read, so entries written by 0.26.0 are replaced on the next pull rather than duplicated. * fix(hooks): compare the project gate in the shell's path spelling The gate tests `$PWD`, and an MSYS shell spells a drive-letter path `/c/proj`, never the `C:/proj` a Windows root resolves to. A gate rendered in the native spelling only therefore never matched, so every project-scoped team hook silently never ran. Render the root in both spellings: the native one, which is what earlier versions wrote and what the read side keeps recognising, so a re-render replaces those entries instead of stacking beside them, plus the MSYS one the runner's `$PWD` actually holds. Drive roots lose their trailing separator (`C:/` -> `/c`). A root without a drive letter (Linux, macOS) is unchanged and the gate still carries a single test there. Restore the execution coverage this PR had dropped. The cmd.exe gate used to be executed for real by `project gate — real cmd.exe execution (win32)`; replacing that with shape assertions is what let a gate that could never match pass. Run the rendered gate through the runner's shell — from inside and outside the project, over directory names carrying the characters that would split the command if the root were not quoted. Two environment notes on that test: it builds its directories outside `os.tmpdir()`, because MSYS mounts the Windows temp directory at `/tmp` and a root under it is reported as `/tmp/...`, where no native-path gate can match; and it spawns asynchronously, because a sandbox can fail the synchronous spawn with EBUSY, which would show up as an empty stdout — the signature of a gate that never matched.
Summary
CodeBuddy runs its Windows hook commands through Git Bash, but teamai rendered them in cmd.exe syntax. Run by Git Bash,
2>nulis a file redirect, not the null device, so every hook invocation created a real 0-byte file namednulin the hook's working directory — and because thePostToolUsewildcard hook fires after every tool call, the project root got one continuously. The same rendering also made the PATH prepend a no-op and turned the deliberate "always exit 0"|| exit /b 0into an exit 2, which is exactly what makes CodeBuddy read a hook asallowed:false.This makes CodeBuddy use the POSIX renderings — built-in dispatch and the team-hook project gate — like every other tool, and resolves its shell to Git Bash.
The gate that shipped with the first version of this PR still could not fire. It compares
$PWD, and an MSYS shell spells a drive-letter path/c/proj, never theC:\projthe root resolves to — so every project-scoped team hook stayed disabled. That is the review finding below, fixed in the follow-up commit01cb44e: the gate is now rendered in both spellings, and a real-shell execution test covers it. Replacing the original execution test with shape assertions is what had let the broken gate pass; that coverage is restored.Fixes #932.
Type of Change
What changed
Drop the cmd.exe renderer.
toolUsesCmdShell()and the cmd branch ofgateTeamHookCommand()are gone; every tool now gets the POSIX gate. CodeBuddy resolves its hook shell through the Git Bash it already requires on Windows (resolveCodebuddyShell()→findGitBashWindows()), returning null — which correctly skips injection with the existing warning — when Git Bash is absent, instead of installing hooks that could never run.Render the gate root in the shell's spelling (
01cb44e).msysProjectRoot()rewrites a drive-letter root to what MSYS reports (C:\proj→/c/proj,C:\→/c), and the gate tests both that and the native form. Keeping the native form is what lets a re-render recognise and replace the entries earlier versions wrote instead of stacking a second gate beside them; a root without a drive letter (Linux, macOS) is unchanged, and the gate still carries a single test there.Keep the legacy cmd gate readable.
cmdProjectGate()stays as a read-side recogniser inisGatedForProject(): entries written by 0.26.0 are still on disk, and matching them is what lets a re-render replace the old gate rather than stack a second one on top.Preserve the Cursor/Copilot wrapper.
skipWhenAnotherHostLoadsClaudeSettings()(#950) is orthogonal and still keyed ontool; only the gate lost its per-tool form. Resolving the conflict kept it intact.Test Plan
npx tsc --noEmit— passesnpm run lint— 0 warnings, 0 errors on 811 files. Note:--type-aware(tsgolint) could not run in my sandbox — its subprocess fails to spawn withos error 231(all pipe instances busy), which is an environment limit rather than a code signal, so that half is unverified here.npx vitest runon the 4 test files touching this area — 67 passed, 3 skipped, 0 failed (hooks-golden,hooks-shell-check,hooks-windows-bash,hooks-reconcile-scope). The full suite is not a usable gate on Windows; unrelated pre-existing/bin/sh,chmodand file-mode cases fail identically onmain.The project gate — real-shell execution
src/__tests__/hooks-reconcile-scope.test.tsruns a freshly rendered gate through the runner's shell and asserts what the harness has to see: the payload runs inside the project (root and subdirectory) and not outside it (the parent and a sibling that merely shares the prefix), with exit 0 either way — a non-zero hook status is what CodeBuddy reads asallowed:false. Directory names carrying&, a space and an injection canary are covered, so the quoting is exercised too.Two things that test needs, both worth knowing:
os.tmpdir(). MSYS mounts the Windows temp directory at/tmp, so a root under it is reported as/tmp/...and no gate rendered from its native path can match there. The cases build undernode_modules, which is on the workspace drive like a real project and is never committed.EBUSY, which surfaces as an empty stdout — indistinguishable from a gate that never matched. (This is what made the first version of this test look like a logic failure.)Environment-independent gate setup
The block used to assert the rendered gate by reading back
~/.codebuddy/settings.json, but never staged a shell for the mockedwin32platform — so whether the case ran at all depended on the developer's Git install location. On a machine where Git lives outsideProgramFiles/LOCALAPPDATAand can only be found via theHKLM\SOFTWARE\GitForWindowsregistry key,findGitBashWindows()returned null,skipToolsWithoutShell()dropped both tools, and the read failed with ENOENT.It now clears the three environment variables and stages both candidates under the mocked home (CodeBuddy's
bash.exe, WorkBuddy's PortableGitsh.exe), matching whathooks-shell-check.test.tsalready does, so the setup is the same on any developer box and on CI.Real-CLI end-to-end verification
Built with
npm run build, then ran the built CLI against a throwaway HOME (a temp dir) with an already-initialised config and a local team repo, so the real injection path ran:codebuddyis no longer skipped (its Git Bash resolves), and<sandbox>\.codebuddy\settings.jsonnow holds:Before this change the same run produced (captured from a real Windows machine, and pinned by the old assertions):
2>nulunder Git Bash creates a file namednul;set "PATH=…;%PATH%"never prepends anything;|| exit /b 0exits 2 on a failing dispatch, which CodeBuddy reads asallowed:false.The project gate moved the same way. It now carries the shell's spelling of the root, which is the part that makes it match at all:
Notes for review
allowed:falseand block everyUserPromptSubmitoutside the project.gateTeamHookCommand()is shared by every tool, so the path-spelling fix is not codebuddy-specific. On Windows the POSIX gate was already whatclaude,workbuddy,codexand the rest were given (toolUsesCmdShell()was true only for codebuddy), which means their project-scoped team hooks were silently disabled on Windows too, for as long as the POSIX gate has existed. Those tools get working gates from this change as well; before it, only codebuddy's cmd gate matched.${gate} || exit /b 0 && (…): the||would swallow a genuine payload failure along with the mismatch. With only one rendering left, the POSIXif …; then …; figives that pass-through directly./mnt/c/...) and Cygwin (/cygdrive/c/...) mount points, and UNC roots (\\server\share), which pass through unchanged. They do not match today either, so nothing regresses; a native POSIX root (Linux, macOS) is unaffected.