Skip to content

Commit 9afd535

Browse files
xiaoxiaojxhai-x
andauthored
fix(cache): invalidate consumer codegen when inlined exports change (#22020)
* fix(cache): invalidate consumer codegen when inlined exports change Include inactive-but-inlined import connections in the module graph hash so filesystem cache does not keep stale literals (#22019). * Add test case * update * fix(cache): encode provided-exports value dependencies unambiguously The provided-exports cache key joined "key=value" pairs with "|", so a DefinePlugin version spelling out those separators could forge another key's entry and reuse a prior configuration's inlined literals. Serialize the still-lazy requests and the value versions as JSON instead. Adds two PersistentCaching cases: a DefinePlugin-provided inlined export changing between cached builds, and two version sets that collided under the old encoding. * refactor(cache): fold the value-version shapes into one branch `instanceof Set` separates the two shapes a value dependency can take; an absent version needs no arm of its own, since JSON.stringify already writes it as null inside the array, distinct from a string and an array. --------- Co-authored-by: xiaoxiaojx <[email protected]> Co-authored-by: Hai <[email protected]>
1 parent 3c50c75 commit 9afd535

15 files changed

Lines changed: 295 additions & 26 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+
Invalidate consumer codegen when an inlined export value changes.

‎lib/ChunkGraph.js‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const util = require("util");
99
const Entrypoint = require("./Entrypoint");
1010
const ModuleGraphConnection = require("./ModuleGraphConnection");
1111
const { DEFAULTS } = require("./config/defaults");
12+
const { isExportInlined } = require("./optimize/InlineExports");
1213
const { first } = require("./util/SetHelpers");
1314
const SortableSet = require("./util/SortableSet");
1415
const {
@@ -21,6 +22,7 @@ const {
2122
} = require("./util/comparators");
2223
const createHash = require("./util/createHash");
2324
const findGraphRoots = require("./util/findGraphRoots");
25+
const memoize = require("./util/memoize");
2426
const {
2527
RuntimeSpecMap,
2628
RuntimeSpecSet,
@@ -29,6 +31,10 @@ const {
2931
runtimeToString
3032
} = require("./util/runtime");
3133

34+
const getHarmonyImportSpecifierDependency = memoize(() =>
35+
require("./dependencies/HarmonyImportSpecifierDependency")
36+
);
37+
3238
/** @import AsyncDependenciesBlock from "./AsyncDependenciesBlock" */
3339
/** @import Chunk, { Chunks, Entrypoints, ChunkId } from "./Chunk" */
3440
/** @import ChunkGroup from "./ChunkGroup" */
@@ -69,6 +75,23 @@ const activeStateToString = (state) => {
6975
throw new Error("Not implemented active state");
7076
};
7177

78+
/**
79+
* True when an inactive connection still embeds an inlined export literal
80+
* in the referencing module's generated code (must stay in its hash).
81+
* @param {ModuleGraph} moduleGraph module graph
82+
* @param {ModuleGraphConnection} connection connection
83+
* @param {RuntimeSpec} runtime runtime
84+
* @returns {boolean} true when the connection contributes an inlined literal
85+
*/
86+
const connectionInactiveDueToInlining = (moduleGraph, connection, runtime) => {
87+
const module = connection.module;
88+
if (!module) return false;
89+
const dep = connection.dependency;
90+
// Lazy: top-level require cycles Module → ChunkGraph → dependency → Module.
91+
if (!(dep instanceof getHarmonyImportSpecifierDependency())) return false;
92+
return isExportInlined(moduleGraph, module, dep.getIds(moduleGraph), runtime);
93+
};
94+
7295
const compareModuleIterables = compareIterables(compareModulesByIdentifier);
7396

7497
/** @typedef {(c: Chunk, chunkGraph: ChunkGraph) => boolean} ChunkFilterPredicate */
@@ -1885,7 +1908,20 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
18851908
if (runtime === undefined || typeof runtime === "string") {
18861909
for (const connection of connections) {
18871910
const state = connection.getActiveState(runtime);
1888-
if (state === false) continue;
1911+
if (state === false) {
1912+
// Keep inlined imports in the hash: their literals are
1913+
// embedded in this module's codegen (#22019).
1914+
if (
1915+
connectionInactiveDueToInlining(
1916+
this.moduleGraph,
1917+
connection,
1918+
runtime
1919+
)
1920+
) {
1921+
processConnection(connection, "I");
1922+
}
1923+
continue;
1924+
}
18891925
processConnection(connection, state === true ? "T" : "O");
18901926
}
18911927
} else {
@@ -1905,7 +1941,18 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
19051941
);
19061942
if (states.size === 1) {
19071943
const state = first(states);
1908-
if (state === false) continue;
1944+
if (state === false) {
1945+
if (
1946+
connectionInactiveDueToInlining(
1947+
this.moduleGraph,
1948+
connection,
1949+
runtime
1950+
)
1951+
) {
1952+
processConnection(connection, "I");
1953+
}
1954+
continue;
1955+
}
19091956
stateInfo = activeStateToString(
19101957
/** @type {ConnectionState} */
19111958
(state)

‎lib/FlagDependencyExportsPlugin.js‎

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,40 @@ const PLUGIN_LOGGER_NAME = `webpack.${PLUGIN_NAME}`;
3030
/** @typedef {{ etag: string, data: InstanceType<RestoreProvidedData> }} MemCacheEntry */
3131

3232
/**
33+
* Cache key fragment for what changes provided exports without changing the
34+
* module's source: still-lazy barrel requests and DefinePlugin value versions.
3335
* @param {Module} module the module
3436
* @param {LazyBarrelController} lazyBarrelController the compilation's lazy barrel controller
3537
* @param {HashFunction} hashFunction hash function for long keys
36-
* @returns {string} the hash
38+
* @returns {string} etag for provided-exports mem/persistent cache, "" when neither applies
3739
*/
38-
const getLazyRequestsHash = (module, lazyBarrelController, hashFunction) => {
39-
const lazyKeys = lazyBarrelController.getLazyRequests(module);
40-
if (!lazyKeys) return "";
41-
const joined = [...lazyKeys].join("|");
40+
const getProvidedExportsEtag = (module, lazyBarrelController, hashFunction) => {
41+
const lazyRequests = lazyBarrelController.getLazyRequests(module);
42+
const buildInfo = /** @type {BuildInfo | undefined} */ (module.buildInfo);
43+
const valueDependencies = buildInfo && buildInfo.valueDependencies;
44+
const hasValueDependencies =
45+
valueDependencies !== undefined && valueDependencies.size > 0;
46+
if (!lazyRequests && !hasValueDependencies) return "";
47+
/** @type {(string | string[] | undefined)[][]} */
48+
const valueEntries = [];
49+
if (hasValueDependencies) {
50+
const versions =
51+
/** @type {NonNullable<BuildInfo["valueDependencies"]>} */
52+
(valueDependencies);
53+
for (const key of [...versions.keys()].sort()) {
54+
const value = versions.get(key);
55+
// An absent version serializes as null, distinct from both other shapes
56+
valueEntries.push([
57+
key,
58+
value instanceof Set ? [...value].sort() : value
59+
]);
60+
}
61+
}
62+
// JSON, so a separator inside a request or a version cannot forge another entry
63+
const joined = JSON.stringify([
64+
lazyRequests ? [...lazyRequests] : [],
65+
valueEntries
66+
]);
4267
if (joined.length < 100) return joined;
4368
const hash = createHash(hashFunction);
4469
hash.update(joined);
@@ -107,7 +132,7 @@ class FlagDependencyExportsPlugin {
107132
const memCache = moduleMemCaches && moduleMemCaches.get(module);
108133
/** @type {MemCacheEntry | undefined} */
109134
const memCacheEntry = memCache && memCache.get(this);
110-
const hash = getLazyRequestsHash(
135+
const etag = getProvidedExportsEtag(
111136
module,
112137
lazyBarrelController,
113138
hashFunction
@@ -116,7 +141,7 @@ class FlagDependencyExportsPlugin {
116141
// detection cannot invalidate the mem cache when the un-lazied
117142
// connections are unsafe-cached (they are skipped when comparing
118143
// references)
119-
if (memCacheEntry !== undefined && memCacheEntry.etag === hash) {
144+
if (memCacheEntry !== undefined && memCacheEntry.etag === etag) {
120145
statRestoredFromMemCache++;
121146
exportsInfo.restoreProvided(memCacheEntry.data);
122147
return callback();
@@ -126,7 +151,7 @@ class FlagDependencyExportsPlugin {
126151
);
127152
cache.get(
128153
module.identifier(),
129-
hash ? `${buildHash}|${hash}` : buildHash,
154+
etag ? `${buildHash}|${etag}` : buildHash,
130155
(err, result) => {
131156
if (err) return callback(err);
132157

@@ -349,12 +374,13 @@ class FlagDependencyExportsPlugin {
349374
changed = true;
350375
}
351376

352-
if (
353-
inlined !== undefined &&
354-
exportInfo.canInlineProvide === undefined
355-
) {
356-
exportInfo.canInlineProvide = inlined;
357-
changed = true;
377+
if (inlined !== undefined) {
378+
const prev = exportInfo.canInlineProvide;
379+
// Watch rebuilds keep ExportInfo; refresh when the constant changes
380+
if (prev === undefined || !prev.equals(inlined)) {
381+
exportInfo.canInlineProvide = inlined;
382+
changed = true;
383+
}
358384
}
359385

360386
if (terminalBinding && !exportInfo.terminalBinding) {
@@ -511,15 +537,15 @@ class FlagDependencyExportsPlugin {
511537
.getRestoreProvidedData();
512538
const memCache =
513539
moduleMemCaches && moduleMemCaches.get(module);
514-
const hash = getLazyRequestsHash(
540+
const etag = getProvidedExportsEtag(
515541
module,
516542
lazyBarrelController,
517543
hashFunction
518544
);
519545
if (memCache) {
520546
/** @type {MemCacheEntry} */
521547
const entry = {
522-
etag: hash,
548+
etag,
523549
data: cachedData
524550
};
525551
memCache.set(this, entry);
@@ -529,7 +555,7 @@ class FlagDependencyExportsPlugin {
529555
);
530556
cache.store(
531557
module.identifier(),
532-
hash ? `${buildHash}|${hash}` : buildHash,
558+
etag ? `${buildHash}|${etag}` : buildHash,
533559
cachedData,
534560
callback
535561
);

‎lib/optimize/ConstExportsPlugin.js‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,23 @@ class ConstExportsPlugin {
189189
}
190190
for (const exportInfo of exportsInfo.ownedExports) {
191191
const inlined = exportInfo.canInline();
192-
const doInline =
193-
!exportInfo.hasUsedName() &&
194-
inlined !== undefined &&
195-
exportInfo.provided === true;
196-
if (doInline) {
197-
exportInfo.setUsedName(new InlinedUsedName(inlined));
198-
exportsInfo.markInlinedExports();
192+
if (inlined !== undefined && exportInfo.provided === true) {
193+
if (!exportInfo.hasUsedName()) {
194+
exportInfo.setUsedName(new InlinedUsedName(inlined));
195+
exportsInfo.markInlinedExports();
196+
} else {
197+
// Watch rebuilds keep ExportInfo; refresh a stale literal
198+
const usedName = exportInfo.getUsedName(
199+
exportInfo.name,
200+
undefined
201+
);
202+
if (
203+
usedName instanceof InlinedUsedName &&
204+
!usedName.value.equals(inlined)
205+
) {
206+
exportInfo.setUsedName(new InlinedUsedName(inlined));
207+
}
208+
}
199209
}
200210
if (exportInfo.exportsInfoOwned && exportInfo.exportsInfo) {
201211
const used = exportInfo.getUsed(undefined);

‎lib/optimize/InlineExports.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ class InlinedValue {
6060
return `(${comment}${this.renderLiteral()})`;
6161
}
6262

63+
/**
64+
* @param {InlinedValue} other other inlined value
65+
* @returns {boolean} true when kind and value match
66+
*/
67+
equals(other) {
68+
return this.kind === other.kind && this.value === other.value;
69+
}
70+
6371
/**
6472
* @param {ObjectSerializerContext} context context
6573
*/

‎test/PersistentCaching.test.js‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,116 @@ export default 40 + file;
473473
await compile(configAdditions);
474474
await expect(getCacheFileTimes()).resolves.toEqual(firstCacheFileTimes);
475475
}, 20000);
476+
477+
// An inlined export embeds its literal in the consumer's codegen; the
478+
// filesystem cache must invalidate that consumer when the value changes.
479+
it("should invalidate consumer codegen when an inlined export value changes", async () => {
480+
const configAdditions = {
481+
mode: "production",
482+
optimization: { minimize: false },
483+
output: {
484+
...config.output,
485+
pathinfo: true
486+
}
487+
};
488+
await updateSrc({
489+
"index.js": `import { FLAG, NUM } from "./env.js";
490+
export default [NUM, FLAG];
491+
`,
492+
"env.js": `export const NUM = 5;
493+
export const FLAG = "on";
494+
`
495+
});
496+
await compile(configAdditions);
497+
expect(execute()).toEqual([5, "on"]);
498+
const first = await readFile(path.resolve(outputPath, "main.js"), "utf8");
499+
expect(first).toContain("inlined export .NUM */5");
500+
expect(first).toContain('inlined export .FLAG */"on"');
501+
502+
// Both values stay within the ≤6-byte inline limit so a yes→no
503+
// boundary change cannot mask the stale-hash bug (#22019).
504+
await updateSrc({
505+
"env.js": `export const NUM = 6;
506+
export const FLAG = "off";
507+
`
508+
});
509+
await compile(configAdditions);
510+
expect(execute()).toEqual([6, "off"]);
511+
const second = await readFile(path.resolve(outputPath, "main.js"), "utf8");
512+
expect(second).toContain("inlined export .NUM */6");
513+
expect(second).toContain('inlined export .FLAG */"off"');
514+
}, 100000);
515+
516+
// A DefinePlugin value changes the inlined literal without touching the
517+
// module source, so its build hash alone cannot key the provided exports.
518+
it("should invalidate consumer codegen when a plugin-provided inlined export changes", async () => {
519+
const { DefinePlugin } = require("../");
520+
521+
const configFor = (/** @type {string} */ flag) => ({
522+
mode: "production",
523+
optimization: { minimize: false },
524+
output: {
525+
...config.output,
526+
pathinfo: true
527+
},
528+
plugins: [new DefinePlugin({ "process.env.FLAG": JSON.stringify(flag) })]
529+
});
530+
await updateSrc({
531+
"index.js": `import { FLAG } from "./env.js";
532+
export default FLAG;
533+
`,
534+
"env.js": "export const FLAG = process.env.FLAG;\n"
535+
});
536+
await compile(configFor("on"));
537+
expect(execute()).toBe("on");
538+
const first = await readFile(path.resolve(outputPath, "main.js"), "utf8");
539+
expect(first).toContain('inlined export .FLAG */"on"');
540+
541+
await compile(configFor("off"));
542+
expect(execute()).toBe("off");
543+
const second = await readFile(path.resolve(outputPath, "main.js"), "utf8");
544+
expect(second).toContain('inlined export .FLAG */"off"');
545+
}, 100000);
546+
547+
// A value version is user-supplied text; spelling out the encoding's own
548+
// separators must not forge another key's entry in the cache key.
549+
it("should key value dependencies whose versions contain the separators", async () => {
550+
const { DefinePlugin } = require("../");
551+
552+
const keyB = `${DefinePlugin.VALUE_DEP_PREFIX}process.env.B`;
553+
const configFor = (
554+
/** @type {string} */ suffix,
555+
/** @type {string} */ versionA,
556+
/** @type {string} */ versionB
557+
) => ({
558+
mode: "production",
559+
optimization: { minimize: false },
560+
plugins: [
561+
new DefinePlugin({
562+
"process.env.A": DefinePlugin.runtimeValue(
563+
() => JSON.stringify(`a${suffix}`),
564+
{ version: versionA }
565+
),
566+
"process.env.B": DefinePlugin.runtimeValue(
567+
() => JSON.stringify(`b${suffix}`),
568+
{ version: versionB }
569+
)
570+
})
571+
]
572+
});
573+
await updateSrc({
574+
"index.js": `import { A, B } from "./env.js";
575+
export default [A, B];
576+
`,
577+
"env.js": `export const A = process.env.A;
578+
export const B = process.env.B;
579+
`
580+
});
581+
await compile(configFor("1", `1|${keyB}=2`, "3"));
582+
expect(execute()).toEqual(["a1", "b1"]);
583+
584+
// The two version sets concatenate to the same "key=value|…" text
585+
await compile(configFor("2", "1", `2|${keyB}=3`));
586+
expect(execute()).toEqual(["a2", "b2"]);
587+
}, 100000);
476588
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const FLAG = process.env.FLAG;
2+
export const NUM = 5;

0 commit comments

Comments
 (0)