skip to content

In an ESLint 10 flat config, when several configuration objects match one file, which settings win, and where do `extends` entries land?

level: middleimportance: must knowfreq 28%

answer

  1. order, not specificity
  2. every matching object contributes
  3. later object wins a conflict
  4. extends expands just before its object
  5. files combined: both must match

basics

~20 s

Every matching object applies, merged in array order, and the later object wins a conflict however broad its glob. defineConfig() expands each extends entry into its own object placed just before the extending object, whose own settings then override the preset.

solid answer

~40 s

ESLint 10 computes a file's configuration from every object whose `files` and `ignores` match it, merged in array order. When two objects set the same thing, the later one wins, and glob specificity is irrelevant. Nested objects such as `languageOptions.globals` merge key by key, so a test object adds globals instead of replacing them. `extends` is resolved by `defineConfig()`: each extended config becomes a separate object inserted immediately before the extending object, its `files` combined with the object's own so that both must match, its `ignores` appended. The object's own `rules` therefore override the preset, and any later object in the array overrides both. Without `defineConfig()`, a top-level `extends` is rejected as an eslintrc-format key.

code

javascript · 17 lines
javascript
// eslint.config.js
import js from "@eslint/js";
import { defineConfig } from "eslint/config";

export default defineConfig([
  {
    name: "birdmap/web",
    files: ["apps/web/**/*.js"],
    extends: [js.configs.recommended],
    rules: { "no-unused-vars": "warn" },
  },
  {
    name: "birdmap/all",
    files: ["**/*.js"],
    rules: { "no-unused-vars": "error" },
  },
]);

go deeper

for a junior

Remember that order in the array decides: when two objects that match a file disagree, the later one wins.

for a middle

Explain how defineConfig() flattens extends: one object per extended config, placed before its parent, with files combined so both must match.

for a senior

Trace a real file through a long config, preset objects included, and predict the final setting without running ESLint, then confirm it with the inspector.

for a principal

Argue for an ordering convention a whole team can follow, broad presets first and named narrow exceptions last, so nobody depends on glob specificity.

## The merge rule A flat config is an ordered array of **configuration objects**. For each file it lints, ESLint 10 walks the array and keeps every object whose `files` patterns match the file and whose `ignores` patterns do not. An object without `files` matches everything the config lints. All matching objects are then **merged in array order**. When two of them set the same thing, the **later object wins**. Two consequences surprise people: - **Specificity does not count.** `files: ["apps/web/**/*.js"]` early in the array loses to `files: ["**/*.js"]` later in the array, even though the first pattern is narrower. - **Objects merge deeply.** `languageOptions.globals` from two matching objects are combined, so a file in `tests/` sees the project-wide globals plus the test globals. A rule set again later takes the later setting; a later entry giving only a severity keeps the earlier options, a detail of rule configuration itself. Inline configuration comments are applied after everything in the config file, so they are not part of this ordering. ## How `extends` flattens `extends` lets one object inherit other configurations: an imported config object, a config array, or a plugin's named config. It is handled by `defineConfig()` from `eslint/config` (since ESLint 9.22.0), which rewrites the array before ESLint sees it: 1. Each extended config becomes **its own object**, inserted **immediately before** the object that extends it, in the order the `extends` array lists them. 2. If the extending object has `files`, every extended object's `files` is **combined** with it: each pair of patterns becomes an AND pattern, so a file must match both. An extended object with no `files` simply takes the extending object's patterns. 3. The extending object's `ignores` are **appended** to each extended object's `ignores`. 4. Each generated object is named `"<object name> > <extended name>"`, for example `birdmap/web > @eslint/js/recommended`. 5. The extending object itself stays in place, after its extensions, without the `extends` key. So the object's own `rules` override the preset it extends, and anything later in the array overrides both. `defineConfig()` rejects nested `extends` and a `basePath` inside an extended config. A config array exported without `defineConfig()` that still contains `extends` fails validation, because ESLint's own schema treats a top-level `extends` as an eslintrc key. ## Tracing one file ```js // eslint.config.js at the repository root import js from "@eslint/js"; import { defineConfig } from "eslint/config"; export default defineConfig([ { name: "birdmap/web", files: ["apps/web/**/*.js"], extends: [js.configs.recommended], rules: { "no-unused-vars": "warn" }, }, { name: "birdmap/all", files: ["**/*.js"], rules: { "no-unused-vars": "error" } }, ]); ``` For `apps/web/src/map.js` the flattened array has three matching objects: | Position | Object | `no-unused-vars` | |---|---|---| | 1 | `birdmap/web > @eslint/js/recommended` | `"error"` | | 2 | `birdmap/web` | `"warn"` | | 3 | `birdmap/all` | `"error"` | The result is `"error"`, from the last matching object. A file in `packages/map-tiles` matches only `birdmap/all`, because the preset was scoped to `apps/web/**/*.js` by the combination step. ## Designing the order - Put **broad defaults first**: presets, project-wide language options. - Put **narrow exceptions last**: tests, scripts, generated code that needs a rule off. - Prefer `extends` inside a `files`-scoped object over spreading a preset at the top level; ESLint's docs recommend always pairing `extends` with `files`, since a preset without them reaches every linted file. - Give each object a `name`, so the config inspector and error messages say which object a setting came from. ## Common misreadings - **"The preset comes last."** Extended configs sit before their parent, never after; a preset cannot override the object that extends it. - **"`extends` widens the scope."** Combining `files` only ever narrows: an extended config with its own `files` reaches the intersection of both lists. - **"A later object without `files` only fills gaps."** It applies to every linted file and overrides anything earlier on a conflict. - **"Two objects with the same `name` are merged."** Names are labels only; ESLint neither merges nor deduplicates objects by name. ## A mental model Think of each object as a transparent sheet laid over the file, in array order: where two sheets write in the same spot, the one on top is what you read. A sheet covers only the files its `files` patterns match.

  • How do `languageOptions.globals` from two matching objects combine?
    They merge. If one object matching `**/*.js` declares a project global and a later one matching `tests/**/*.js` declares `describe` and `it`, a test file sees all three. Only a key set by both objects takes the later value.
  • Why does an `eslint.config.js` that exports a plain array containing `extends` fail, while the same array inside `defineConfig()` works?
    `extends` is not a key ESLint's own config schema accepts; it treats a top-level `extends` as an eslintrc-format key and reports a configuration error. `defineConfig()` expands `extends` into separate objects before ESLint validates the array.
  • An extended preset carries `files: ["**/*.jsx"]` and the extending object has `files: ["apps/web/**"]`. Which files does the preset reach?
    Only files matching both: `.jsx` files under `apps/web`. `defineConfig()` pairs each base pattern with each extension pattern as an AND pattern, so the preset is narrowed, never widened.

Configuration objects are transparent overlays stacked in array order over each file they match: where two sheets mark the same spot, the top sheet, the later object, is what you read, however small its patch.

  • Says the object with the most specific files glob wins a conflict
  • Believes the first matching object locks a setting for that file
  • Thinks a later object replaces all of an earlier object's globals
  • Assumes extended presets are applied after the extending object's own rules
  • Expects extends to work in an array exported without defineConfig()
  • Thinks an extended config's files widen the extending object's files

Assumes ESLint 10 with defineConfig() from eslint/config (since 9.22.0); later-object-wins merging dates from flat config's introduction, extends flattening from defineConfig().