Skip to content

Commit e07791a

Browse files
authored
fix(cache): count a module's references when restoring its mem cache (#22749)
* fix(cache): count a module's references when restoring its mem cache With `cacheUnaffected`, a module counted as unaffected when every connection it still had pointed where it did, so a connection gone because its target was deleted went unnoticed and the module kept its re-exports. Store how many references were recorded and compare that too. * test: cover a two-level re-export and the filesystem cache
1 parent 04aa03b commit e07791a

13 files changed

Lines changed: 81 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+
Rebuild a module under `cacheUnaffected` when a module it re-exports is deleted.

‎lib/Compilation.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,13 @@ const dependencyReportCacheTokens = new WeakMap();
640640
*/
641641
const modulesWithoutProblems = new WeakMap();
642642

643+
/**
644+
* How many connections each stored reference map holds, so a module whose
645+
* connection is gone, not only one pointing elsewhere, counts as affected.
646+
* @type {WeakMap<WeakReferences, number>}
647+
*/
648+
const referenceCounts = new WeakMap();
649+
643650
/**
644651
* Reduce affect type.
645652
* @param {Readonly<ModuleGraphConnection[]>} connections connections
@@ -3018,13 +3025,16 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
30183025
const computeReferences = (module) => {
30193026
/** @type {WeakReferences | undefined} */
30203027
let references;
3028+
let count = 0;
30213029
for (const connection of moduleGraph.getOutgoingConnections(module)) {
30223030
const d = connection.dependency;
30233031
const m = connection.module;
30243032
if (!d || !m || unsafeCacheDependencies.has(d)) continue;
30253033
if (references === undefined) references = new WeakMap();
30263034
references.set(d, m);
3035+
count++;
30273036
}
3037+
if (references !== undefined) referenceCounts.set(references, count);
30283038
return references;
30293039
};
30303040

@@ -3036,14 +3046,16 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
30363046
*/
30373047
const compareReferences = (module, references) => {
30383048
if (references === undefined) return true;
3049+
let count = 0;
30393050
for (const connection of moduleGraph.getOutgoingConnections(module)) {
30403051
const d = connection.dependency;
30413052
if (!d) continue;
30423053
const entry = references.get(d);
30433054
if (entry === undefined) continue;
30443055
if (entry !== connection.module) return false;
3056+
count++;
30453057
}
3046-
return true;
3058+
return count === referenceCounts.get(references);
30473059
};
30483060

30493061
const modulesWithoutCache = new Set(modules);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./target";
2+
export const own = 1;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const deleted = WATCH_STEP === "1";
2+
3+
for (const [name, load, keys, deletedKeys] of [
4+
["export { a as r } from", () => require("./named"), ["own", "r"], ["own"]],
5+
["export * from", () => require("./star"), ["a", "own"], ["own"]],
6+
[
7+
"export * from a module re-exporting it",
8+
() => require("./top"),
9+
["a", "own", "top"],
10+
["own", "top"]
11+
]
12+
]) {
13+
it(`should follow ${name} of a deleted and restored module`, () => {
14+
if (deleted) {
15+
expect(load).toThrow(/Cannot find module '\.\/target'/);
16+
// the exports defined before the throw stay in the module cache
17+
expect(Object.keys(load()).sort()).toEqual(deletedKeys);
18+
return;
19+
}
20+
expect(Object.keys(load()).sort()).toEqual(keys);
21+
});
22+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { a as r } from "./target";
2+
export const own = 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./target";
2+
export const own = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = "target";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./deep";
2+
export const top = 1;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
const errors = [1, 2, 3].map(() => [
4+
/Module not found/,
5+
/Can't resolve '\.\/target'/
6+
]);
7+
8+
module.exports = [...errors, ...errors];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DELETE

0 commit comments

Comments
 (0)