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

[import/no-unused-modules] - "No files matching the pattern" error #1645

Open
EvgenyOrekhov opened this issue Feb 5, 2020 · 39 comments
Open

Comments

@EvgenyOrekhov
Copy link

I'm using both eslint-plugin-import and eslint-plugin-json. When there are only *.json files in my project, I get "No files matching the pattern" error from the import/no-unused-modules rule, and ESLint crashes.

Steps to reproduce:

  1. Clone this minimal reproducible case: https://github.com/EvgenyOrekhov/eslint-config-hardcore/tree/no-files-matching-the-pattern-bug
  2. npm install
  3. npm test

Actual result:

> [email protected] test C:\eslint-config-hardcore
> eslint --ext .js,.json .


Oops! Something went wrong! :(

ESLint: 6.8.0.

No files matching the pattern "C:\eslint-config-hardcore" were found.
Please check for typing mistakes in the pattern.

npm ERR! Test failed.  See above for more details.

Expected result: there should be no "No files matching the pattern" error, there should be only one warning from eslint-plugin-json

> [email protected] test C:\eslint-config-hardcore
> eslint --ext .js,.json .


C:\eslint-config-hardcore\invalid.json
  1:2  error  Expected a JSON object, array or literal  json/*

✖ 1 problem (1 error, 0 warnings)

npm ERR! Test failed.  See above for more details.

When I remove the import/no-unused-modules rule from .eslintrc.json, the problem goes away.

@EvgenyOrekhov
Copy link
Author

Note that it's not about *.json files. It's about the absence of *.js files. If I try to use the import/no-unused-modules rule in a TypeScript project, and if there are no *.js files, I get the same error.

@ljharb
Copy link
Member

ljharb commented Feb 5, 2020

This is an error coming from eslint itself. By default, if you're linting "no javascript files", it's probably a bug.

If you have no .foo files, don't pass .foo to --ext.

@EvgenyOrekhov
Copy link
Author

@ljharb I have a config that has ~500 rules from 15 different plugins, and only import/no-unused-modules is causing this error.

@ljharb
Copy link
Member

ljharb commented Feb 5, 2020

Indeed; it's the only rule where we're using eslint's FileEnumerator logic (which is what the command-line eslint uses as well).

You can fix it by configuring here src to be an empty array (it defaults to process.cwd()).

@EvgenyOrekhov
Copy link
Author

@ljharb Will it actually perform the analysis this way? Reading the docs makes me think it won't.

@ljharb
Copy link
Member

ljharb commented Feb 5, 2020

If you have no .js files, then there's nothing to analyze - json files don't import anything. If you have .ts files, then configure src to be ['**/*.ts'].

@EvgenyOrekhov
Copy link
Author

@ljharb I would like to be able to use my config in any project, irregardless of which types of files they have, without having to readjust the import/no-unused-modules rule for each one. Is it possible somehow?

@ljharb
Copy link
Member

ljharb commented Feb 6, 2020

I'm not sure how; many rules - including that one - require per-project config, and env settings are always app-specific.

@EvgenyOrekhov
Copy link
Author

@ljharb

If you have no .foo files, don't pass .foo to --ext.

I just tried running eslint --ext .json . on my example, still got the "No files matching the pattern" error.

@ljharb
Copy link
Member

ljharb commented Mar 4, 2020

I see what you're saying; but the assumption is that an eslint config is always running on the full set of files that config applies to.

One thing you could do is configure using overrides a no-unused-modules rule for each file extension. Does that work?

@EvgenyOrekhov
Copy link
Author

@ljharb Tried this:

test.ts

export default 123;

.eslintrc.json

{
    "parserOptions": {
        "ecmaVersion": 2020,
        "sourceType": "module"
    },

    "plugins": ["import"],

    "overrides": [
        {
            "files": ["*.ts"],
            "rules": {
                "import/no-unused-modules": [
                    "error",
                    {
                        "missingExports": false,
                        "unusedExports": true
                    }
                ]
            }
        }
    ]
}
> eslint --ext .ts .


Oops! Something went wrong! :(

ESLint: 6.8.0.

No files matching the pattern "C:\eslint-config-hardcore" were found.
Please check for typing mistakes in the pattern.

npm ERR! Test failed.  See above for more details.

@ljharb
Copy link
Member

ljharb commented Mar 5, 2020

src isn't configured in that example for the import/no-unused-modules rule; could you configure it?

@EvgenyOrekhov
Copy link
Author

@ljharb Yeah, adding "src": ["**/*.ts"], fixed the error.

So, in order to have a generic config that would work with several file extensions without throwing any errors, I would have to do this:

{
    "parserOptions": {
        "ecmaVersion": 2020,
        "sourceType": "module"
    },

    "plugins": ["import"],

    "overrides": [
        {
            "files": ["*.js"],
            "rules": {
                "import/no-unused-modules": [
                    "error",
                    {
                        "src": ["**/*.js"],
                        "missingExports": false,
                        "unusedExports": true
                    }
                ]
            }
        },
        {
            "files": ["*.ts"],
            "rules": {
                "import/no-unused-modules": [
                    "error",
                    {
                        "src": ["**/*.ts"],
                        "missingExports": false,
                        "unusedExports": true
                    }
                ]
            }
        },
        {
            "files": ["*.jsx"],
            "rules": {
                "import/no-unused-modules": [
                    "error",
                    {
                        "src": ["**/*.jsx"],
                        "missingExports": false,
                        "unusedExports": true
                    }
                ]
            }
        },
        {
            "files": ["*.tsx"],
            "rules": {
                "import/no-unused-modules": [
                    "error",
                    {
                        "src": ["**/*.tsx"],
                        "missingExports": false,
                        "unusedExports": true
                    }
                ]
            }
        }
    ]
}

I guess I could live with that, but I would rather prefer to not have to do it.

@ljharb
Copy link
Member

ljharb commented Mar 6, 2020

Yes, that seems right to me.

One last try for a single config - what if you do "src": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]?

@EvgenyOrekhov
Copy link
Author

@ljharb Tried doing that, plus the same extensions in the 'files' array - got the error.

@ljharb
Copy link
Member

ljharb commented Mar 6, 2020

In that case I think the multiple configs is the only current option.

It seems useful to preserve the error if any of the globs you provide result in matching zero files.

@EvgenyOrekhov
Copy link
Author

If I had to do it for every rule, that would be horrible.

@ljharb
Copy link
Member

ljharb commented Mar 7, 2020

Very true, but most rules apply to a file; this one has to check a whole codebase.

@EvgenyOrekhov
Copy link
Author

@ljharb Trying to fix it here: https://github.com/EvgenyOrekhov/eslint-config-hardcore/pull/59/files. Something weird is going on, now I'm getting a false positive: https://travis-ci.org/EvgenyOrekhov/eslint-config-hardcore/builds/659511799#L213. Am I missing something?

@ljharb
Copy link
Member

ljharb commented Mar 7, 2020

Hmm, i wonder if with this approach, TS files not used by TS files (etc) would be considered unused.

In other words, I’m no longer sure if it’s possible for this rule to support the use case of running on “not every extension the codebase uses at once”. In other words, this might after all require each project to configure it themselves (ie, it might not be possible to configure it in a generic sense, for a shared config)

@EvgenyOrekhov
Copy link
Author

I know that in ESLint 7 they will have some changes to the way ESLint handles file extensions. For example, they will not require using the --ext option, having overrides with specific extensions will be enough. Maybe it will fix this issue with no-unused-modules.

@EvgenyOrekhov
Copy link
Author

@ljharb I upgraded ESLint to 7.0.0 and removed the --ext option in my examples (the minimal reproducible case and the workaround), and I'm still getting the same errors.

@ljharb
Copy link
Member

ljharb commented Jun 1, 2020

@EvgenyOrekhov when i clone your minimal repro, and run npm install, npm test passes (as does npx eslint . and npx eslint --ext=.json *.json). What should I see?

@EvgenyOrekhov
Copy link
Author

@ljharb Make sure you checkout the no-files-matching-the-pattern-bug branch after you clone the repo. I get No files matching the pattern "C:\eslint-config-hardcore" were found. error.

I updated the code to use eslint . command. The goal is to make eslint . command (without any extra options like --ext) work even when there are no *.js files in the folder.

@EvgenyOrekhov
Copy link
Author

@ljharb I added a Travis CI config, you can see the log here: https://travis-ci.com/github/EvgenyOrekhov/eslint-config-hardcore/builds/169082082.

@ljharb
Copy link
Member

ljharb commented Jun 1, 2020

aha, the branch :-) thanks, will look.

