Repository navigation
Description
Describe the bug
When a project hook runs something that triggers vp config (typically pnpm install from post-checkout / post-merge, with "prepare": "vp config"), and the regenerated .vite-hooks/_/h differs from the one on disk, the hook fails after the user script finishes:
.vite-hooks/_/post-checkout: 33: .vite-hooks/_/h: Syntax error: "(" unexpected
We hit this switching between branches that pin vite-plus 1.5.0 and 1.8.0 (their dispatchers differ), with a post-checkout hook that runs pnpm install when the lockfile changes.
Cause: the generated h does not end with a newline (the template ends in exit $c`), and install() rewrites it in place with writeFileSync (same inode). /bin/sh (dash here) sources h, reads it into its buffer, and runs the user hook. When it reaches the final exit $c it has no terminating newline, so it calls read() again on the same fd at the old EOF offset. By then the file has been rewritten, so dash reads the tail of the new h and appends it to exit $c, producing garbage and a syntax error. The exact message depends on the bytes past the old length ("(" unexpected, Unterminated quoted string, …).
The install itself succeeded; only the hook's exit status and the error output are wrong, but git checkout returns non-zero.
Reproduction
Inline script below (no external repo needed).
Steps to reproduce
Linux with /bin/sh → dash (Debian/Ubuntu default):
mkdir repro && cd repro && git init -q
echo '{"name":"repro","private":true}' > package.json
vp config --no-agent
cat > .vite-hooks/post-checkout <<'EOF'
#!/usr/bin/env sh
# Stand-in for `pnpm install` whose `prepare: vp config` writes a different
# (here: longer) dispatcher, as when switching between branches that pin
# different vite-plus versions.
{ echo '# simulated newer dispatcher'; cat .vite-hooks/_/h; } > .vite-hooks/_/h.new
cat .vite-hooks/_/h.new > .vite-hooks/_/h
EOF
chmod +x .vite-hooks/post-checkout
git add -A && git commit -qm init --no-verify
git checkout -b other
# .vite-hooks/_/post-checkout: 35: .vite-hooks/_/h: Syntax error: Unterminated quoted stringAppending a trailing newline to .vite-hooks/_/h (echo >> .vite-hooks/_/h) and repeating the checkout makes it pass, because dash then finishes parsing exit $c from its buffer and exits without reading again.
Suggested fix
Either (ideally both):
- End the generated
h(and the per-hook stubs) with\n. - Write
hatomically: write to a temp file in_/andrenameSyncit overh, so a shell that already has the old file open keeps reading the old inode.
Workaround for users: run the install inside the hook with VP_GIT_HOOKS=0 pnpm install so prepare does not reinstall the dispatcher mid-hook.
System Info
vp v1.8.0 (local vite-plus v1.8.0; no global vp installed, so `vp env current` is unavailable)
Also reproduced with the dispatcher generated by vite-plus 1.5.0.
OS: Ubuntu, Linux 6.8 x64
/bin/sh: dash 0.5.12
git 2.43.0
node v24.16.0Used Package Manager
pnpm
Logs
.vite-hooks/_/post-checkout: 33: .vite-hooks/_/h: Syntax error: "(" unexpectedMetadata
Metadata
Assignees
Labels
Type
Fields
Priority
Effort
Target date
Start date
Activity
voidzero-bot commentedon Nov 24, 2026
packages/cli/src/config/__tests__/hooks.spec.tsprepare: vp config+ install-in-hook workflow (e.g.pnpm installfrompost-checkout/post-merge) on shells like dash, and the install itself still succeeds — only the hook's exit status andgit checkout's exit code are wrong. A straightforward workaround exists (VP_GIT_HOOKS=0 pnpm installinside the hook, or appending a trailing newline to.vite-hooks/_/h).The generated
.vite-hooks/_/hdispatcher ends withexit $cwithout a trailing newline, andinstall()inpackages/cli/src/config/hooks.tsrewrites it in place withwriteFileSync(same inode) on everyvp configrun. When a hook triggers an install whosepreparerunsvp config, dash — which sourcedhand is waiting on the user hook — re-reads the open file past the old EOF because the final line is unterminated, appends bytes of the new dispatcher toexit $c, and dies with a syntax error. This was confirmed by the issue's exact reproduction (checkout exits 1,Unterminated quoted string), a passing control case with the trailing-newline workaround, and a minimal standalone dash experiment isolating the read-past-EOF behavior.I found a potential fix for this issue. The branch above makes
hookScript()andHOOK_STUB_SCRIPTend with(so the shell never re-reads past the old EOF) and replaceshand the hook stubs atomically (temp file +renameSync, with an in-place fallback where the rename cannot replace the target), so a shell that already sourced the old dispatcher keeps reading the old inode. Three regression tests were added; all fail before the fix and pass after it, and the original reproduction now exits 0. This suggestion still needs human review before merging.Full Triage Report
Triage Report — Issue #3567
Issue Summary
Title: Git hook dispatcher fails with a shell syntax error when
vp configrewrites.vite-hooks/_/hduring a hookReporter: salmundani
Report: When a project hook (e.g.
post-checkoutrunningpnpm installwithprepare: vp config) causesvp configto regenerate.vite-hooks/_/h, and the regenerated dispatcher differs from the one on disk, the hook fails after the user script finishes with a syntax error such as:or
Unterminated quoted string, andgit checkoutexits non-zero.Expected: the hook and
git checkoutcomplete successfully (exit 0).Actual: the hook's shell (
dash) reports a syntax error on.vite-hooks/_/handgit checkoutexits 1.Environment
vp v1.8.0(local vite-plus v1.8.0, built from the current checkout; no globalvp)/bin/sh→ dash (reporter: dash 0.5.12)Reproduction
/tmp/vite-plus-triage/gh-3567/repro— inline script from the issue, run verbatim.git init -q,package.json,vp config --no-agent(exit 0).Inspection:
.vite-hooks/_/h(1057 bytes) ends withexit $cwithout a trailing newline; the stubs also lack one.Added the reported
post-checkouthook (stand-in forpnpm install+prepare: vp configwriting a longer dispatcher), committed, thengit checkout -b other:Exit code: 1 — matches the report (the exact message depends on the bytes read past the old EOF).
Control — regenerated
h, appended the reporter's trailing-newline workaround, repeated the checkout: exit 0. The control rules out an unrelated hook failure.Result: Reproduced with the current checkout's
vp(v1.8.0 local build); reproduction does not depend on the reporter's older release.Diagnosis
All in
packages/cli/src/config/hooks.ts(local JS CLI,vp configpath):hookScript(dir)— the dispatcher template ends withexit $c`with no trailing newline.HOOK_STUB_SCRIPT—#!/usr/bin/env sh . "$(dirname "$0")/h"also has no trailing newline.install()— rewrites the dispatcher in place on every run withwriteFileSync(same inode, no atomic replace).vp config(packages/cli/src/config/bin.ts) runs on everyprepare/install, so a hook-triggered install rewriteshwhile the running hook shell still has the oldhopen.The templates have ended without a newline since the dispatcher was introduced in #674 (
29c41a6a6) — a long-standing latent bug, not a recent regression.Root cause (confirmed): the stub
_/post-checkoutis executed by git with/bin/sh(dash) and sourcesh, which runs the user hook and waits. The user hook's install runsprepare: vp config, which truncates and rewriteshin place. When the outer dash resumes at the final line,exit $chas no terminating newline, so its lexer re-reads the open fd at the old EOF offset and gets bytes of the new, longer dispatcher, appending garbage toexit $c→ syntax error and a non-zero hook exit, sogit checkoutfails even though the install succeeded.Mechanism proof: a minimal standalone dash experiment (
outersourcesinner;innerends withexit 0without a newline and rewrites itself mid-run to a longer file) reproduces it in isolation: exit 2 withexit: Illegal number: 0./innerwithout the newline, exit 0 with the newline.Confidence: high — proven by code inspection, the full reproduction/control pair, and the isolated dash experiment.
Verification
Verdict: BUG (high confidence).
docs/guide/commit-hooks.mddocuments that the dispatcher is "recreated byvp configorvp hooks enable" and thatVP_GIT_HOOKS=0opts out of "hook installation from lifecycle scripts such asprepareorpostinstall" — reinstalling the dispatcher mid-hook is a documented, supported workflow that the design does not intend to be fragile.hwithout a trailing newline or in place; the omission dates to the feature's introduction in feat(hooks): add built-in pre-commit hook viavp config+vp staged#674.packages/cli/src/config/hooks.ts), not forwarded upstream behavior; Vite+ owns the fix.Fix
Changed files:
packages/cli/src/config/hooks.tspackages/cli/src/config/__tests__/hooks.spec.ts(regression tests)Both of the reporter's suggested defenses were implemented; either alone is sufficient and they complement each other:
hookScript()andHOOK_STUB_SCRIPTnow end with, so a shell that sourced the dispatcher finishes parsing the finalexit $cline from its buffer and never re-reads past the old EOF. This alone removes the reported failure even if another tool rewrites the dispatcher in place.install()now writeshand the hook stubs via a newwriteGeneratedHookFile()helper: write a fully formed temp file (<target>.<pid>.tmpinside<dir>/_/) andrenameSyncit over the target, so POSIX replaces the inode and a shell that already sourced the old dispatcher keeps reading the old, complete file. This also protects upgrades from stale dispatchers that still lack the trailing newline. If the rename cannot replace the target (e.g. a locked destination on Windows), it falls back to the previous in-place write, so behavior there is never worse than before. Mode0o755is re-applied withchmodSyncafter the write.findUnsafeHookInstallPath(), keepsnlink = 1, lives inside the gitignored_/directory, and is removed on rename failure.packages/cli/src/migrator/git-hooks.tsneeded no source change: it compares on-disk content against the same exported templates (now updated); stale dispatchers refresh on the nextvp config/vp hooks enable.Regression tests (all fail before the fix, pass after)
ends the generated dispatcher and hook stubs with a newline— afterinstall(),hand every stub end with(all platforms).replaces the dispatcher with a new file instead of rewriting it in place(POSIX) — a secondinstall()changes thehinode, proving the rename-based atomic write.keeps a running hook shell healthy when the dispatcher is rewritten mid-hook(POSIX) — full issue reproduction as a test: apost-checkoutuser hook rewrites the dispatcher in place to a longer file;git checkout -bmust exit 0 with noSyntax erroron stderr.Pre-fix failures were verified by stashing only
hooks.ts: all three fail (expected … to match / $/, inode unchanged, andSyntax error: Unterminated quoted stringwith checkout exit 1).Validation
vp:git checkout -b otherexits 0, no syntax error,hends with.vp config(atomic rename path) — checkout exits 0, because the running shell keeps reading the old inode.vp test packages/cli/src/config/__tests__/hooks.spec.ts packages/cli/src/config/__tests__/bin.spec.ts packages/cli/src/migration/__tests__/setup-plan-git-hooks.spec.ts— 142 passed, 1 skipped.pnpm test:unit— 129 files, 3763 passed, 1 skipped.vp check: formatting passes; lint/type error count equals the clean-tree baseline (105 pre-existing errors, all indocs/.vitepress/*, unrelated).just snapshot-test migration_add_git_hooks(passed; recorded.mdunchanged since the added trailing newline is invisible in the fenced transcript),just snapshot-test hooks(26 passed),just snapshot-test config(128 passed).Skipped checks
just check/just test/just lint: no Rust code changed; the Rust global CLI only dispatchesvp configto the JS package.pnpm testsnapshot suite (1449 cases): not run end-to-end for time; the config/hooks/migration-relevant filters above all pass.This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.
I've opened a pull request with this fix: #3570
fix(cli): parse the whole hook dispatcher before the hook runs
fix(cli): parse the whole hook dispatcher before the hook runs
fix(cli): keep git hooks running when vp config rewrites the dispatch…