Why does a pre-commit hook running `eslint --max-warnings 0` on the staged file list fail on generated files the ESLint config ignores, and what fixes it?
answer
- named on the command line
- an ignored file is not silent
- one warning per ignored path
- threshold counts it
- a flag that drops the notice
basics
~20 sESLint skips an ignored file named on the command line but reports a one-line File ignored warning for it, and --max-warnings 0 turns that warning into exit code 1. Add --no-warn-ignored so ignored files stay skipped and silent.
solid answer
~40 sWhen ESLint finds files itself, from `.` or a glob, ignored files are skipped without a warning. A hook that passes the staged file names is different: each ignored path is **named explicitly**, and ESLint answers with a result holding one warning, `File ignored because of a matching ignore pattern`, or `File ignored by default` under `node_modules`. The file is not linted, but the warning counts, so with `--max-warnings 0` the commit fails with exit code `1`. The documented fix is `--no-warn-ignored`, made for exactly this pairing. `--no-ignore` would lint the generated files instead, and `--quiet` only hides the warning, which still counts toward `--max-warnings`. If the hook also passes `--fix`, ESLint rewrites the files in the working tree, and getting those edits back into the commit is the hook's job.
code
bash · 10 lines$ npx eslint --max-warnings 0 src/lockers/status.ts src/generated/locker-api.ts
/repo/src/generated/locker-api.ts
0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to disable file ignore settings or use "--no-warn-ignored" to suppress this warning
✖ 1 problem (0 errors, 1 warning)
ESLint found too many warnings (maximum: 0).
$ echo $?
1go deeper
Remember that an ignored file named on the command line is not linted but produces a File ignored warning, and that --no-warn-ignored removes it.
Explain the difference between patterns ESLint expands, which skip ignored files silently, and explicit file names, which produce a warning that --max-warnings counts.
Diagnose a hook that fails on files nobody wrote: read the File ignored lines, reject --no-ignore and --quiet as fixes, and cover the empty-file-list and unconfigured-file edges.
Keep one source of truth for what is linted: let the config's ignores decide and pass --no-warn-ignored, rather than copying ignore patterns into every hook and script.
## The setup that fails Picture an operator dashboard for a parcel-locker network. Its `eslint.config.js` ignores `src/generated/**`, where an API client is regenerated on every schema change. A pre-commit hook collects the names of the staged JavaScript and TypeScript files and runs: ```bash npx eslint --max-warnings 0 src/lockers/status.ts src/generated/locker-api.ts ``` The developer's own file is clean, yet the commit is rejected with exit code `1`. ## Why an ignored file produces a warning ESLint treats two kinds of input differently: - **Patterns it expands itself**, such as `.` or `"src/**"`. Ignored files are dropped while ESLint searches, and nothing is reported for them, unless every file a pattern matches is ignored, which stops the run with an error and exit code `2`. - **File names passed explicitly.** ESLint assumes you meant that file, so instead of staying silent it returns a result for it with a single warning-level message and no rule ID, and it does not lint the file. The message depends on why the file is skipped: | Why the file is skipped | Message | |---|---| | it matches an ignore pattern | `File ignored because of a matching ignore pattern. Use "--no-ignore" to disable file ignore settings or use "--no-warn-ignored" to suppress this warning.` | | it sits under `node_modules` | `File ignored by default because it is located under the node_modules directory. ...` | | no configuration object matches it, such as a Markdown file | `File ignored because no matching configuration was supplied.` | Each of these counts as one warning. That is harmless in a plain run, which still exits `0`. With `--max-warnings 0`, one ignored staged file is one warning too many, and ESLint exits `1` with `ESLint found too many warnings (maximum: 0).` ## The fix and the flags that look like fixes 1. **`--no-warn-ignored`** suppresses the ignored-file warning for explicitly passed files. ESLint's CLI documentation describes it as useful with `--max-warnings 0`, because it prevents exactly this exit code `1`. The generated file stays unlinted and unreported. 2. **`--no-ignore`** is the wrong tool: it disables ignore patterns, so ESLint lints the generated client and fails on whatever that code contains. 3. **`--quiet`** hides warnings from the output, but the exit code is computed from the full results. With `--max-warnings 0` set, the hidden warning still counts and the commit still fails, now with an empty report. 4. **Filtering the file list in the hook** works too, but it duplicates the ignore patterns in a second place that can drift from `eslint.config.js`. ```bash npx eslint --no-warn-ignored --max-warnings 0 src/lockers/status.ts src/generated/locker-api.ts # exit code 0: status.ts is linted, locker-api.ts is skipped without a warning ``` ## Two more edges of a staged-files run - **An empty file list.** If a commit stages only files the hook filters out, the hook may call `eslint` with no file arguments. ESLint then lints `.`, the whole project, which is slow and can fail on files nobody touched. `--pass-on-no-patterns` makes ESLint exit `0` when no patterns are passed. - **`--fix` in the hook.** `--fix` writes fixes to the files on disk and reports only what remains. ESLint does not touch the Git index, so whether the fixed content ends up in the commit depends on the hook tooling, which is a question about Git hook managers rather than ESLint. ## A checklist for a staged-files lint command - Pass `--no-warn-ignored` whenever `--max-warnings` is set, so ignored and unconfigured files do not count. - Keep the ignore list in `eslint.config.js` only; let ESLint decide what is skipped. - Decide what happens when the file list is empty: filter in the hook, or pass `--pass-on-no-patterns`. - If the hook passes `--fix`, remember that ESLint edits the working tree only; the remaining, unfixed problems decide the exit code. - Read exit code `2` as a setup failure, such as a config that does not load, not as a lint failure. ## Why this matters beyond the hook The same behaviour appears wherever a tool hands ESLint an explicit list of paths: an editor task, a script that lints the files changed on a branch, or a CI step that passes a computed file list. Any of them combined with `--max-warnings 0` needs `--no-warn-ignored`, or it fails on files the config already says not to lint.
- Why does `eslint --max-warnings 0 .` pass on the same repository while the hook fails?With `.` ESLint expands the pattern itself and drops ignored files silently, so no warning is produced. The hook names each staged file explicitly, and an explicitly named ignored file gets a `File ignored ...` warning, which counts toward `--max-warnings`.
- The hook stages a Markdown file and the config has no object matching `.md`. What does ESLint do with it?It does not lint it and, because it was named explicitly, reports `File ignored because no matching configuration was supplied.` as a warning. With `--max-warnings 0` that fails the commit too; `--no-warn-ignored` silences it, and so does passing only file types the config covers.
- A commit stages no lintable files and the hook ends up calling `eslint` with no file arguments. What happens?ESLint assumes `.` and lints the whole project, which is slow and can fail on files outside the commit. `--pass-on-no-patterns` makes ESLint exit `0` when no file or directory patterns are given.
- ESLint lints an ignored file anyway when you pass its name explicitly.
- --no-ignore is the flag for skipping ignored files without a warning.
- --quiet stops ignored-file warnings from counting toward --max-warnings.
- An ignored file passed by name makes ESLint exit with code 2.
- Ignore patterns only apply when ESLint is run on a directory.