Fix exit code 2 when every error in a file is suppressed and anything else is reported
What minimal example or steps are needed to reproduce the bug?
npm i -D [email protected]
printf 'a { color: white; }\n' > a.css
printf 'b {}\n' > b.css
npx stylelint a.css --suppress # records a.css: color-named 1
npx stylelint a.css # exit 0
npx stylelint a.css b.css # exit 2 — "0 errors, 1 warning"
npx stylelint a.css --formatter json # exit 2What minimal configuration is needed to reproduce the bug?
{
"rules": {
"color-named": "never",
"block-no-empty": [true, { "severity": "warning" }]
}
}How did you run Stylelint?
CLI, as above. The Node.js API shows the same state: after --suppress, stylelint.lint({ files: ['a.css'] }) returns errored: true with an empty warnings array for a.css.
Which Stylelint-related dependencies are you using?
{ "stylelint": "17.15.0" }Also reproduces on 17.14.1.
What did you expect to happen?
Exit code 0 for npx stylelint a.css b.css and for npx stylelint a.css --formatter json. Every error-severity problem is suppressed, and the output says 0 errors, 1 warning.
What actually happened?
Exit code 2 in both cases.
npx stylelint a.css exits 0 only because nothing is printed. lib/cli.mjs returns early when the report is empty, before it checks errored. As soon as any other output exists (a warning in another file, or a formatter that always prints, like json), the run fails although no unsuppressed error remains.
Do you have a proposal to fix the bug?
SuppressionsService.applySuppressions (lib/utils/suppressionsService.mjs) removes the suppressed warnings from result.warnings. It leaves result.errored at the true that linting set, and prepareReturnValue then counts the file as errored.
Recomputing the flag after the filter fixes it:
result.warnings = result.warnings.filter((warning) => warning.rule !== rule);
result.errored = result.warnings.some((warning) => warning.severity === 'error');Checked with the steps above on 17.15.0:
| Run | Before | After |
|---|---|---|
a.css b.css |
2 | 0 |
a.css --formatter json |
2 | 0 |
a new color-named error added to suppressed a.css |
2 | 2 |
Parse errors stay covered, because prepareReturnValue checks parseErrors separately. I am happy to open a PR with this change and a test.
Source: stylelint/stylelint