webpro-nl / knip

✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects. Knip it before you ship it!
https://knip.dev
ISC License
7.06k stars 177 forks source link

🐛 False positive for `unlisted` when extending a tsconfig file from a package #761

Closed me4502 closed 1 month ago

me4502 commented 3 months ago

Prerequisites

Reproduction url

https://github.com/me4502/knip-reproduction

Description of the issue

When you include entries exported via package.json exports in a tsconfig file's types field, Knip looks for the full path rather than the package that it's included in for the unlisted check. For example in the reproduction codesandbox, it looks for @monorepo/helper-types/base and @monorepo/helper-types/other, rather than just @monorepo/helper-types which is already included in the dependencies field.

When you extend a shareable tsconfig file coming from a package, Knip will expect any packages mentioned in the "types" field of the shareable config to also be present in all configurations that extend it. This is unnecessary as those are dependencies of the package that serves the shareable tsconfig file.

webpro commented 3 months ago

The repro is inaccessible to me (404)

me4502 commented 3 months ago

ah sorry, i seem to have a lot of issues with that codesandbox site disconnecting for me. i'll create a GitHub reproduction case tomorrow.

me4502 commented 3 months ago

I've just switched it out with a GitHub repo, and after some investigation i've also slightly altered the issue to better reflect what I believe is the underlying cause.

webpro commented 3 months ago

Thanks for the reproduction.

The problem here is that Knip uses ts.readConfigFile which returns the final TS configuration (per workspace). There's no way to stop when descending into other local workspaces through extends (while we do want to recursively load TS config files local to the workspace).

A solution I can think is a custom TS config reader for the TypeScript plugin that does stop when traversing into other workspaces.

webpro commented 1 month ago

Working on some refactorings and things are coming together nicely, including recursively handling config files in their own workspace. Which is what should handle this case a lot better. It seems to handle the example repo well. This will be included in the next version. You could try an early build if you like:

npm i -D https://pkg.pr.new/knip@c03f963
me4502 commented 1 month ago

Thanks, I can confirm that fixes the problem :)

webpro commented 1 month ago

Going to close with the release of v5.34.0