@ljharb
Copy link
Member

ljharb commented Jun 1, 2020

@EvgenyOrekhov sooooo yay? I reproduced your error, and then i npm linked in the master branch of eslint-plugin-import, and now I get:

/path/to/eslint-config-hardcore/invalid.json
  1:2  error  Expected a JSON object, array or literal  json/*

✖ 1 problem (1 error, 0 warnings)

Looks like the next release will fix this?

@EvgenyOrekhov
Copy link
Author

@ljharb Great, looks like it will! Feel free to close this issue. I will check it once the next version is released.

@ljharb
Copy link
Member

ljharb commented Jun 1, 2020

Thanks for the repro case :-) if there's a trivial way to add it as a test case to this repo, so we don't regress, that would be awesome too.

@ljharb ljharb closed this as completed Jun 1, 2020
@ljharb
Copy link
Member

ljharb commented Jun 5, 2020

Hmm; I just tried again and the error occurs again, even with it linked - but when I try to make a test case in this repo, it provides the expected error.

I wonder if merely updating your lockfile with npm update would fix it - could you confirm?

ljharb added a commit to ljharb/eslint-plugin-import that referenced this issue Jun 5, 2020
ljharb added a commit to ljharb/eslint-plugin-import that referenced this issue Jun 5, 2020
ljharb added a commit to ljharb/eslint-plugin-import that referenced this issue Jun 5, 2020
@ljharb
Copy link
Member

ljharb commented Jun 5, 2020

I've added the test case to master; hopefully that's accurate and also prevents future regressions.

@EvgenyOrekhov
Copy link
Author

@ljharb I updated all dependencies (including all transitive ones) in my repro case. Still getting the same error. Looking forward to the next release of eslint-plugin-import!

@EvgenyOrekhov
Copy link
Author

@ljharb I updated eslint-plugin-import in my repro case to 2.21.1, and I'm still getting the same error: No files matching the pattern.

:(

@ljharb
Copy link
Member

ljharb commented Jun 8, 2020

I’ll reopen - the next step, i think, is for you to try to make a PR with a failing test case, since my existing test cases pass :-/

@ljharb ljharb reopened this Jun 8, 2020
@mcabs3
Copy link

mcabs3 commented Jun 16, 2020

@EvgenyOrekhov

I was having a relevant issue with the cli

eslint --ext .js,.jsx,.ts,.tsx src and alternative eslint src/**/*.{js,jsx,ts,tsx}

what worked for me to "fix" this error was to add quotes... ** facepalm **

eslint --ext ".js,.jsx,.ts" src and eslint \"src/**/*.{js,jsx,ts,tsx}\"

NOT a fix, but it looks like the CLI accepts this

@EvgenyOrekhov
Copy link
Author

@mcabs3 I need to make it work without specifying extensions: eslint .

@marcos-venicius
Copy link

it works on eslint 7.32.0 eslint . --ext .js,.ts,.tsx --fix

but, on version 8.23.0 i use like that: eslint . --ext .js --ext .ts --ext .tsx --fix

and it works

and it is my .eslint file i'm using NextJS

{
  "root": true,
  "extends": "next/core-web-vitals",

  "overrides": [
    {
      "files": ["*.ts", "*.tsx"],
      "rules": {
        "no-console": "warn",
        "semi": "off",
        "no-extra-semi": "error",
        "semi-spacing": "error",
        "semi-style": "error"
      }
    }
  ]
}

@ljharb
Copy link
Member

ljharb commented Nov 21, 2022

@marcos-venicius if the comma form for extensions doesn’t work, that’s an eslint bug; please file it with them.

@soheilous
Copy link

If you guys using turborepo, please don't forget to put root: true in your eslint configs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants