Skip to content

build: dev-init.sh in a git worktree re-points the SHARED pre-commit hook at the worktree, silently disabling the hook on main when the worktree is removed #700

Closed

Description

@bpowers

Problem

scripts/dev-init.sh installs the project's pre-commit hook by symlinking the shared git hooks entry to the checkout's copy of scripts/pre-commit. When run inside a linked git worktree (e.g. the .claude/worktrees/agent-* worktrees Claude Code creates), it re-points the shared hook at the worktree's transient copy of the script. When that worktree is later removed, the symlink dangles and git silently skips the pre-commit hook for all subsequent commits in the main checkout -- with no warning. This defeats the project's "lean on the pre-commit hook" workflow.

This actually happened during an LTM review session: a commit on main bypassed fmt/clippy/tests because five agent worktrees had each run dev-init.sh.

Observed dangling state:

.git/hooks/pre-commit -> /home/bpowers/src/simlin2/.claude/worktrees/agent-ae83caaeb128b9a49/scripts/pre-commit

Root cause

In scripts/dev-init.sh:

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
...
# 1. Git hooks
if HOOK_PATH="$(git rev-parse --git-path hooks/pre-commit 2>/dev/null)"; then
    DESIRED="$REPO_ROOT/scripts/pre-commit"
    if ! { [ -L "$HOOK_PATH" ] && [ "$(readlink "$HOOK_PATH")" = "$DESIRED" ]; }; then
        mkdir -p "$(dirname "$HOOK_PATH")"
        rm -f "$HOOK_PATH"
        ln -s "$DESIRED" "$HOOK_PATH"
    fi
fi

The two sides of the symlink resolve to different repositories when run inside a linked worktree:

  • REPO_ROOT resolves to the worktree's checkout (the directory containing this copy of dev-init.sh), so DESIRED points at <worktree>/scripts/pre-commit.
  • git rev-parse --git-path hooks/pre-commit resolves to the shared common .git/hooks/pre-commit -- linked worktrees share the main repo's hooks directory (core.hooksPath resolves to the main .git/hooks).

So the script installs <main>/.git/hooks/pre-commit -> <worktree>/scripts/pre-commit. The worktree is transient; once git worktree remove deletes it, the symlink target no longer exists. git treats a non-executable / missing hook as "no hook" and commits silently succeed without running it.

Why it matters

  • Correctness / CI gate bypass: commits on main skip Rust fmt, clippy, the test suite, TS lint/typecheck, the WASM build, and the Python bindings tests -- exactly the gate CLAUDE.md instructs everyone to lean on. The failure is silent: no error, no warning.
  • Multi-agent amplification: the project's documented workflow spawns multiple worktrees that each run dev-init.sh at session start, so the shared hook is repeatedly clobbered and the dangling state is the steady state after worktrees are cleaned up.

Component(s) affected

  • scripts/dev-init.sh (hook installation, section "1. Git hooks")
  • Indirectly: the entire pre-commit gate (scripts/pre-commit) and any commit made in the main checkout after worktrees are torn down.

Possible approaches

  1. Skip hook installation in linked worktrees. Detect a linked worktree and do nothing: when git rev-parse --git-dir != git rev-parse --git-common-dir, the current checkout is a linked worktree -- leave the shared hook alone.
  2. Resolve DESIRED against the MAIN repository root. Point the shared symlink at the main checkout's scripts/pre-commit (e.g. the parent of git rev-parse --git-common-dir) rather than REPO_ROOT, so the target survives worktree removal.
  3. Install a relative symlink or a tiny wrapper that always resolves scripts/pre-commit against the checkout being committed (the worktree's own working tree), so the hook is correct no matter which checkout commits. This is the most robust: each commit runs the script from the tree it is committing.

Option 1 is the smallest safe fix; option 3 is the most correct. Worth a guard regardless: if the existing hook symlink is dangling, repair it.

How it was discovered

Identified during an LTM review session after a main commit was observed to bypass the pre-commit gate; traced to five agent worktrees having each run dev-init.sh and left the shared .git/hooks/pre-commit pointing at a since-removed worktree.

Activity

  1. added a commit that references this issue on Jun 4, 2026
    65af521
  2. bpowers commented on Jun 8, 2026

    @bpowers
    OwnerAuthor

    Fixed by 26a641e (PR #705): scripts/dev-init.sh now resolves the hook symlink target against the MAIN checkout (the parent of git rev-parse --git-common-dir) rather than the running worktree's REPO_ROOT, and only installs when that target exists -- so running dev-init in a transient linked worktree can no longer re-point the shared .git/hooks/pre-commit at a path that dangles after git worktree remove. The hook-installation block's comment cites this issue. Verified at HEAD: .git/hooks/pre-commit resolves to the main checkout's scripts/pre-commit and the symlink survives worktree removal by construction.

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions