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

Bug: unhandled error in lintFiles with glob patterns when config is invalid #18250

Closed
1 task done
fasttime opened this issue Mar 30, 2024 · 6 comments · Fixed by #18295
Closed
1 task done

Bug: unhandled error in lintFiles with glob patterns when config is invalid #18250

fasttime opened this issue Mar 30, 2024 · 6 comments · Fixed by #18295
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features repro:yes

Comments

@fasttime
Copy link
Member

Environment

Node version: v21.7.1
npm version: v10.5.0
Local ESLint version: v9.0.0-rc.0 (Currently used)
Global ESLint version: Not found
Operating System: darwin 23.4.0

What parser are you using?

Default (Espree)

What did you do?

Configuration
export default {
  invalid: 'foobar',
};
// index.js
import { ESLint } from 'eslint';

const eslint = new ESLint();
try {
  await eslint.lintFiles('*.js');
} catch {
  console.log('An error should be caught because the config is invalid');
}
console.log('This message should be printed');

What did you expect to happen?

lintFiles should throw an error in the current execution context because the config is invalid.

What actually happened?

The process terminated abnormally while running lintFiles because of an uncaught error in an asynchronous callback.

$ node ./index.js
.../project/node_modules/@humanwhocodes/object-schema/src/object-schema.js:270
                throw new UnexpectedKeyError(key);
                ^

UnexpectedKeyError: Unexpected key "invalid" found.
    at ObjectSchema.validate (.../project/node_modules/@humanwhocodes/object-schema/src/object-schema.js:270:23)
    at .../project/node_modules/@humanwhocodes/object-schema/src/object-schema.js:239:18
    at Array.reduce (<anonymous>)
    at ObjectSchema.merge (.../project/node_modules/@humanwhocodes/object-schema/src/object-schema.js:237:24)
    at .../project/node_modules/@humanwhocodes/config-array/api.js:935:42
    at Array.reduce (<anonymous>)
    at FlatConfigArray.getConfig (.../project/node_modules/@humanwhocodes/config-array/api.js:934:39)
    at FlatConfigArray.isFileIgnored (.../project/node_modules/@humanwhocodes/config-array/api.js:962:15)
    at .../project/node_modules/eslint/lib/eslint/eslint-helpers.js:322:49
    at Array.reduce (<anonymous>)

The callback in question is the entryFilter of @nodelib/fs.walk, which apparently is not expected to throw errors.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/stackblitz-starters-swoxsq

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No response

@fasttime fasttime added bug ESLint is working incorrectly repro:needed labels Mar 30, 2024
@mdjermanovic mdjermanovic added repro:yes core Relates to ESLint's core APIs and features accepted There is consensus among the team that this change meets the criteria for inclusion and removed repro:needed labels Apr 1, 2024
@mdjermanovic
Copy link
Member

I can reproduce this. Any ideas how to fix it?

@fasttime
Copy link
Member Author

fasttime commented Apr 2, 2024

Any ideas how to fix it?

We could catch the error in entryFilter and rethrow it after doFsWalk. Or we could call fswalk.walk inside a promise executor (instead of wrapping it with util.promisify), so we could reject the promise when entryFilter throws an error.

Finally, we could open an issue at nodelib and ask them to handle errors in their filter callbacks, so we don't have to. Upgrading to @nodelib/fs.walk 2.0.0 didn't help, and I couldn't find a closed issue in their repo that matches our problem.

@mdjermanovic
Copy link
Member

Finally, we could open an issue at nodelib and ask them to handle errors in their filter callbacks, so we don't have to. Upgrading to @nodelib/fs.walk 2.0.0 didn't help, and I couldn't find a closed issue in their repo that matches our problem.

This seems like the best solution, but it will probably take some time. Can you open an issue there?

Or we could call fswalk.walk inside a promise executor (instead of wrapping it with util.promisify), so we could reject the promise when entryFilter throws an error.

This would be the easiest solution for us at the moment, though I wouldn't expect it to work if util.promisify doesn't, but let's try it.

@fasttime
Copy link
Member Author

fasttime commented Apr 4, 2024

Thanks for the feedback @mdjermanovic.

Finally, we could open an issue at nodelib and ask them to handle errors in their filter callbacks, so we don't have to. Upgrading to @nodelib/fs.walk 2.0.0 didn't help, and I couldn't find a closed issue in their repo that matches our problem.

This seems like the best solution, but it will probably take some time. Can you open an issue there?

Sure!

Or we could call fswalk.walk inside a promise executor (instead of wrapping it with util.promisify), so we could reject the promise when entryFilter throws an error.

This would be the easiest solution for us at the moment, though I wouldn't expect it to work if util.promisify doesn't, but let's try it.

I can look into this and see what work works for us.

@nzakas
Copy link
Member

nzakas commented Apr 9, 2024

Issue: nodelib/nodelib#105

@pumano
Copy link

pumano commented Apr 17, 2024

got same error when using nx and config contains:

 languageSettings: { parserOptions: { project: ["./tsconfig.*?.json"] } }

looks like schema does not contain that field and error handling processed incorrectly (as mentioned above)

console output:

node_modules/@nx/eslint/node_modules/@humanwhocodes/object-schema/src/object-schema.js:270
                      throw new UnexpectedKeyError(key);
                      ^
      
      UnexpectedKeyError: Unexpected key "languageSettings" found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features repro:yes
Projects
Status: Complete
Development

Successfully merging a pull request may close this issue.

4 participants