Skip to content

Commit a8279a4

Browse files
refactor(exports): add internal ExportsInfo reset APIs for incremental rebuilds (#21553)
* refactor(exports): add internal ExportsInfo reset APIs for incremental rebuilds Reset provide-side and use-side export info to the undetermined state so FlagDependencyExports/FlagDependencyUsage can recompute them against a persisted module graph. No behavior change. * refactor(exports): recurse use-info reset into nested and redirected exports Mirror setHasUseInfo: _resetUseInfo recurses into an owned nested exportsInfo and _resetUsedExports follows the redirect target, so no stale usage survives a use-only reset. * refactor(exports): keep nested exports info on provide reset; drop stale inlined flag Reset an owned nested exportsInfo in place instead of discarding it (its use state must survive a provide-only reset), and clear the _hasInlinedExports fast-path flag when use info is reset. * refactor(exports): recurse setHasProvideInfo into owned nested exports info After a provide reset the nested exports are back to undefined, and FlagDependencyExports only upgrades provided from false/null, so the module-level re-init must reach nested exports (mirrors setHasUseInfo). * refactor(exports): never follow the redirect target in the use reset The redirect points into another module's exports info; the global incremental reset loop already resets it via its own module, so following it wiped foreign state (and double-reset shared targets). * test(exports): assert runtime-usage, target-memo and deep-nested resets * test(exports): fix type errors in reset assertions Assert use-state resets through the public getUsed/getUsedName API instead of private fields, and cast the setTarget/canInlineProvide fixtures.
1 parent c7142e3 commit a8279a4

2 files changed

Lines changed: 295 additions & 0 deletions

File tree

‎lib/ExportsInfo.js‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,39 @@ class ExportsInfo {
307307
}
308308
}
309309

