Skip to content

[rush] Git hooks installed into the per-worktree folder never run in linked worktrees #6246

Backlog

Description

Summary

When rush install or rush update runs inside a linked Git worktree, Rush copies common/git-hooks/* into the worktree's private Git dir (<common-git-dir>/worktrees/<name>/hooks). Git never reads hooks from that folder. It reads them only from the common Git dir (<common-git-dir>/hooks), or from core.hooksPath when that is set. So in a linked worktree the hooks Rush installs never run, and commits skip them without any warning.

The bug stays hidden in a normal clone: as long as someone runs rush install in the main checkout, .git/hooks gets populated and every worktree shares it. It shows up when every install runs in a linked worktree, for example in a bare clone with all branches checked out as worktrees, or in any setup where the main checkout is never installed.

Repro steps

git clone --bare <rush-repo-url> repo/bare
cd repo
git -C bare worktree add ../main main
cd main/<rush.json folder>
rush install

git rev-parse --git-path hooks     # prints .../repo/bare/hooks
ls ../bare/hooks                   # (path relative to the worktree) only *.sample files
ls ../bare/worktrees/main/hooks    # the hooks Rush installed
git hook run pre-commit            # error: cannot find a hook named pre-commit

Expected result: the hooks from common/git-hooks run on commit, so Rush writes them to the folder git rev-parse --git-path hooks returns.

Actual result: Rush writes them to <common-git-dir>/worktrees/<name>/hooks. Git never runs them, and git commit succeeds without running the pre-commit hook.

Details

Git.getHooksFolder() (libraries/rush-lib/src/logic/Git.ts, same on main today) uses the per-worktree Git dir:

public getHooksFolder(): string | undefined {
  const repoInfo: gitInfo.GitRepoInfo | undefined = this.getGitInfo();
  if (repoInfo && repoInfo.worktreeGitDir) {
    return path.join(repoInfo.worktreeGitDir, 'hooks');
  }
  return undefined;
}

The method right after it, getIsHooksPathDefaultAsync(), already treats <commonGitDir>/hooks as the default hooks path and compares it against git rev-parse --git-path hooks. Inside a linked worktree, Rush therefore validates one folder and installs into another.

The per-worktree folder looks like it dates from #990, which fixed a crash in worktrees by switching to git-repo-info's worktreeGitDir. Git has never read hooks from a per-worktree folder, though; per-worktree hooks are only possible through core.hooksPath with extensions.worktreeConfig.

Suggested fix: have getHooksFolder() return the common Git dir's hooks folder (repoInfo.commonGitDir), or the output of git rev-parse --git-path hooks.

One follow-up for the fix: the wrapper Rush installs calls the implementation script by a path relative to the hooks folder, pointing at the worktree that ran rush install:

SCRIPT_IMPLEMENTATION_PATH="$SCRIPT_DIR/../../../../<worktree>/<rush-folder>/common/git-hooks/pre-commit"

Once the wrapper lives in the shared hooks folder, every worktree runs it, but it would still call the script from whichever worktree installed it last. If that worktree is removed, the wrapper prints "The pre-commit Git hook no longer exists in your version of the repo…" and exits 0, so the commit goes through and the hook is skipped silently. Resolving the script from the committing worktree (git rev-parse --show-toplevel) avoids both problems.

Related: #5633 (broader worktree support).

Standard questions

Question Answer
@microsoft/rush globally installed version? 5.179.0 (via install-run-rush)
rushVersion from rush.json? 5.179.0 (code is unchanged on main; latest published is 5.182.1)
pnpmVersion, npmVersion, or yarnVersion from rush.json? pnpm 10.34.9
(if pnpm) useWorkspaces from pnpm-config.json? true
Operating system? Windows 11 (Git for Windows 2.55.0); the logic is platform-independent
Would you consider contributing a PR? Undecided
Node.js version (node -v)? 24.22.3

Activity

  1. Ultranmus commented on Dec 3, 2026

    @Ultranmus

    Opened #6248 for this. It installs the hooks in the common Git dir and has the wrapper find the scripts from git rev-parse --show-toplevel, as you suggested. Thanks for the detailed write-up.

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

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Needs triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions