Skip to content

🐛 In a git worktree outside the repo's tree, Lefthook hooks + ** entry patterns make Knip crawl the whole filesystem #2168

Closed

Description

@npateras

Prerequisites

  • I'm using the latest version (6.42.0)
  • I've read the relevant documentation
  • I've searched for existing issues (closest: 🐛 Knip fails in a git worktree + lefthook #990, whose fix is what yields the path below)
  • I've checked the list of known issues
  • I've read the issue reproduction guide

Reproduction url

Self-contained script below. It needs a real git worktree that lives outside the repo's directory tree, which a sandbox can't host.

Reproduction access

  • The script is public (it is in this issue)

Description of the issue

In a git worktree whose only common ancestor with the main repo is / (repo under $HOME, worktree under /tmp), knip never finishes: it walks the whole disk. It needs the Lefthook plugin with an installed hook plus a plugin that adds ** entry patterns (Next.js here). The same project finishes in seconds in the main checkout.

#!/bin/sh
# The worktree must not share a parent directory with the repo below `/`:
# repo under $HOME, worktree under /tmp.
set -e
REPO="$HOME/knip-worktree-repro"
WT="/tmp/knip-worktree-repro-wt"

mkdir "$REPO" && cd "$REPO"
git init -q
cat > package.json <<'JSON'
{
  "name": "knip-worktree-repro",
  "private": true,
  "devDependencies": {
    "knip": "6.42.0",
    "lefthook": "2.2.2",
    "next": "16.3.8",
    "react": "19.3.1",
    "react-dom": "19.3.1"
  }
}
JSON
printf 'node_modules\n' > .gitignore
printf 'pre-commit:\n  jobs:\n    - run: echo ok\n' > lefthook.yml
npm install --ignore-scripts --silent
npx lefthook install              # writes .git/hooks/pre-commit
git add -A && git commit -qm init --no-verify

echo "main checkout:"; time npx knip > /dev/null || true

git worktree add -q --detach "$WT"
cd "$WT" && npm install --ignore-scripts --silent
echo "worktree:"; time timeout 120 npx knip > /dev/null || echo "exit $?"

On macOS (Node 24, npm 12):

main checkout:  real 0m5.7s
worktree:       real 2m0.1s   (killed by the 120 s timeout, exit 124; sys 41.8 s)

While it hangs, lsof on the process shows / and paths such as /Applications/Microsoft Excel.app/... open.

Cause, as far as I can trace it: in the worktree, getGitHookPaths() follows git rev-parse --git-path hooks, which returns the main repo's absolute hooks directory. The installed .git/hooks/pre-commit then becomes a skip-exports entry pattern relative to the worktree, ../../../../../../../Users/<me>/knip-worktree-repro/.git/hooks/pre-commit. The "entry paths from plugins (ignore exports)" glob gets that pattern together with the Next.js patterns such as {,src/}app/**/{layout,page,route,template}.{js,jsx,ts,tsx}, so the glob's root becomes / and the ** patterns make it crawl everything. Without a ** pattern in that glob (Lefthook alone, or Lefthook + Vitest, whose ** patterns land in a different glob) it finishes in about a second, and with no installed hook the hook patterns match nothing.

Expected: the run takes about as long as in the main checkout. Perhaps hook files outside the cwd could be resolved separately (they are fixed paths, so existsSync would do) instead of joining the cwd-relative glob.

Workaround: lefthook: false in a config used only in such worktrees.

Activity

  1. github-actions commented on Oct 20, 2026

    @github-actions

    Preview release available:

    npm i -D https://pkg.pr.new/knip@46a825d
  2. webpro commented on Oct 20, 2026

    @webpro
    Member

    Thanks for the report, Νικόλας! Should be good in the next release, but feel free to try out the preview release (in previous comment).

  3. added a commit that references this issue on Oct 20, 2026
    46a825d
  4. webpro commented on Oct 24, 2026

    @webpro
    Member

    🚀 This issue has been resolved in v6.43.0. See Release 6.43.0 for release notes.

    Using Knip in a commercial project? Please consider becoming a sponsor.

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