Skip to content

Commit 8ec818c

Browse files
committed
fix: honor the processErrors hook in Stats.hasErrors
`Stats.hasErrors()` read `compilation.errors` directly while `hasWarnings()` goes through `getWarnings()` (the `processWarnings` waterfall hook). As a result a plugin that filtered errors out of `processErrors` was still contradicted by `hasErrors()` returning true. Route `hasErrors()` through `getErrors()` so both stay symmetric.
1 parent d7237bf commit 8ec818c

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Make `Stats.hasErrors()` read errors through the `processErrors` hook (via `getErrors()`), matching `hasWarnings()`, so a plugin that filters errors out of the hook is no longer contradicted by `hasErrors()`.

‎lib/stats/Stats.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Stats {
4848
*/
4949
hasErrors() {
5050
return (
51-
this.compilation.errors.length > 0 ||
51+
this.compilation.getErrors().length > 0 ||
5252
this.compilation.children.some((child) => child.getStats().hasErrors())
5353
);
5454
}

‎test/unitCases/Errors.unittest.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,3 +934,49 @@ describe("Loaders", () => {
934934
`);
935935
});
936936
});
937+
938+
describe("Stats.hasErrors", () => {
939+
/**
940+
* @param {(compilation: import("../../").Compilation) => void=} tap
941+
* optional per-compilation setup, e.g. to tap `processErrors`
942+
* @returns {Promise<import("../../").Stats>} the stats object
943+
*/
944+
function compileToStats(tap) {
945+
return new Promise((resolve, reject) => {
946+
const compiler = webpack(
947+
/** @type {import("../../").Configuration} */ ({
948+
...defaults.options,
949+
entry: "./missingFile"
950+
})
951+
);
952+
compiler.outputFileSystem = defaults.outputFileSystem;
953+
if (tap) {
954+
compiler.hooks.compilation.tap("test", tap);
955+
}
956+
compiler.run((bailedError, stats) => {
957+
if (bailedError) return reject(bailedError);
958+
compiler.close((closeError) => {
959+
if (closeError) return reject(closeError);
960+
resolve(/** @type {import("../../").Stats} */ (stats));
961+
});
962+
});
963+
});
964+
}
965+
966+
it("reports true when the compilation has errors", async () => {
967+
const stats = await compileToStats();
968+
expect(stats.hasErrors()).toBe(true);
969+
});
970+
971+
// `hasErrors()` must go through `getErrors()` (the `processErrors` waterfall
972+
// hook), not read `compilation.errors` directly — otherwise a plugin that
973+
// filters errors out of the hook is contradicted by `hasErrors()`, which is
974+
// exactly the asymmetry `hasWarnings()`/`processWarnings` already avoids.
975+
it("respects a processErrors hook that filters errors out", async () => {
976+
const stats = await compileToStats((compilation) => {
977+
compilation.hooks.processErrors.tap("test", () => []);
978+
});
979+
expect(stats.toJson({ errorDetails: false }).errors).toHaveLength(0);
980+
expect(stats.hasErrors()).toBe(false);
981+
});
982+
});

0 commit comments

Comments
 (0)