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

🐛 Cascaded ESLint configuration flags eslintrc files as unused #957

Closed
6 tasks done
pjonsson opened this issue Feb 24, 2025 · 6 comments
Closed
6 tasks done

🐛 Cascaded ESLint configuration flags eslintrc files as unused #957

pjonsson opened this issue Feb 24, 2025 · 6 comments
Labels
bug Something isn't working

Comments

@pjonsson
Copy link

Prerequisites

Reproduction url

https://knip.dev/reference/plugins/eslint#default-configuration

Reproduction access

  • I've made sure the reproduction is publicly accessible

Description of the issue

The ESLint plugin default configuration at https://knip.dev/reference/plugins/eslint#default-configuration lists the config files as ".eslintrc", ".eslintrc.{js,json,cjs}", "eslintrc.{yml,yaml}", "package.json", but the old non-flat ESLint config format can have .eslintrc files appear in subdirectories (https://eslint.org/docs/v8.x/use/configure/configuration-files#cascading-and-hierarchy), so with this (minimized) knip.jsonc:

{
  "$schema": "https://unpkg.com/knip@5/schema-jsonc.json",
  "entry": ["test/**/*.{ts,tsx}"],
  "project": ["./**/*.{js,jsx,ts,tsx}"],
}

I get buildprocess/.eslintrc.js and test/.eslintrc.js flagged as unused, but not .eslintrc.js. If I change knip.jsonc to:

{
  "$schema": "https://unpkg.com/knip@5/schema-jsonc.json",
  "entry": ["test/**/*.{ts,tsx}"],
  "project": ["./**/*.{js,jsx,ts,tsx}"],
  "eslint": {
    "config": [
      "**/.eslintrc",
      "**/.eslintrc.{js,json,cjs}",
      "**/.eslintrc.{yml,yaml}",
      "package.json"
    ],
    "entry": [
      "eslint.config.{js,cjs,mjs}"
    ]
  }
}

I do not get the .eslintrc.js files in subdirectories flagged either.

@pjonsson pjonsson added the bug Something isn't working label Feb 24, 2025
@webpro
Copy link
Member

webpro commented Feb 24, 2025

Please provide an actual and minimal reproduction. Without, I can't see if there are workspaces or how the files are referencing each other, etc.

@pjonsson
Copy link
Author

Here's a zip of a git repository: repro-knip-cascading-eslint-config.zip

I used the node:20 docker image for these commands: after yarn install, run yarn gulp lint to see that three lint errors appear, and yarn knip to get:

Unused files (2)
buildprocess/.eslintrc.js
test/.eslintrc.js

After that, add:

"eqeqeq": 0,

before "no-sync": 0 in buildprocess/.eslintrc.js and run yarn gulp lint to see that only two lint errors remain, so the ESLint config in that directory is used.

@webpro
Copy link
Member

webpro commented Feb 24, 2025

Thanks for the reproduction, that's clear.

The issue here is that Knip does not implement all the logic that ESLint itself has. So indeed the other .eslintrc.js on non-default locations are unused to Knip because there are no explicit references to it.

Here are the defaults for the ESLint plugin: https://knip.dev/reference/plugins/eslint

This an example of how it can be overridden (as you were already doing):

{
  "$schema": "https://unpkg.com/knip@5/schema-jsonc.json",
  "entry": [
    "gulpfile.js", "test/**/*Spec.js"
  ],
  "eslint": [".eslintrc.js", "buildprocess/.eslintrc.js", "test/.eslintrc.js"] // or ["**/.eslintrc.js"]
}

@pjonsson
Copy link
Author

The issue here is that Knip does not implement all the logic that ESLint itself has.

As far as I can tell, the cascading configuration "logic" in ESLint is that any configuration file in a subdirectory to the directory of the root ESLint configuration file is used, as described in the paragraph directly under the first code block here: https://eslint.org/docs/v8.x/use/configure/configuration-files#cascading-and-hierarchy

So indeed the other .eslintrc.js on non-default locations are unused to Knip because there are no explicit references to it.

I don't see how the .eslintrc.js files differ in that respect, there are no explicit references to the root .eslintrc.js either.

If you don't want to add **/ before the non-flat configuration filenames in the ESLint plugin, could you at least add some documentation on the ESLint plugin webpage that mentions cascaded ESLint configurations are not supported?

@webpro
Copy link
Member

webpro commented Feb 25, 2025

You do have a point. My initial reluctance came from the fact that none of the Knip plugins have this "expensive" glob pattern for config files, and only few tools actually support config cascade.

However, ESLint v9 does not seem to have this feature. Adding a note to the docs.

Thanks, glad we got this cleared up.

@webpro webpro closed this as completed in ee2c94d Feb 25, 2025
@webpro
Copy link
Member

webpro commented Feb 25, 2025

🚀 This issue has been resolved in v5.45.0. See Release 5.45.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants