eslint-plugin-import icon indicating copy to clipboard operation
eslint-plugin-import copied to clipboard

[no-unused-modules] Error with ESLint configuration non-standard name

Open regseb opened this issue 2 years ago • 6 comments

The import/no-unused-modules rule doesn't work when the ESLint configuration file doesn't have a standard name. In my projects, I group the configuration files in a directory to avoid having too many files in the root (example with Cast Kodi).

  • index.js

    console.log("foo");
    
  • my_eslint.js

    import pluginImport from "eslint-plugin-import";
    
    export default [{
        plugins: { "import": pluginImport },
    
        rules: {
            "no-console": "warn",
            "import/no-unused-modules": ["error", { unusedExports: true }]
        }
    }];
    
  • package.json

    {
        "name": "testcase",
        "version": "1.0.0",
        "type": "module",
        "dependencies": {
            "eslint-plugin-import": "2.31.0",
            "eslint": "9.12.0"
        }
    }
    
  1. npm install

  2. npx eslint -c my_eslint.js index.js

    Oops! Something went wrong! :(
    
    ESLint: 9.12.0
    
    ESLint couldn't find a configuration file. To set up a configuration file for this project, please run:
    
        npm init @eslint/config@latest
    
    ESLint looked for configuration files in /home/sregne/Téléchargements/import and its ancestors. If it found none, it then looked in your home directory.
    
    If you think you already have a configuration file or if you need more help, please stop by the ESLint Discord server: https://eslint.org/chat
    

regseb avatar Jan 14 '23 18:01 regseb

This seems like an issue with eslint itself, and not with eslint-plugin-import - this plugin doesn't do anything with eslint config.

(typically you'd only ever have one eslint config file in any repo, at the root, and you'd use overrides to configure subdirectories, but either way it's specific to eslint)

ljharb avatar Jan 14 '23 21:01 ljharb

@ljharb I created an issue at ESLint and eslint-plugin-import use indirectly ESLint config:

  • https://github.com/eslint/eslint/issues/16787

    The import/no-unused-modules rule indirectly loads eslint configs by using eslint's internal module FileEnumerator. This module isn't intended to be used outside eslint package. When used by eslint, FileEnumerator receives full execution context that includes processed CLI options. The error you're getting is caused by the rule not providing the context to the FileEnumerator instance it's using, so it works as if -c my_eslint.json is not specified and therefore looks for .eslintrc.* config files. We can discuss in https://github.com/eslint/eslint/issues/13481 how this rule could work with the new config system, but until then it seems incompatible with the -c usage.

regseb avatar Jan 15 '23 13:01 regseb

Yes, everything uses it indirectly - the bug is only with something that uses it directly.

ljharb avatar Jan 15 '23 15:01 ljharb

Reopening per the comment on the linked issue.

ljharb avatar Jan 15 '23 15:01 ljharb

This also seems to be an issue with the new eslint flat config file. When I enable this rule, I get the message:

Oops! Something went wrong! :(

ESLint: 8.38.0

ESLint couldn't find a configuration file. To set up a configuration file for this project, please run:

    npm init @eslint/config

ESLint looked for configuration files in [<rootDir>]/[<childDir>] and its ancestors. If it found none, it then looked in your home directory.

If you think you already have a configuration file or if you need more help, please stop by the ESLint Discord server: https://eslint.org/chat

Flat config is in the root directory of the project, with no -c flag provided in the eslint command. Without this rule, everything runs fine, but as soon as I enable it, the above error flags up.

tetarchus avatar Apr 14 '23 16:04 tetarchus

+1, still seeing the same on eslint 9.18.0

thenick775 avatar Jan 23 '25 03:01 thenick775