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

feat: Provide helpful error message for nullish configs #18357

Merged
merged 3 commits into from Apr 19, 2024
Merged

Conversation

nzakas
Copy link
Member

@nzakas nzakas commented Apr 17, 2024

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[x] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Upgraded @humanwhocodes/config-array to include new error details, then updated FlatConfigArray to use that info to produce an error message containing the index of the config that caused the error.

fixes #18259

Is there anything you'd like reviewers to focus on?

I used the terms "original", "base", and "user-defined" to differentiate between the different parts of the config array. I'm not sure how accurate those are but they are at least indicating the different parts of the array where the error occurred.

@nzakas nzakas requested a review from a team as a code owner April 17, 2024 21:31
@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Apr 17, 2024
@github-actions github-actions bot added the core Relates to ESLint's core APIs and features label Apr 17, 2024
Copy link

netlify bot commented Apr 17, 2024

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit 2833384
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/6621877a9912550008cb597c

Comment on lines +95 to +99
* A config array is set up in this order:
* 1. Base config
* 2. Original configs
* 3. User-defined configs
* 4. CLI-defined configs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A CLI-defined config is what gets passed to the overrideConfig option of the ESLint constructor? When that fails normalization, it's unclear what the index in the error message is meant to be:

import { ESLint } from "eslint";
const eslint = new ESLint({ overrideConfig: [undefined] });
await eslint.lintFiles("*.js");

When running the above code with node in the eslint repo directory, the error message will be

TypeError: Config (unnamed): Unexpected undefined config at user-defined index 24.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also ignorePatterns.

There is no easy way to determine when CLI flags have been added from inside the config array, so I think this error is okay for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe configIndex and location (base, original, user-defined, ignore-pattern, etc.) should be added as metadata to each config before normalization, because they are known at that time regardless of the order in which the configs are inserted into the config array. Then, when an error occurs, there would be no need to calculate them back based on the final index.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea! 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require a lot of extra work for very little extra value. Because we're talking about an array, there are many different ways configs could potentially be added.

I think what I have here is good enough for where we are right now. It provides helpful context without much overhead. If we find that this doesn't serve our purpose in the future, we can revisit.

lib/config/flat-config-array.js Outdated Show resolved Hide resolved
lib/config/flat-config-array.js Outdated Show resolved Hide resolved
nzakas and others added 2 commits April 18, 2024 13:49
Copy link
Member

@fasttime fasttime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! Leaving open for another approval.

@fasttime fasttime added the accepted There is consensus among the team that this change meets the criteria for inclusion label Apr 19, 2024
@fasttime
Copy link
Member

Merging now to get this change in v9.1.0.

@fasttime fasttime merged commit 03068f1 into main Apr 19, 2024
18 checks passed
@fasttime fasttime deleted the issue18259 branch April 19, 2024 21:02
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 core Relates to ESLint's core APIs and features feature This change adds a new feature to ESLint
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Change Request: explicitly handle undefined being passed as a config value
3 participants