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
6.3k stars 141 forks source link

regression after v2.30.1 with test files #286

Closed Zamiell closed 10 months ago

Zamiell commented 10 months ago

Hello,

I recently upgraded my version of Knip from version 2.30.1 to 2.32.4, which resulted in new lint errors from Knip, complaining that all of the test files in my project (of the form foo.test.ts) are "unused".

As a troubleshooting measure, I explicitly enabled the Jest plugin by adding "jest: true" in my "knip.json", but the errors persist.

Is there anything else I should try? Are these error expected? I expect this to be some form of regression in Knip.

For reference, here is the CI run where I updated my dependencies.

I can provide more information if you need.

webpro commented 10 months ago

Looks like that knip.json has "jest": "true" and ignores "jest.config.cjs".

Can you try a boolean for "jest": true and remove that ignored item? Otherwise Knip will not find that configuration file to find dependencies (which now includes the test file patterns).

Zamiell commented 10 months ago

Hi webpro, thanks for the quick reply.

I tried that, and it didn't seem to help.

For reference, I'm using a "knip.json" as follows:

{
  "$schema": "https://unpkg.com/knip/schema.json",
  "entry": ["src/index.ts", "tools/createRule.ts", "tools/generate.ts"],
  "jest": "true",
  "ignore": [
    ".eslintrc.cjs",
    "src/template.ts",
    "tests/fixtures/file.ts",
    "tests/template.ts"
  ],
  "ignoreDependencies": ["@typescript-eslint/rule-tester", "prettier"]
}

And the output of npx knip is as follows:

james@1qaz MINGW64 /d/Repositories/isaacscript/packages/eslint-plugin-isaacscript (main)
$ npx knip
Unused files (43)
jest.config.cjs
tests/completeSentences.test.ts
tests/format-list.test.ts
tests/format.test.ts
tests/format.ts
tests/rules/complete-sentences-jsdoc.test.ts
tests/rules/complete-sentences-line-comments.test.ts
tests/rules/consistent-enum-values.test.ts
tests/rules/enum-member-number-separation.test.ts
tests/rules/eqeqeq-fix.test.ts
tests/rules/format-jsdoc-comments.test.ts
tests/rules/format-line-comments.test.ts
tests/rules/jsdoc-code-block-language.test.ts
tests/rules/newline-between-switch-case.test.ts
tests/rules/no-confusing-set-methods.test.ts
tests/rules/no-empty-jsdoc.test.ts
tests/rules/no-empty-line-comments.test.ts
tests/rules/no-explicit-array-loops.test.ts
tests/rules/no-explicit-map-set-loops.test.ts
tests/rules/no-for-in.test.ts
tests/rules/no-invalid-default-map.test.ts
tests/rules/no-let-any.test.ts
tests/rules/no-number-enums.test.ts
tests/rules/no-object-any.test.ts
tests/rules/no-object-methods-with-map-set.test.ts
tests/rules/no-string-length-0.test.ts
tests/rules/no-template-curly-in-string-fix.test.ts
tests/rules/no-throw.test.ts
tests/rules/no-unsafe-plusplus.test.ts
tests/rules/no-void-return-type.test.ts
tests/rules/prefer-plusplus.test.ts
tests/rules/prefer-postfix-plusplus.test.ts
tests/rules/require-break.test.ts
tests/rules/require-capital-const-assertions.test.ts
tests/rules/require-capital-read-only.test.ts
tests/rules/require-unannotated-const-assertions.test.ts
tests/rules/require-v-registration.test.ts
tests/rules/require-variadic-function-argument.test.ts
tests/rules/strict-enums-assignment.test.ts
tests/rules/strict-enums-functions.test.ts
tests/rules/strict-enums-incrementing.test.ts
tests/rules/strict-enums.ts
tests/utils.ts
Unused exports (1)
getSentences  src/completeSentence.ts
Unused exported types (20)
Options                        type  src/rules/complete-sentences-jsdoc.ts
Options                        type  src/rules/complete-sentences-line-comments.ts
MessageIds                     type  src/rules/format-jsdoc-comments.ts
Options                        type  src/rules/format-jsdoc-comments.ts
MessageIds                     type  src/rules/format-line-comments.ts
Options                        type  src/rules/format-line-comments.ts
MessageIds                     type  src/rules/no-explicit-array-loops.ts
Options                        type  src/rules/no-explicit-array-loops.ts
NoInvalidDefaultMapMessageIds  type  src/rules/no-invalid-default-map.ts
Options                        type  src/rules/no-invalid-default-map.ts
MessageIds                     type  src/rules/prefer-plusplus.ts
Options                        type  src/rules/prefer-plusplus.ts
MessageIds                     type  src/rules/require-capital-const-assertions.ts
Options                        type  src/rules/require-capital-const-assertions.ts
MessageIds                     type  src/rules/require-capital-read-only.ts
Options                        type  src/rules/require-capital-read-only.ts
MessageIds                     type  src/rules/require-variadic-function-argument.ts
Options                        type  src/rules/require-variadic-function-argument.ts
MessageIds                     type  src/rules/strict-enums.ts
Options                        type  src/rules/strict-enums.ts
Configuration issues (1)
Unused item in ignoreDependencies: @typescript-eslint/rule-tester
webpro commented 10 months ago

