In ESLint 10's flat config, why does `ignores: ["dist/"]` beside `rules` skip nothing, while `globalIgnores(["dist/"])` skips the folder?
answer
- the other keys decide
- ignores alone, apart from name
- non-global patterns match files only
- dir/** instead of dir/
- relative to the config file
basics
~20 sAn ignores key next to other keys only removes files from that one object, and such patterns match files, not directories, so dist/ matches nothing. Alone, or through globalIgnores(), ignores applies to every object and may match directories.
solid answer
~40 sIn flat config, `ignores` has two modes. Next to other keys such as `files` or `rules` it is **non-global**: it only removes files from that object, and its patterns match file paths, so a directory pattern like `"dist/"` matches nothing; `"dist/**"` is needed. In an object with only `ignores` (plus `name`) it is **global**: the patterns apply to every object, may match directories, and ESLint does not even traverse an ignored directory. `globalIgnores([...])` from `eslint/config` builds that object explicitly and names it. Patterns are relative to the config file's directory, so `"dist/"` covers only the top-level `dist`; nested build folders need `"**/dist/"`. `.eslintignore` is not read in flat config.
code
javascript · 12 lines// eslint.config.js
import { defineConfig, globalIgnores } from "eslint/config";
export default defineConfig([
globalIgnores(["**/dist/", "coverage/"], "birdmap/generated"),
{
name: "birdmap/map-tiles",
files: ["packages/map-tiles/**/*.js"],
ignores: ["packages/map-tiles/fixtures/**"],
rules: { "no-console": "off" },
},
]);go deeper
Recall that globalIgnores() is how you keep build output out of the lint run, and that it lives in eslint.config.js.
Explain the two modes of ignores, why a non-global pattern needs dir/** rather than dir/, and how patterns anchor to the config file.
Diagnose why generated files are still linted, by checking whether the ignore object gained another key or lacks a **/ prefix, and fix it with a named globalIgnores().
Decide where ignore knowledge lives, one named global entry or an imported .gitignore, so build outputs added later are excluded by default.
## Two kinds of `ignores` The same `ignores` key behaves differently depending on what sits next to it in a configuration object. | | Non-global `ignores` | Global `ignores` | |---|---|---| | Object shape | `ignores` plus other keys (`files`, `rules`, ...) | `ignores` alone, optionally with `name` | | Scope | Removes files from **this object only** | Removes files from **every object** | | Matches | File paths only (`dir/file.js`, `dir/**`) | Files **and directories** (`dir/`) | | Effect on traversal | The file is still linted with the other objects | ESLint does not descend into an ignored directory | | Explicit helper | none | `globalIgnores([...])` from `eslint/config` | So `{ files: ["**/*.js"], ignores: ["dist/"], rules: {...} }` changes nothing: `dist/` is a directory pattern, and a non-global `ignores` only tests file names. Writing `"dist/**"` would remove the files from that one object, but other objects would still lint them. To stop ESLint from linting the folder at all, the pattern has to be global. ## `globalIgnores()` makes the intent visible An object counts as global ignores only while it holds nothing but `ignores` (and `name`; `defineConfig()` also accepts a `basePath` there). Adding a `rules` key later silently turns it into a non-global ignore. `globalIgnores()`, exported from `eslint/config` since ESLint 9.22.0, returns that object for you and names it, which protects against the accident and labels it in the config inspector: ```js // eslint.config.js at the repository root import { defineConfig, globalIgnores } from "eslint/config"; export default defineConfig([ globalIgnores(["**/dist/", "coverage/"], "birdmap/generated"), { name: "birdmap/map-tiles", files: ["packages/map-tiles/**/*.js"], ignores: ["packages/map-tiles/fixtures/**"], rules: { "no-console": "off" }, }, ]); ``` The first entry keeps every `dist` folder and the top-level `coverage` folder out of the run. The second object's `ignores` only stops its own `no-console` override from reaching the fixtures; the fixtures are still linted by everything else. ## Where patterns are anchored Patterns are **minimatch** globs evaluated relative to the directory of the `eslint.config.js` that defines them, which differs from `.gitignore`: - `"dist/"` ignores only the `dist` directory next to the config file. An app's `apps/web/dist/` needs `"**/dist/"`. - `"temp.js"` ignores only a `temp.js` beside the config file; `"**/temp.js"` ignores it everywhere. - With `--config other.js`, patterns are evaluated relative to the current working directory instead. - `--ignore-pattern` on the command line adds ignore patterns, evaluated relative to the working directory. ESLint always ignores `**/node_modules/` and `.git/` first; your patterns are added after those defaults. Dot-files are not ignored by default. ## Un-ignoring, and the `dir/**` trap A `!` prefix un-ignores a path matched by an earlier pattern. Because a pattern like `build/**` ignores the directory itself, ESLint never enters it, so nothing inside can be un-ignored. To keep one file, ignore the contents instead: 1. `"build/**/*"` ignores everything under `build`, but not the directory. 2. `"!build/test.js"` brings back the one file. ## What replaced `.eslintignore` Flat config does not read `.eslintignore`, and the eslintrc-era `--ignore-path` flag went with it. Move its patterns into `globalIgnores()`, adding `**/` where the old file relied on gitignore-style matching at any depth. `includeIgnoreFile()` from `eslint/config` can import a `.gitignore` instead of copying it. ## Choosing between the two Both forms have a place, and the choice follows from what should happen to the files: - **Never lint these paths** (build output, coverage reports, vendored code): a global ignore, written with `globalIgnores()` so its purpose and name survive later edits. - **Lint these paths, but without one object's settings** (fixtures that may log to the console, generated stubs that need a rule off): a non-global `ignores` with file patterns such as `fixtures/**`, beside the `files` it narrows. - **Lint these paths with different settings**: neither; add a later object whose `files` match them. A config that keeps the global ignores in one named entry at the top of the array is easier to audit than one that scatters `ignores` across many objects. ## Symptoms worth recognising - Generated files still fail the run: the ignore pattern sits in an object that also has `rules`. - Only the root `dist` is skipped: the pattern lacks `**/`. - Passing an ignored file explicitly prints a warning that it was ignored because of a matching ignore pattern, rather than linting it.
- How do you ignore everything in `build/` except `build/test.js`?Use `globalIgnores(["build/**/*", "!build/test.js"])`. The pattern `build/**` would ignore the directory itself, so ESLint would never enter it and the negation could not bring the file back; `build/**/*` ignores only the contents.
- A `.eslintignore` from ESLint 8 is still in the repository after the move to ESLint 10. What happens to it?Nothing: flat config never reads `.eslintignore`. Its patterns must move into `globalIgnores()`, rewritten for flat config anchoring, where `temp.js` means only the file beside the config and `**/temp.js` means everywhere.
- Thinks any ignores key ignores the files for the whole run
- Writes dist/ in a non-global ignores and expects the folder skipped
- Assumes dist/ in a root config also skips apps/web/dist
- Expects ESLint 10 to keep reading .eslintignore beside the flat config
- Believes a negated pattern can bring back a file under an ignored dir/**
Assumes ESLint 10 flat config with globalIgnores() from eslint/config (since 9.22.0); .eslintignore is not read by flat config, the only format ESLint 10 supports.