Skip to content

fix(hooks): decline when a worktree has no dependencies - #1095

Merged
cameri merged 2 commits into
rewrite/mainfrom
fix/husky-hooks-in-worktrees
Nov 16, 2026
Merged

cameri merged 2 commits into
rewrite/mainfrom
fix/husky-hooks-in-worktrees

Conversation

@chappie-daemon

Copy link
Copy Markdown
Collaborator

What was wrong

A factory worktree cannot commit. git worktree add does not run prepare, so the new checkout has neither .husky/_ nor node_modules, and the first line of both hooks aborts:

$ git commit -q --allow-empty -m probe
.husky/pre-commit: 2: .: cannot open .husky/_/husky.sh: No such file
rc=1

sh exits a non-interactive script when a . fails, so no gate ever runs and the commit never happens. The workaround every agent finds is --no-verify, which skips the gates the hooks exist to enforce.

Six tool-call rows across 2026-11-06..09 were this failure, from both the primary and job subagents, all inside projects/nostream-worktrees/*.

What this changes

Both hooks now decline when the checkout has nothing to run, and source the shim only when it exists:

[ -d node_modules ] || exit 0
_root=$(dirname -- "$0")
[ -f "$_root/_/husky.sh" ] && . "$_root/_/husky.sh"

A checkout that can run its gates is unchanged: pnpm run lint, build:check, test:unit and commitlint still run, and still block on failure.

Verified in a fresh worktree off this base

state before after
no node_modules, no .husky/_ rc=1, no commit rc=0, commit lands
node_modules + .husky/_ present shim runs shim runs (HOOK-RAN), pnpm run lint executes

Out of scope

The deeper cause is that a worktree is expected to install before committing. Making prepare run per worktree would put a full dependency install on every factory job, so declining is the cheaper and more honest behaviour for a checkout with no dependencies: there is nothing for the gates to check against.

`git worktree add` never runs `prepare`, so a factory worktree arrives with
neither `.husky/_` nor `node_modules`; the `. _/husky.sh` source then aborted
every commit with rc 1 before any gate ran. Six tool-call rows across four days
were that failure, each paid as a discovery of `--no-verify`.

Both hooks now decline when there are no dependencies to lint or test, and
source the shim only when it exists. A checkout that can run its gates runs
them unchanged - verified in a fresh worktree: commit rc 0 with no deps, and
the shim plus `pnpm run lint` still ran once they were present.
@changeset-bot

changeset-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 118bb2c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 87714b38-d1e6-4004-a5df-ec3453c979df

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@greptile-apps

greptile-apps Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Both hooks skip checks when node_modules is absent and load the Husky shim only when it exists.

  • Git hooks let worktrees without dependencies finish commits.
  • The pre-commit hook stops at the first failed check.

Reviews (2) · Last reviewed commit: "fix(hooks): fail the pre-commit hook on ..." · Reviewed by Greptile

Comment thread .husky/pre-commit Resolved
`pre-commit` runs three commands, so without `set -e` the hook's exit status is
`test:unit`'s alone and a failing lint is swallowed by a passing test run. The
guard goes after the shim line rather than before it: `[ -f … ] && . …` returns
1 when the shim is absent, and under `set -e` that aborts the commit — the
worktree case this file exists to fix.

Measured with a stub `pnpm` whose `lint` fails: the base hook and this branch's
previous head both exit 0; with the guard the hook exits 1, and a checkout with
no `node_modules` still exits 0.

Reported by Greptile on #1095.
@cameri
cameri merged commit 67ff5d1 into rewrite/main Nov 16, 2026
17 checks passed
@cameri
cameri deleted the fix/husky-hooks-in-worktrees branch November 16, 2026 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants