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: FlatESLint
getRulesMetaForResults
fails on anonymous files when option cwd
is set
#16410
Comments
Thanks for finding this, PR is welcome!
That's the bug here,
Makes sense, but we should probably account for the case when the file is ignored (so it has no config) but appears in the results with a "File ignored..." warning. |
Thanks for the feedback @mdjermanovic.
Absolutely. Looking at the code, the calculated config is only ever used to fetch the rules meta for those results that contain messages (or message with a eslint/lib/eslint/flat-eslint.js Lines 696 to 703 in 173e820
Currently, files without rule-related messages have their configs calculated but never used, not even validated. These include ignored files, files without lint problems and files with syntax errors. Now this leaves room for some optimization: we could calculate the config lazily, and do it only if one of the messages has a Another option is to keep the config calculation for every result, except if the file is ignored. If no config is found, then throw an error. To me, the first solution seems preferable, because it's more conservative and we would not need to check if a file is ignored beforehand. Any thoughts? |
|
@mdjermanovic That sounds reasonable. So we'll just (re)load the config whenever a message with a |
…16437) * fix: handle files with unspecified path in `getRulesMetaForResults` * In `getRulesMetaForResults`, files with an unspecified path are now treated as if they were located inside `cwd`. * In `getRulesMetaForResults`, when a result referencing a rule has no config, we will explicitly throw an error with a descriptive message. * Added top-level, internal functions `getPlaceholderPath` and `createExtraneousResultsError` to avoid code repetition. * Added two new unit tests. * Renamed an existing unit test to better disambiguate it from a new one. Also changed the assertion to check both error message and constructor. Fixes #16410 * Add a new unit test, move around an existing one * Add a unit test with linted and ignored files * Assert that there is an ignored file
Environment
Node version: v18.10.0
npm version: v8.19.2
Local ESLint version: v8.25.0 (Currently used)
Global ESLint version: Not found
Operating System: darwin 21.6.0
What parser are you using?
Default (Espree)
What did you do?
Using
FlatESLint
, the methodgetRulesMetaForResults
thows an error for certain values of the optioncwd
when passed a result of an anonymous file. This happens whenevercwd
is set to a subdirectory of the current directory.The following repro shows the problem.
What did you expect to happen?
Calling
getRulesMetaForResults
with the result of an anonymous file should not throw an error, regardless of the engine'scwd
.What actually happened?
The above repro fails with an error like this:
Participation
Additional comments
What strikes me in the implementation are the different values used to call
configs.getConfig
in the case of an anonymous file:lintText
callsconfigs.getConfig
with a full path like${cwd}/__placeholder__.js
eslint/lib/eslint/flat-eslint.js
Line 956 in 173e820
whereas
getRulesMetaForResults
ends up callingconfigs.getConfig("__placeholder__.js")
eslint/lib/eslint/flat-eslint.js
Lines 689 to 699 in 173e820
Then it finds no config and it fails.
Another issue related to this is the unhelpful message of the
TypeError
.When
getRulesMetaForResults
finds no matching config, it could simply tell the user that the results were not created from the current instance of ESLint (not the case here, but still).The error message is already there, but it is currently only used in a more limited edge case:
eslint/lib/eslint/flat-eslint.js
Line 684 in 173e820
The text was updated successfully, but these errors were encountered: