Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to report unused exports in entry files in folders that are not being 'used' or 'imported' throughout the project? #142

Closed
VladSez opened this issue Jun 13, 2023 · 9 comments
Labels
discussion Discussion

Comments

@VladSez
Copy link

VladSez commented Jun 13, 2023

Hello, thanks for a wonderful library ❤️

I have a question regarding unused exports
I have a project (not a monorepo), which looks like this (simplified):

Reproduction

/src
- /module1
- - index.ts
- /module2
- - index.ts
- /module3
- - index.ts
file: src/module1/index.ts
========
// this is NOT reported by knip, because we don't import anything throughout the app from 'module1/index.ts'
export function sum(a: number, b: number) {
  return a + b;
}

// this is NOT reported by knip, because we don't import anything throughout the app from 'module1/index.ts'
export function multiply(a: number, b: number) {
  return a * b;
}

file: src/module2/index.ts
========
// this function is imported in module3/index.ts
export function subtract(a: number, b: number) {
  return a - b;
}

// this is reported by knip correctly, because we import 'subtract()' at 'module3/index.ts
export const test = 23;

file: src/module3/index.ts
=======

import { subtract } from "../module2/index.js";

// this is NOT reported by knip, because we don't import anything throughout the app from 'module3/index.ts'
export function calculate(a: number, b: number) {
  return subtract(a, b);
}

// this is NOT reported by knip, because we don't import anything throughout the app from 'module3/index.ts'
export const smth = 'smth;

knip.json

{
  "project": ["src/**/*.ts"]
}

package.json

{
  "name": "knip-test",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "module1": "tsx src/module1/index.ts",
    "module2": "tsx src/module2/index.ts",
    "module3": "tsx src/module3/index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "knip": "2.13.0",
    "tsx": "3.12.7",
    "typescript": "5.1.3"
  }
}

if I run npx knip I get:

Unused exports (1)
test  src/module2/index.ts

Is it possible to make knip detect unused exports for module1 and module3?

I have found this in docs: https://github.com/webpro/knip/blob/main/docs/handling-issues.md#unused-exports
But still not quite clear for me, if I can do anything to report those unused exports..

Also I have found that --include-entry-exports flag have been removed in v2, maybe this is related to this problem?

@VladSez VladSez added the discussion Discussion label Jun 13, 2023
@webpro
Copy link
Owner

webpro commented Jun 14, 2023

Thanks for your detailed reporting. I'm working on #140 which is related, and I'm planning to have a fix in the coming days.

@webpro
Copy link
Owner

webpro commented Jun 15, 2023

I've re-introduced the --include-entry-exports flag in v2.14.0-next.0.

Any chance you could try it out on your project(s)?

You can install with e.g. npm install -D knip@next

@VladSez
Copy link
Author

VladSez commented Jun 15, 2023

@webpro thanks for the quick fix
I have tried on the above example, it works as expected now

But I have tried on another project

and I am getting prettier lint-staged and vite config files as reported
on knip: 2.13.0 I wasn't getting this

knip: 2.14.0-next.0

Unused exports (5)
default  .prettierrc.cjs       
default  knip.ts               
default  lint-staged.config.cjs
LABELS   src/cli/index.ts  ---> this is reported correctly
default  vite.config.ts

Is it expected?

@webpro
Copy link
Owner

webpro commented Jun 15, 2023

Thanks for trying so quickly.

Yeah I was kinda afraid of such results, that's why I did a pre-release first.

It is expected, but not desired. I think I need to make a difference between regular source code entry files versus entry files added by plugins. WIP :)

@webpro
Copy link
Owner

webpro commented Jun 15, 2023

I've released another next pre-release.

I should add that for files referenced in scripts like this:

"module1": "tsx src/module1/index.ts"

The unused exports won't be reported. Maybe these should be added to entry explicitly? E.g.:

{
  "entry": ["src/*/index.ts"],
  "project": ["src/**/*.ts"]
}

@VladSez
Copy link
Author

VladSez commented Jun 15, 2023

@webpro thank you, I have tried the latest version and it works as you mentioned, but in my opinion this behaviour is a bit confusing

in package.json I have

{
  "scripts": {
    "start:cli": "tsx src/cli/index.ts",
    "start:server": "tsx src/server/index.ts"
  }
}

and in knip:

{
  "entry": ["src/*/index.ts"],
  "project": ["src/**/*.ts"]
}

and with this setup src/cli/index.ts and src/server/index.ts are not reported, if I remove these scripts from package.json everything is reported correctly.

But, I think, it's useful to keep thoose scripts in package.json, unfortunately this breaks knip --include-entry-exports flag reporting

Is there any solution here?

@webpro webpro closed this as completed in 1e3d3b6 Jun 17, 2023
@webpro
Copy link
Owner

webpro commented Jun 17, 2023

🚀 This issue has been resolved in v2.14.0. See Release 2.14.0 for release notes.

@webpro
Copy link
Owner

webpro commented Jun 17, 2023

Gave it a little more thought and I think it makes sense to include script files when using the --include-entry-exports flag.

By the way, I didn't mean to say you should include scripts in both entry config and npm scripts. Only in one place should be enough indeed.

@VladSez
Copy link
Author

VladSez commented Jun 18, 2023

@webpro thanks for your effort, everything works now 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Discussion
Projects
None yet
Development

No branches or pull requests

2 participants