Skip to content

Commit 628ea45

Browse files
fix(cache): hash which dependencies are inactive by request
A module's graph hash counted its inactive connections only, so watch kept stale code when one unused dependency went missing as another came back. Hash their requests instead: unlike the target's graph hash, a request does not change when the target's id or export usage does. Co-authored-by: aryanraj45 <[email protected]>
1 parent c47e84f commit 628ea45

28 files changed

Lines changed: 178 additions & 19 deletions
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 in watch when its missing unused dependencies swap.

‎lib/graph/ChunkGraph.js‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const findGraphRoots = require("./findGraphRoots");
3030
/** @import AsyncDependenciesBlock from "./AsyncDependenciesBlock" */
3131
/** @import Chunk, { Chunks, Entrypoints, ChunkId } from "./Chunk" */
3232
/** @import ChunkGroup from "./ChunkGroup" */
33+
/** @import Dependency from "./Dependency" */
3334
/** @import Entrypoint from "./Entrypoint" */
3435
/**
3536
* @import Module, {
@@ -1938,9 +1939,21 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
19381939
/** @type {Map<string, Module | Set<Module>>} */
19391940
const connectedModules = new Map();
19401941
// a dependency whose target is missing has no connection, so without
1941-
// this count it hashes like one whose target is inactive, while templates
1942-
// print a missing-module throw only for the first.
1943-
let inactiveConnections = 0;
1942+
// these it hashes like one whose target is inactive, while templates
1943+
// print a missing-module throw only for the first
1944+
/** @type {string[] | undefined} */
1945+
let inactiveRequests;
1946+
/**
1947+
* Records an inactive connection by its request, as templates print nothing of its target.
1948+
* @param {ModuleGraphConnection} connection inactive connection
1949+
*/
1950+
const addInactive = (connection) => {
1951+
// only connections without an origin module lack a dependency
1952+
const dependency = /** @type {Dependency} */ (connection.dependency);
1953+
(inactiveRequests || (inactiveRequests = [])).push(
1954+
`${dependency.getResourceIdentifier()}`
1955+
);
1956+
};
19441957
/**
19451958
* Process connection.
19461959
* @param {ModuleGraphConnection} connection connection
@@ -1967,7 +1980,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
19671980
for (const connection of connections) {
19681981
const state = connection.getActiveState(runtime);
19691982
if (state === false) {
1970-
inactiveConnections++;
1983+
addInactive(connection);
19711984
continue;
19721985
}
19731986
processConnection(connection, state === true ? "T" : "O");
@@ -1992,7 +2005,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
19922005
}
19932006
if (sameState) {
19942007
if (firstState === false) {
1995-
inactiveConnections++;
2008+
addInactive(connection);
19962009
continue;
19972010
}
19982011
stateInfo = activeStateToString(
@@ -2007,7 +2020,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
20072020
if (
20082021
activeNamespaceModules.size === 0 &&
20092022
connectedModules.size === 0 &&
2010-
inactiveConnections === 0
2023+
inactiveRequests === undefined
20112024
) {
20122025
return graphHash;
20132026
}
@@ -2059,7 +2072,11 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
20592072
addModuleToHash(modules);
20602073
}
20612074
}
2062-
if (inactiveConnections > 0) hash.update(`F${inactiveConnections}`);
2075+
if (inactiveRequests !== undefined) {
2076+
// connections arrive as resolutions finish, so sort like connectedModules
2077+
if (inactiveRequests.length > 1) inactiveRequests.sort();
2078+
for (const request of inactiveRequests) hash.update(request);
2079+
}
20632080
hash.update(graphHash);
20642081
return hash.digest("hex");
20652082
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const x = 1;
2+
export const y = 2;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { m } from "./m";
2+
import * as a from "./a";
3+
4+
it("should keep the hash of a module whose unused import changes usage", () => {
5+
expect(m).toBe(1);
6+
expect(a.x).toBe(1);
7+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { x } from "./a";
2+
3+
export const unused = () => x;
4+
export const m = 1;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { m } from "./m";
2+
import * as a from "./a";
3+
4+
it("should keep the hash of a module whose unused import changes usage", () => {
5+
expect(m).toBe(1);
6+
expect(a.y).toBe(2);
7+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict";
2+
3+
/** @type {string | undefined} */
4+
let previousHash;
5+
6+
/** @type {import("../../../../").Configuration} */
7+
module.exports = {
8+
mode: "development",
9+
module: { rules: [{ test: /a\.js$/, sideEffects: false }] },
10+
optimization: { usedExports: true, innerGraph: true, sideEffects: true },
11+
plugins: [
12+
(compiler) => {
13+
compiler.hooks.compilation.tap(
14+
"InactiveDependencyHash",
15+
(compilation) => {
16+
compilation.hooks.afterHash.tap("InactiveDependencyHash", () => {
17+
for (const module of compilation.modules) {
18+
if (!/m\.js$/.test(module.nameForCondition() || "")) continue;
19+
const { chunkGraph } = compilation;
20+
const [runtime] = chunkGraph.getModuleRuntimes(module);
21+
const hash = chunkGraph.getModuleGraphHash(module, runtime);
22+
// m.js uses nothing of a.js, so which of its exports index.js uses
23+
// must not reach m.js's hash
24+
if (previousHash !== undefined && hash !== previousHash) {
25+
compilation.errors.push(
26+
new compiler.webpack.WebpackError("m.js hash changed")
27+
);
28+
}
29+
previousHash = hash;
30+
}
31+
});
32+
}
33+
);
34+
}
35+
]
36+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
// other.js and swap.js both miss swap-b.js, swap.js also misses swap-d.js
4+
const errors = ["swap-b.js", "swap-b.js", "swap-d.js"].map((request) => [
5+
/Module not found/,
6+
new RegExp(`Can't resolve '\\./${request}'`)
7+
]);
8+
9+
module.exports = [...errors, ...errors];

‎test/watchCases/recover-from-error/restored-unused-dependency/0/index.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,21 @@ it("should follow new URL() of a deleted and restored module", () => {
2323
WATCH_STEP === "1" || WATCH_STEP === "2"
2424
);
2525
});
26+
27+
it("should follow new URL() targets that swap which one is missing", () => {
28+
// separate entries, so concatenation does not put both pairs in one module
29+
expect(require("./swap-entry").a).toBe(1);
30+
expect(require("./swap-unnamed-entry").b).toBe(1);
31+
const source = __non_webpack_require__("fs").readFileSync(__filename, "utf-8");
32+
const missing = (file) =>
33+
source.includes(["Cannot find module", `'./${file}'`].join(" "));
34+
// one target of each pair is missing at every step but 3, so only which
35+
// one tells the steps apart
36+
const swapped = WATCH_STEP === "1" || WATCH_STEP === "2";
37+
const both = WATCH_STEP === "3";
38+
// a and b have module ids (other.js uses them), swap-unnamed's c and d none
39+
expect(missing("swap-a.js")).toBe(swapped);
40+
expect(missing("swap-b.js")).toBe(!swapped && !both);
41+
expect(missing("swap-c.js")).toBe(swapped);
42+
expect(missing("swap-d.js")).toBe(!swapped && !both);
43+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
export default () => import(/* webpackChunkName: "shared" */ "./shared");
2+
// used here, the swap assets get module ids, so they hash by graph hash
3+
globalThis.swaps = [
4+
new URL("./swap-a.js", import.meta.url),
5+
new URL("./swap-b.js", import.meta.url)
6+
];

0 commit comments

Comments
 (0)