The "jest": "true", is still a string, it should be a boolean: true (no quotes)

Zamiell commented 10 months ago

Sorry about that, I did not read your previous comment carefully enough.

I have changed it to a boolean now, but that seems to completely disable knip. In other words, knip successfully runs with no errors, but it doesn't detect any unused exports and so on.

webpro commented 10 months ago

Not sure what's going on, seems fine here:

✘ lars@Neo ../packages/eslint-plugin-isaacscript main ❯ npx knip@2.32.4
Need to install the following packages:
knip@2.32.4
Ok to proceed? (y) 
Unlisted dependencies (2)
ts-jest/dist/types   jest.config.cjs
ts-jest/jest-preset  jest.config.cjs
✘ lars@Neo ../packages/eslint-plugin-isaacscript main ❯ cat knip.json 
{
  "$schema": "https://unpkg.com/knip@2/schema.json",
  "entry": ["src/index.ts", "tools/createRule.ts", "tools/generate.ts"],
  "jest": true,
  "ignore": [
    ".eslintrc.cjs",
    "src/template.ts",
    "tests/fixtures/file.ts",
    "tests/template.ts"
  ],
  "ignoreDependencies": ["@typescript-eslint/rule-tester", "prettier"]
}
lars@Neo ../packages/eslint-plugin-isaacscript main ❯ 
Zamiell commented 10 months ago

After upgrading to v2.32.5 that you just released, everything seems to be working fine, thank you!

vieira commented 10 months ago

I am also experiencing this issue. I am using (yarn) workspaces; have no jest.config.js file. Jest is run from root with jest --silent. Tests are located in packages/<workspace>/test/<test>.test.js and the issue started on version 2.32.0 (maybe due to 82278f8f ?) (no problems in 2.31.0).

webpro commented 10 months ago

Sounds like there's no jest in devDependencies in the workspaces? In that case you'll need to enable Jest manually using jest: true (in root of config and/or per workspace). Also see https://github.com/webpro/knip#plugin-configuration. Apologies if this meant a breaking change for you.

vieira commented 9 months ago

Hello @webpro, thanks for your reply. I tried setting "jest": true in root and in the . workspace but nothing changed. I then tried to touch jest.config.js in the root directory of the project and that did work, however it seems a bit weird having an empty jest.config.js 😅

webpro commented 9 months ago

@vieira I've just done some housekeeping and bug fixing in plugins and configurations. I think your issue should be fixed in v2.33.4.

vieira commented 9 months ago

@webpro Having "jest": true in the root package.json file makes v2.33.3 work fine. I tried with v2.33.4 as suggested but got it by a barrage of false positives unrelated to tests: 1287 unused files, 163 unused dependencies, etc only from running npx knip@2.33.4 instead of npx knip@2.33.3. This is a in a mono repo that uses yarn workspaces, not sure if it's related.

As an example:

Unused dependencies (163)
(...)
@guuru/chat-events-web                services/chat/package.json
(...)

However if I do cd services/chat; git grep "@guuru/chat-events-web":

package.json:    "@guuru/chat-events-web": "1.0.0",
src/sagas/chatActions/postHeightUpdate.js:import { heightUpdatedEvent } from '@guuru/chat-events-web';
src/views/form/ExpanderButton/index.jsx:import { expandEvent, shrinkEvent } from '@guuru/chat-events-web';
src/views/form/QuestionForm/index.jsx:import { expandEvent } from '@guuru/chat-events-web';

Also tested with the latest version (2.38.1) but the same problem persists. I will just pin to 2.33.3 for now, if there is anything I can do to help let me know.

webpro commented 9 months ago

Any chance you could create some reproduction I can look at?