310+
/**
311+
* Resets provide-side info so FlagDependencyExports can recompute it on an
312+
* incremental rebuild against the persisted module graph (which otherwise
313+
* keeps exports removed by the rebuild, since the plugin only merges).
314+
* @returns {void}
315+
*/
316+
_resetProvidedExports() {
317+
for (const exportInfo of this._exports.values()) {
318+
exportInfo._resetProvideInfo();
319+
}
320+
this._redirectTo = undefined;
321+
this._otherExportsInfo._resetProvideInfo();
322+
}
323+
324+
/**
325+
* Resets the use-side info to the undetermined state so FlagDependencyUsage
326+
* can re-trace usage on an incremental rebuild. The persisted graph keeps a
327+
* module marked "used", which stops the trace re-queuing it, so exports newly
328+
* used by a changed importer would never be flagged.
329+
* @returns {void}
330+
*/
331+
_resetUsedExports() {
332+
for (const exportInfo of this._exports.values()) {
333+
exportInfo._resetUseInfo();
334+
}
335+
this._sideEffectsOnlyInfo._resetUseInfo();
336+
// every InlinedUsedName was just cleared, so drop the fast-path flag
337+
this._hasInlinedExports = false;
338+
// never follow _redirectTo: it points into another module's exports info
339+
// (FlagDependencyExports targets), whose own reset/re-trace owns that state
340+
this._otherExportsInfo._resetUseInfo();
341+
}
342+
310343
setHasUseInfo() {
311344
for (const exportInfo of this._exports.values()) {
312345
exportInfo.setHasUseInfo();
@@ -1263,6 +1296,56 @@ class ExportInfo {
12631296
if (this.canMangleProvide === undefined) {
12641297
this.canMangleProvide = true;
12651298
}
1299+
// mirror setHasUseInfo: after a provide reset the owned nested info is
1300+
// undefined again, and FlagDependencyExports only upgrades from false/null
1301+
if (this.exportsInfoOwned) {
1302+
/** @type {ExportsInfo} */
1303+
(this.exportsInfo).setHasProvideInfo();
1304+
}
1305+
}
1306+
1307+
/**
1308+
* Resets provide-side info to the undetermined state (use info untouched).
1309+
* @returns {void}
1310+
*/
1311+
_resetProvideInfo() {
1312+
this.provided = undefined;
1313+
this.terminalBinding = false;
1314+
this.canMangleProvide = undefined;
1315+
this._inlinedValue = undefined;
1316+
if (this.exportsInfoOwned) {
1317+
// reset the owned nested info in place so its use state survives
1318+
/** @type {ExportsInfo} */
1319+
(this.exportsInfo)._resetProvidedExports();
1320+
} else {
1321+
this.exportsInfo = undefined;
1322+
}
1323+
this._target = undefined;
1324+
// also drop the memoized max target so it cannot serve stale entries
1325+
this._maxTarget = undefined;
1326+
// clear the provide-side pure bit; FlagDependencyExports only ever sets it
1327+
// true, so an export that turned impure would otherwise stay marked pure
1328+
this._inlineFlags &= ~PURE_PROVIDE_FLAG;
1329+
}
1330+
1331+
/**
1332+
* Resets the use-side info to the undetermined state (provide info untouched,
1333+
* incl. the pure-provide bit) so FlagDependencyUsage can re-trace it.
1334+
* @returns {void}
1335+
*/
1336+
_resetUseInfo() {
1337+
this._globalUsed = undefined;
1338+
this._usedInRuntime = undefined;
1339+
this._hasUseInRuntimeInfo = false;
1340+
this.canMangleUse = undefined;
1341+
this._usedName = null;
1342+
this._inlineFlags &= PURE_PROVIDE_FLAG;
1343+
// mirror setHasUseInfo: recurse into an owned nested exportsInfo, where
1344+
// FlagDependencyUsage also writes usage
1345+
if (this.exportsInfoOwned) {
1346+
/** @type {ExportsInfo} */
1347+
(this.exportsInfo)._resetUsedExports();
1348+
}
12661349
}
12671350

12681351
setHasUseInfo() {

‎test/ExportsInfo.unittest.js‎

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
"use strict";
2+
3+
const ExportsInfo = require("../lib/ExportsInfo");
4+
5+
const { ExportInfo, UsageState } = ExportsInfo;
6+
7+
describe("ExportInfo", () => {
8+
describe("_resetProvideInfo", () => {
9+
it("clears the pure-provide bit so a rebuilt impure export is not left marked pure", () => {
10+
const info = new ExportInfo("foo");
11+
info.pureProvide = true;
12+
expect(info.pureProvide).toBe(true);
13+
14+
info._resetProvideInfo();
15+
16+
// FlagDependencyExports only ever sets pureProvide true, so without the
17+
// reset an export that turned impure would incorrectly stay pure
18+
expect(info.pureProvide).toBeUndefined();
19+
});
20+
21+
it("keeps an owned nested exportsInfo and resets it in place", () => {
22+
const info = new ExportInfo("foo");
23+
const nested = info.createNestedExportsInfo();
24+
const nestedExport = nested.getExportInfo("bar");
25+
nestedExport.provided = true;
26+
nestedExport.canMangleUse = false;
27+
28+
info._resetProvideInfo();
29+
30+
const kept =
31+
/** @type {import("../lib/ExportsInfo")} */
32+
(info.getNestedExportsInfo());
33+
expect(kept).toBe(nested);
34+
// provide side reset in place, use side untouched
35+
expect(kept.getExportInfo("bar").provided).toBeUndefined();
36+
expect(kept.getExportInfo("bar").canMangleUse).toBe(false);
37+
});
38+
39+
it("resets provide-side fields to the undetermined state", () => {
40+
const info = new ExportInfo("foo");
41+
info.provided = true;
42+
info.terminalBinding = true;
43+
info.canMangleProvide = false;
44+
45+
info._resetProvideInfo();
46+
47+
expect(info.provided).toBeUndefined();
48+
expect(info.terminalBinding).toBe(false);
49+
expect(info.canMangleProvide).toBeUndefined();
50+
});
51+
52+
it("clears the target map, the max-target memo and canInlineProvide", () => {
53+
const info = new ExportInfo("foo");
54+
info.setTarget(
55+
/** @type {EXPECTED_ANY} */ ({}),
56+
/** @type {EXPECTED_ANY} */ ({}),
57+
["a"],
58+
1
59+
);
60+
// prime the memo so a stale entry would be observable
61+
info._getMaxTarget();
62+
info.canInlineProvide = /** @type {EXPECTED_ANY} */ (true);
63+
64+
info._resetProvideInfo();
65+
66+
expect(info._target).toBeUndefined();
67+
expect(info._maxTarget).toBeUndefined();
68+
expect(info.canInlineProvide).toBeUndefined();
69+
});
70+
71+
it("resets two levels of owned nested exports info in place", () => {
72+
const info = new ExportInfo("foo");
73+
const nested = info.createNestedExportsInfo();
74+
const deep = nested.getExportInfo("bar").createNestedExportsInfo();
75+
deep.getExportInfo("baz").provided = true;
76+
77+
info._resetProvideInfo();
78+
79+
expect(nested.getExportInfo("bar").getNestedExportsInfo()).toBe(deep);
80+
expect(deep.getExportInfo("baz").provided).toBeUndefined();
81+
});
82+
});
83+
84+
describe("_resetUseInfo", () => {
85+
it("preserves the pure-provide bit (provide info is untouched)", () => {
86+
const info = new ExportInfo("foo");
87+
info.pureProvide = true;
88+
89+
info._resetUseInfo();
90+
91+
expect(info.pureProvide).toBe(true);
92+
});
93+
94+
it("resets use-side fields but keeps provide info", () => {
95+
const info = new ExportInfo("foo");
96+
info.provided = true;
97+
info.canMangleUse = false;
98+
99+
info._resetUseInfo();
100+
101+
expect(info.canMangleUse).toBeUndefined();
102+
expect(info.provided).toBe(true);
103+
});
104+
105+
it("recurses into an owned nested exportsInfo", () => {
106+
const info = new ExportInfo("foo");
107+
const nested = info.createNestedExportsInfo();
108+
const nestedExport = nested.getExportInfo("bar");
109+
nestedExport.canMangleUse = false;
110+
111+
info._resetUseInfo();
112+
113+
expect(nested.getExportInfo("bar").canMangleUse).toBeUndefined();
114+
});
115+
116+
it("resets global and per-runtime usage state and the used name", () => {
117+
const info = new ExportInfo("foo");
118+
info.setHasUseInfo();
119+
info.setUsed(UsageState.Used, undefined);
120+
info.setUsed(UsageState.Used, "main");
121+
info.setUsedName("a");
122+
123+
info._resetUseInfo();
124+
125+
// no use info at all until the plugin re-initializes
126+
expect(info.getUsed("main")).toBe(UsageState.NoInfo);
127+
// after re-init the cleared state reads as unused in every runtime
128+
info.setHasUseInfo();
129+
expect(info.getUsed(undefined)).toBe(UsageState.Unused);
130+
expect(info.getUsed("main")).toBe(UsageState.Unused);
131+
// and a fresh use resolves to the export's own name, not the stale "a"
132+
info.setUsed(UsageState.Used, "main");
133+
expect(info.getUsedName(undefined, "main")).toBe("foo");
134+
});
135+
136+
it("recurses through two levels of owned nested exports info", () => {
137+
const info = new ExportInfo("foo");
138+
const nested = info.createNestedExportsInfo();
139+
const deep = nested.getExportInfo("bar").createNestedExportsInfo();
140+
deep.getExportInfo("baz").canMangleUse = false;
141+
142+
info._resetUseInfo();
143+
144+
expect(deep.getExportInfo("baz").canMangleUse).toBeUndefined();
145+
});
146+
});
147+
148+
describe("setHasProvideInfo", () => {
149+
it("recurses into an owned nested exportsInfo so a provide reset can be re-merged", () => {
150+
const info = new ExportInfo("foo");
151+
const nested = info.createNestedExportsInfo();
152+
nested.getExportInfo("bar").provided = true;
153+
154+
info._resetProvideInfo();
155+
info.setHasProvideInfo();
156+
157+
// FlagDependencyExports only upgrades provided from false/null, never
158+
// from undefined, so the re-init must reach nested exports too
159+
expect(nested.getExportInfo("bar").provided).toBe(false);
160+
expect(nested.getExportInfo("bar").canMangleProvide).toBe(true);
161+
});
162+
});
163+
});
164+
165+
describe("ExportsInfo", () => {
166+
it("_resetProvidedExports clears the pure-provide bit on every export", () => {
167+
const exportsInfo = new ExportsInfo();
168+
const info = exportsInfo.getExportInfo("foo");
169+
info.pureProvide = true;
170+
exportsInfo.otherExportsInfo.pureProvide = true;
171+
172+
exportsInfo._resetProvidedExports();
173+
174+
expect(exportsInfo.getExportInfo("foo").pureProvide).toBeUndefined();
175+
expect(exportsInfo.otherExportsInfo.pureProvide).toBeUndefined();
176+
});
177+
178+
it("_resetUsedExports resets use info on every export but keeps provide info", () => {
179+
const exportsInfo = new ExportsInfo();
180+
const info = exportsInfo.getExportInfo("foo");
181+
info.provided = true;
182+
info.canMangleUse = false;
183+
184+
exportsInfo._resetUsedExports();
185+
186+
expect(exportsInfo.getExportInfo("foo").canMangleUse).toBeUndefined();
187+
expect(exportsInfo.getExportInfo("foo").provided).toBe(true);
188+
});
189+
190+
it("_resetUsedExports drops the inlined-exports fast-path flag", () => {
191+
const exportsInfo = new ExportsInfo();
192+
exportsInfo.markInlinedExports();
193+
194+
exportsInfo._resetUsedExports();
195+
196+
expect(exportsInfo._hasInlinedExports).toBe(false);
197+
});
198+
199+
it("_resetUsedExports does not wipe a redirect target owned by another module", () => {
200+
const exportsInfo = new ExportsInfo();
201+
const redirectTarget = new ExportsInfo();
202+
exportsInfo.setRedirectNamedTo(redirectTarget);
203+
const info = redirectTarget.getExportInfo("foo");
204+
info.canMangleUse = false;
205+
206+
exportsInfo._resetUsedExports();
207+
208+
// the target belongs to target.module; wiping it would lose usage that
209+
// module's own reset/re-trace is responsible for
210+
expect(redirectTarget.getExportInfo("foo").canMangleUse).toBe(false);
211+
});
212+
});

0 commit comments

Comments
 (0)