Skip to content

Git hook dispatcher fails with a shell syntax error when vp config rewrites .vite-hooks/_/h during a hook #3567

Description

@salmundani

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 string

Appending 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):

  1. End the generated h (and the per-hook stubs) with \n.
  2. Write h atomically: write to a temp file in _/ and renameSync it over h, 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.0

Used Package Manager

pnpm

Logs

.vite-hooks/_/post-checkout: 33: .vite-hooks/_/h: Syntax error: "(" unexpected

Activity

  1. added theissue type on Nov 24, 2026
  2. voidzero-bot commented on Nov 24, 2026

    @voidzero-bot
    Contributor
    • Reproduced: Yes
    • Exploration: Yes — View branch
    • Unit Test: Yes — packages/cli/src/config/__tests__/hooks.spec.ts
    • Priority: Priority P2: Has Workaround. The failure only hits the documented prepare: vp config + install-in-hook workflow (e.g. pnpm install from post-checkout/post-merge) on shells like dash, and the install itself still succeeds — only the hook's exit status and git checkout's exit code are wrong. A straightforward workaround exists (VP_GIT_HOOKS=0 pnpm install inside the hook, or appending a trailing newline to .vite-hooks/_/h).

    The generated .vite-hooks/_/h dispatcher ends with exit $c without a trailing newline, and install() in packages/cli/src/config/hooks.ts rewrites it in place with writeFileSync (same inode) on every vp config run. When a hook triggers an install whose prepare runs vp config, dash — which sourced h and 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 to exit $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() and HOOK_STUB_SCRIPT end with (so the shell never re-reads past the old EOF) and replaces h and 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 config rewrites .vite-hooks/_/h during a hook

    • Reporter: salmundani

    • Report: When a project hook (e.g. post-checkout running pnpm install with prepare: vp config) causes vp config to 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:

      .vite-hooks/_/post-checkout: 33: .vite-hooks/_/h: Syntax error: "(" unexpected
      

      or Unterminated quoted string, and git checkout exits non-zero.

    Expected: the hook and git checkout complete successfully (exit 0).

    Actual: the hook's shell (dash) reports a syntax error on .vite-hooks/_/h and git checkout exits 1.

    Environment

    • Vite+: vp v1.8.0 (local vite-plus v1.8.0, built from the current checkout; no global vp)
    • Node.js: v24.22.3
    • pnpm: v12.11.2
    • OS: Ubuntu Linux x64 runner, /bin/sh → dash (reporter: dash 0.5.12)

    Reproduction

    /tmp/vite-plus-triage/gh-3567/repro — inline script from the issue, run verbatim.

    1. git init -q, package.json, vp config --no-agent (exit 0).

    2. Inspection: .vite-hooks/_/h (1057 bytes) ends with exit $c without a trailing newline; the stubs also lack one.

    3. Added the reported post-checkout hook (stand-in for pnpm install + prepare: vp config writing a longer dispatcher), committed, then git checkout -b other:

      Switched to a new branch 'other'
      .vite-hooks/_/post-checkout: 35: .vite-hooks/_/h: Syntax error: Unterminated quoted string
      

      Exit code: 1 — matches the report (the exact message depends on the bytes read past the old EOF).

    4. 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 config path):

    1. hookScript(dir) — the dispatcher template ends with exit $c` with no trailing newline.
    2. HOOK_STUB_SCRIPT — #!/usr/bin/env sh . "$(dirname "$0")/h" also has no trailing newline.
    3. install() — rewrites the dispatcher in place on every run with writeFileSync (same inode, no atomic replace).
    4. Call chain: vp config (packages/cli/src/config/bin.ts) runs on every prepare/install, so a hook-triggered install rewrites h while the running hook shell still has the old h open.

    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-checkout is executed by git with /bin/sh (dash) and sources h, which runs the user hook and waits. The user hook's install runs prepare: vp config, which truncates and rewrites h in place. When the outer dash resumes at the final line, exit $c has 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 to exit $c → syntax error and a non-zero hook exit, so git checkout fails even though the install succeeded.

    Mechanism proof: a minimal standalone dash experiment (outer sources inner; inner ends with exit 0 without a newline and rewrites itself mid-run to a longer file) reproduces it in isolation: exit 2 with exit: Illegal number: 0./inner without 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.md documents that the dispatcher is "recreated by vp config or vp hooks enable" and that VP_GIT_HOOKS=0 opts out of "hook installation from lifecycle scripts such as prepare or postinstall" — reinstalling the dispatcher mid-hook is a documented, supported workflow that the design does not intend to be fragile.
    • No commit, comment, changelog, test, or prior issue/PR shows any intent to write h without a trailing newline or in place; the omission dates to the feature's introduction in feat(hooks): add built-in pre-commit hook via vp config + vp staged #674.
    • The dispatcher template and install logic are Vite+ code (packages/cli/src/config/hooks.ts), not forwarded upstream behavior; Vite+ owns the fix.

    Fix

    Changed files:

    1. packages/cli/src/config/hooks.ts
    2. packages/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:

    • Trailing newline: hookScript() and HOOK_STUB_SCRIPT now end with , so a shell that sourced the dispatcher finishes parsing the final exit $c line 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.
    • Atomic replace: install() now writes h and the hook stubs via a new writeGeneratedHookFile() helper: write a fully formed temp file (<target>.<pid>.tmp inside <dir>/_/) and renameSync it 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. Mode 0o755 is re-applied with chmodSync after the write.
    • The temp name is not inspected by findUnsafeHookInstallPath(), keeps nlink = 1, lives inside the gitignored _/ directory, and is removed on rename failure.
    • packages/cli/src/migrator/git-hooks.ts needed no source change: it compares on-disk content against the same exported templates (now updated); stale dispatchers refresh on the next vp config / vp hooks enable.

    Regression tests (all fail before the fix, pass after)

    1. ends the generated dispatcher and hook stubs with a newline — after install(), h and every stub end with (all platforms).
    2. replaces the dispatcher with a new file instead of rewriting it in place (POSIX) — a second install() changes the h inode, proving the rename-based atomic write.
    3. keeps a running hook shell healthy when the dispatcher is rewritten mid-hook (POSIX) — full issue reproduction as a test: a post-checkout user hook rewrites the dispatcher in place to a longer file; git checkout -b must exit 0 with no Syntax error on stderr.

    Pre-fix failures were verified by stashing only hooks.ts: all three fail (expected … to match / $/, inode unchanged, and Syntax error: Unterminated quoted string with checkout exit 1).

    Validation

    • Original issue reproduction re-run end-to-end with the rebuilt vp: git checkout -b other exits 0, no syntax error, h ends with .
    • Realistic upgrade scenario: a stale dispatcher without a trailing newline replaced mid-hook by the fixed vp config (atomic rename path) — checkout exits 0, because the running shell keeps reading the old inode.
    • Focused tests: 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.
    • Full unit suite: 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 in docs/.vitepress/*, unrelated).
    • Snapshot fixtures: just snapshot-test migration_add_git_hooks (passed; recorded .md unchanged 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

    • Rust just check / just test / just lint: no Rust code changed; the Rust global CLI only dispatches vp config to the JS package.
    • Full pnpm test snapshot 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

  3. added
    - P2: has workaround
    on Nov 24, 2026
  4. added a commit that references this issue on Nov 25, 2026
    ce91aa6
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

    - P2: has workaroundtriage: fix verified

    Type

    Fields

    Priority

    None yet

    Effort

    None yet

    Target date

    None yet

    Start date

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions