Skip to content

Conversation

@parksb
Copy link

@parksb parksb commented Nov 19, 2025

This PR continues the work from #924. Thanks @jwoo0122 :)

When a project uses Yarn PnP, this PR makes knip look for manifest files in the PnP file instead of searching in the node_modules directory. Previously, knip incorrectly reported peer dependencies required by packages as unused dependencies in Yarn PnP projects.

Below is an example where knip reports react and react-dom as unused dependencies in a project using styled-components. (styled-components requires react and react-dom as peer dependencies.)

before
SCR-20251119-jwfr

With Yarn PnP support in this PR, knip correctly reports no unused dependencies as shown below.

after
SCR-20251119-jway

@parksb
Copy link
Author

parksb commented Nov 19, 2025

For test consistency, I've added the Yarn PnP cache files. This is equivalent to committing the node_modules directory to git. Without adding this, you would need to run yarn install once in the fixtures/yarn-pnp directory before running the tests.


if (pnpApi != null) {
const packageJsonPath = join(packageName, 'package.json');
const resolvedPath = pnpApi.resolveToUnqualified(packageJsonPath, dir);
Copy link
Author

@parksb parksb Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used resolveToUnqualified to bypass exports map restrictions. This is necessary for reading metadata (bin, peerDependencies, types, ...) even when:

  1. Workspace packages don't have ./package.json entry in exports map
  2. Packages have no JavaScript entry point (e.g., @types/* packages)

Actual resolution (oxc-resolver) still respects exports map, so invalid imports will be correctly detected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant