Skip to content

fix(prepare): install husky hooks in git worktrees - #151

Open
tomcasaburi wants to merge 1 commit into
masterfrom
fix/husky-prepare-worktree
Open

tomcasaburi wants to merge 1 commit into
masterfrom
fix/husky-prepare-worktree

Conversation

@tomcasaburi

Copy link
Copy Markdown
Member

Problem

prepare was test -d .git && husky install || true. In a linked git worktree .git is a file, so husky was never installed there and .husky/_/husky.sh was missing. core.hooksPath=.husky is shared repo config, so .husky/commit-msg still ran, and every commit in the worktree failed:

.husky/commit-msg: line 2: .husky/_/husky.sh: No such file or directory

Reproduced with a fresh git worktree add from origin/master, followed by npm ci and git commit.

Fix

Change the guard to test -e .git. This matches husky 8's own check (fs.existsSync('.git')). A file .git also covers submodules.

Installs without a .git are unchanged:

  • Docker excludes .git via .dockerignore and installs with --ignore-scripts.
  • Registry and tarball installs have no .git.

Test

test/cli/prepare-husky.test.ts reads the real prepare script from package.json and runs it in a temporary repo, both in the normal checkout and in a linked worktree. It asserts that .husky/_/husky.sh exists afterwards. The worktree case fails on the old script and passes with the fix. The test is skipped on Windows, where npm runs scripts through cmd.exe.

  • npm run build && npm run build:test pass
  • npm run test:cli: 45 files, 344 tests passed, 1 skipped
  • The commit in this PR was made from a worktree with the commit-msg hook running.

Closes #150

The prepare guard `test -d .git` is false in a linked worktree, where .git
is a file, so husky install was skipped and .husky/_/husky.sh never
written. core.hooksPath is shared repo config, so the commit-msg hook still
ran and every commit in the worktree failed sourcing the missing file.
Use `test -e .git`, matching husky's own existsSync('.git') check.
@coderabbitai

coderabbitai Bot commented Oct 2, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 54 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: cddfa37a-8e81-4512-8dd7-dd21599c2eaf

📥 Commits

Reviewing files that changed from the base of the PR and between ccf3aa7 and 31d33af.

📒 Files selected for processing (2)
  • package.json
  • test/cli/prepare-husky.test.ts
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


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

Comment @coderabbitai help to get the list of available commands.

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.

prepare skips husky install in git worktrees, so every commit fails

2 participants