-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
fix: Ensure globbing doesn't include subdirectories #16272
Conversation
✅ Deploy Preview for docs-eslint canceled.
|
// check if the pattern is inside the directory or not | ||
const patternRelativeToFilePath = path.relative(filePath, fullFilePattern); | ||
|
||
if (patternRelativeToFilePath.startsWith("..")) { | ||
return false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work well when **
(or anything else that isn't a literal directory name) is in the middle of filePattern
.
For example:
// eslint.config.js
module.exports = {
files: ["foo/**/*.md"],
// ...configuration for .md files
};
Command eslint foo/bar
, where foo/bar
is an existing directory, would not find .md
files in foo/bar
directory because patternRelativeToFilePath
will be calculated as ..\**\*.md
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also doesn't work when the pattern starts with a non-literal (except for the special-cased **
).
For example, if the config entry has files: ["{foo,bar}/*.md"]
, command eslint foo
won't find .md
files in foo
because patternRelativeToFilePath
is ..\{foo,bar}\*.md
, so the pattern gets filtered out here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, yeah, this is the trickiest part of all of this. Do you have any suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this step (filtering out patterns that can't match inside the given directory), I think we could use minimatch with partial: true
. But then there's the next step where we should adjust the remaining patterns to match only inside the given directory, which seems overly complex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some alternative approaches:
- Use
globby
to simply get all files in the directory (if the given directory isfoo/bar
, passfoo/bar/**
) and then filter out those that don't match at least one pattern that doesn't end with a*
. I think this solution is the least error-prone. - Negative patterns work as logical AND, so it might be possible to get the intersection by appending some form of double negation to the list of patterns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestions. These were super helpful. I went with a minimatch
-based approach that seems to work. I didn't need to use partial: true
because we actually do have the full path at that point.
I can't reproduce the lint error locally, so I have no idea what's going on there. |
Nevermind, I can reproduce it now. Didn't notice there was a separate command for linting docs JavaScript files. |
@mdjermanovic I could use your help figuring out why the linting in the |
It's separate from the main Lines 508 to 518 in 42bfbd7
It uses the same top-level The problem will be fixed by #16274 |
Oh nice! I'll rebase on top of that. |
* docs: update docs package ver from main on release fixes #16212 * docs: read docs package ver instead of main fixes #16212 * docs: add newline to updated docs package json Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * docs: update docs package json version Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
lib/eslint/eslint-helpers.js
Outdated
} | ||
|
||
// check if the pattern matches | ||
if (minimatch(filePath, path.dirname(fullFilePattern))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think we would need { partial: true }
for this approach.
Consider the following example.
Config:
{
files: ["t*/internal-rules/*.js"],
// ...
}
Command:
eslint tools
The "t*/internal-rules/*.js"
pattern from the config should not be filtered out, because it can match something under the tools
directory?
Current behavior:
filePath: D:\projects\eslint\tools
fullFilePattern: D:\projects\eslint\t*\internal-rules\*.js
path.dirname(fullFilePattern): D:\projects\eslint\t*\internal-rules
minimatch(filePath, path.dirname(fullFilePattern)): false
I think what we need is:
if (minimatch(filePath, fullFilePattern, { partial: true })) {
return true;
}
filePath: D:\projects\eslint\tools
fullFilePattern: D:\projects\eslint\t*\internal-rules\*.js
minimatch(filePath, fullFilePattern, { partial: true }): true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah gotcha. I think I misunderstood the purpose of partial: true
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately mdjermanovic hasn't been able to follow up on the open discussions. Mitigating that, 1) the suggestions from those discussions look to have been implemented as discussed, and 2) this is use-at-your-own-risk code that's easier to change post-release.
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [eslint](https://eslint.org) ([source](https://github.com/eslint/eslint)) | devDependencies | patch | [`8.23.0` -> `8.23.1`](https://renovatebot.com/diffs/npm/eslint/8.23.0/8.23.1) | --- ### Release Notes <details> <summary>eslint/eslint</summary> ### [`v8.23.1`](https://github.com/eslint/eslint/releases/tag/v8.23.1) [Compare Source](eslint/eslint@v8.23.0...v8.23.1) #### Bug Fixes - [`b719893`](eslint/eslint@b719893) fix: Upgrade eslintrc to stop redefining plugins ([#​16297](eslint/eslint#16297)) (Brandon Mills) - [`734b54e`](eslint/eslint@734b54e) fix: improve autofix for the `prefer-const` rule ([#​16292](eslint/eslint#16292)) (Nitin Kumar) - [`6a923ff`](eslint/eslint@6a923ff) fix: Ensure that glob patterns are normalized ([#​16287](eslint/eslint#16287)) (Nicholas C. Zakas) - [`c6900f8`](eslint/eslint@c6900f8) fix: Ensure globbing doesn't include subdirectories ([#​16272](eslint/eslint#16272)) (Nicholas C. Zakas) #### Documentation - [`16cba3f`](eslint/eslint@16cba3f) docs: fix mobile double tap issue ([#​16293](eslint/eslint#16293)) (Sam Chen) - [`e098b5f`](eslint/eslint@e098b5f) docs: keyboard control to search results ([#​16222](eslint/eslint#16222)) (Shanmughapriyan S) - [`1b5b2a7`](eslint/eslint@1b5b2a7) docs: add Consolas font and prioritize resource loading ([#​16225](eslint/eslint#16225)) (Amaresh S M) - [`1ae8236`](eslint/eslint@1ae8236) docs: copy & use main package version in docs on release ([#​16252](eslint/eslint#16252)) (Jugal Thakkar) - [`279f0af`](eslint/eslint@279f0af) docs: Improve id-denylist documentation ([#​16223](eslint/eslint#16223)) (Mert Ciflikli) #### Chores - [`38e8171`](eslint/eslint@38e8171) perf: migrate rbTree to js-sdsl ([#​16267](eslint/eslint#16267)) (Zilong Yao) - [`1c388fb`](eslint/eslint@1c388fb) chore: switch nyc to c8 ([#​16263](eslint/eslint#16263)) (唯然) - [`67db10c`](eslint/eslint@67db10c) chore: enable linting `.eleventy.js` again ([#​16274](eslint/eslint#16274)) (Milos Djermanovic) - [`42bfbd7`](eslint/eslint@42bfbd7) chore: fix `npm run perf` crashes ([#​16258](eslint/eslint#16258)) (唯然) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox. --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzMi4xOTQuNSIsInVwZGF0ZWRJblZlciI6IjMyLjE5NC41In0=--> Co-authored-by: cabr2-bot <cabr2.help@gmail.com> Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1543 Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org> Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org> Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Fixes #16260
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
I changed the resolution mechanism for globbing. Previously, we were automatically looking for every glob pattern inside of the cwd, which could result in more files than necessary being linted. Now, we only look at glob patterns relative to the directory passed in on the command line.
Is there anything you'd like reviewers to focus on?
Please take a look at the new
shallow-glob
fixture to make sure it makes sense. I verified that the test using these files failed prior to this change and passed after this change.