Skip to content

Commit 409112f

Browse files
fix: keep earlier stats readable after their modules are cleaned up (#22695)
cleanupForCache cached each source type's size but not the total, which stats read without a type. Stats of an earlier build then crashed with "Cannot read properties of undefined (reading 'getSize')" once a later build had rebuilt one of their modules and no longer had it.
1 parent 96116c2 commit 409112f

3 files changed

Lines changed: 54 additions & 0 deletions

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+
Keep stats from an earlier build readable after a later build no longer has one of their modules.

‎lib/module/NormalModule.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,8 @@ class NormalModule extends Module {
955955
for (const type of /** @type {SourceTypes} */ (this._sourceTypes)) {
956956
this.size(type);
957957
}
958+
// the total size, which stats read without a source type
959+
this.size();
958960
}
959961
super.cleanupForCache();
960962
this.parser = undefined;

‎test/unitCases/Watch.unittest.js‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,53 @@ describe("Watch", () => {
116116
}, 50);
117117
});
118118

119+
it("should keep earlier stats readable after their modules are cleaned up", (done) => {
120+
const context = path.join(testDirectory, "js", "Watch", "stats-after-cleanup");
121+
const entry = path.join(context, "index.js");
122+
const changedFile = path.join(context, "a.js");
123+
fs.rmSync(context, { recursive: true, force: true });
124+
fs.mkdirSync(context, { recursive: true });
125+
fs.writeFileSync(entry, 'require("./a");');
126+
fs.writeFileSync(changedFile, "module.exports = 1;");
127+
const compiler = webpack({
128+
mode: "development",
129+
context,
130+
entry: "./index.js",
131+
cache: { type: "memory" }
132+
});
133+
compiler.outputFileSystem = /** @type {import("../../").OutputFileSystem} */ (
134+
/** @type {unknown} */ (createFsFromVolume(new Volume()))
135+
);
136+
const later = new Date(Date.now() + 10000);
137+
/** @type {import("../../").Stats[]} */
138+
const builds = [];
139+
const run = () =>
140+
compiler.run((err, stats) => {
141+
if (err) return done(err);
142+
builds.push(/** @type {import("../../").Stats} */ (stats));
143+
if (builds.length === 1) {
144+
// the second build rebuilds the module instance the first stats hold
145+
fs.writeFileSync(changedFile, "module.exports = 2;");
146+
fs.utimesSync(changedFile, later, later);
147+
} else if (builds.length === 2) {
148+
// the third build no longer has the module, so it stays cleaned up
149+
fs.writeFileSync(entry, "");
150+
fs.utimesSync(entry, later, later);
151+
}
152+
if (builds.length < 3) return run();
153+
compiler.close((closeErr) => {
154+
if (closeErr) return done(closeErr);
155+
try {
156+
builds[0].toString({ all: true });
157+
} catch (error) {
158+
return done(error);
159+
}
160+
done();
161+
});
162+
});
163+
run();
164+
});
165+
119166
it("should rebuild when a file is removed while a build is running", (done) => {
120167
const context = path.join(testDirectory, "js", "Watch", "removed-while-building");
121168
const removedFile = path.join(context, "b.js");

0 commit comments

Comments
 (0)