knip icon indicating copy to clipboard operation
knip copied to clipboard

🐛 TBD

Open webpro opened this issue 1 year ago • 2 comments

Sorry for not providing a reproduction, I tried one here that best resembles my project setup with minimum complexity.

Workspace b is using only a subset of workspace a. In the current state, packages/a/src/utils/unused.ts should be flagged as an unused file/all of its exports should be flagged as unused, and in packages/a/src/utils/used.ts, only the aFoo variable is being used, so the aBar export/variable should be flagged as unused.

The repo uses TS project references and workspaces (here npm, although mine is Yarn, but I don't believe it makes a huge difference), so the dependency between workspaces is seen both through the project references' setup (packages/b/tsconfig.json contains a project reference to packages/a/tsconfig.json) and through the regular dependencies (packages/b/package.json has a "@internal/a": "*" dev dependency). You can make sure that everything works properly by running npm run b (I've made this script readily available in the root), it should simply display aFoo.

Both workspaces define a "exports": { "./*": "./src/*.ts" } subpath export, which basically makes every file available from the outside. So technically, I guess this warrants an entry: ['./src/**'] config in Knip as every file could be accessed, so that's what I've done here. But despite that, we still want to know whether some of those files (and the exported variables inside) are unused, hence adding includeEntryExports: true to Knip's config.

Despite all this, if you run npm run knip (which I've setup to just target the packages/a workspace), the current result is the following, which isn't what I expected:


Unused exports (3)

unusedVar  unknown  packages/a/src/utils/unused.ts:1:14

aFoo       unknown  packages/a/src/utils/used.ts:1:14  

aBar       unknown  packages/a/src/utils/used.ts:2:14

Do you reckon my usage makes sense for Knip to analyze? Am I misconfiguring or misunderstanding something? Can you confirm the current configuration is correct (or not) and would work if this current issue were to be tackled?

Originally posted by @acidoxee in #853

webpro avatar Feb 21 '25 08:02 webpro

There was an error when running Knip because of "references": ["../a"] (no "path" prop):

❯ npx knip
Analyzing workspace packages/b...
TypeError: Cannot read properties of undefined (reading 'endsWith')
    at eval (file:///home/projects/stackblitz-starters-tml4esvn/node_modules/knip/dist/plugins/typescript/index.js:30:46)
    at _0x5f388d._evaluate (https://stackblitzstarterstml4esvn-3ik3.w-credentialless-staticblitz.com/blitz.7f809d15.js:40:796928)

Node.js v18.20.3

And after I fixed that, the result was:

❯ npx knip
Unused exports (2)
unusedVar  unknown  packages/a/src/utils/unused.ts:1:14
aBar       unknown  packages/a/src/utils/used.ts:2:14

webpro avatar Feb 21 '25 08:02 webpro

@acidoxee not sure what is actionable here, can you please elaborate?

webpro avatar Mar 08 '25 09:03 webpro

Closing due to inactivity, not sure what to could be done here.

webpro avatar Mar 25 '25 07:03 webpro