Skip to content

False positive unused files in git worktree (entry points not resolved) #1655

@ErisDS

Description

@ErisDS

Prerequisites

Good version

N/A (workaround was --no-gitignore)

Bad version

6.1.1 (also 5.86.0)

Description

In a git worktree, knip reports package entry points as "unused files" that are correctly recognized in the main repo checkout. The .gitignore worktree fix in #1424 (v5.79.0) resolved gitignore traversal, but entry point resolution from package.json main/exports fields still fails in worktrees.

Environment

  • Git worktree (.git is a file, not a directory)
  • pnpm monorepo with multiple workspaces
  • knip 6.1.1

What happens

Running knip in a git worktree produces 18 "unused files" including:

  • apps/api/src/index.ts (the API entry point)
  • packages/td/src/bin.ts (a CLI bin entry)
  • shared/db/src/migrate.ts and shared/db/src/seed.ts (script entry points)
  • All apps/api/src/routes/*/index.ts barrel files (imported by the entry point)

Running knip in the main repo checkout (same commit, same config) correctly recognizes all of these and reports zero unused files.

Workaround

Previously we used --no-gitignore which happened to mask this issue (though it introduced other problems like scanning dist/ and coverage/ directories). There is no clean workaround that does not hide real findings.

Likely cause

The entry point resolution probably uses a path or fs.stat check on .git that assumes it is a directory. In worktrees, .git is a file containing gitdir: /path/to/main/.git/worktrees/<name>. This may affect how knip determines the repository root and resolves workspace package.json paths relative to it.

Related

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