Skip to content

🔄 Dependencies incorrectly reported as unused when run from package directory (regression in 5.7.0) #1642

@astegmaier

Description

@astegmaier

Prerequisites

Reproduction url

https://github.com/astegmaier/playground-knip-internal-dependencies-bug

Reproduction access

  • I've made sure the reproduction is publicly accessible

Good version

5.6.1

Bad version

5.7.0

Description of the regression

Starting in version 5.7.0, when a package imports a sibling workspace package, knip incorrectly reports that dependency as unused — but only when knip is invoked from within the package directory.

Running from the monorepo root, or using --workspace from the root, correctly recognizes the dependency as used.

This seems very simiilar to #902. But I'm hoping you might reconsider because:

  1. Unlike before, it's now clear that this is a regression (albeit a relatively old one)
  2. The performance docs say that "In case Knip is unbearably slow (or even crashes), you could resort to lint individual workspaces." I'm in exactly such a situation with an extremely large monorepo - knip 6.x takes 3-4 minutes to run in CI when run from the repo root (which is a blocker), but we can work around this by using lage with caching to only run knip on the packages that have changed in a given PR. But this bug is blocking that approach for the dependencies rule.
  3. I'm willing to work on a fix. AI suggested a simple fix that appears to work (see repro). But with your approval I'd give this some human attention and do extensive testing.

Example

For example, given this simplified repro structure:

package-a/
  index.js             # import { add } from '@repo/package-b'
  package.json         # dependencies: { "@repo/package-b": "workspace:*" }
package-b/
  index.js             # export function add(a, b) { return a + b; }
  package.json         # name: @repo/package-b
pnpm-workspace.yaml    # packages: [package-a, package-b]
package.json           # root, devDependencies: { knip }

You'll get these results...

# From root — CORRECT: no issues reported
pnpm knip --include dependencies

# From root with workspace filter — CORRECT: no issues reported
pnpm knip --include dependencies --workspace package-a

# From package directory — REGRESSION (starting in 5.7.0): @repo/package-b is reported unused
cd package-a
pnpm knip --include dependencies
# Unused dependencies (1)
# @repo/package-b  package.json:6:6

Metadata

Metadata

Assignees

No one assigned

    Labels

    regressionSomething is no longer working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions