Skip to content

Commit 3601e5a

Browse files
committed
fix: guard stale fact-cache repopulation after invalidation and restore Rspack sync fast path
- module-facts: add a generation counter to the fact cache so that factsForFile does not write back a parse that started before an invalidateFactCache call, preventing stale stableResult from persisting across HMR edits - rspack loader: defer this.async() until the module actually needs async work (fact candidates or server import requests), restoring the synchronous fast path for the common case and matching the Vite plugin's existing behavior
1 parent ff72377 commit 3601e5a

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

‎packages/octane/src/compiler/module-facts.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,12 @@ export function createFactResolver(io) {
385385
async function factsForFile(path) {
386386
let cached = cache.files.get(path);
387387
if (cached !== undefined) return cached;
388+
const gen = cache.generation;
388389
const source = await io.readFile(path);
389390
cached = source === null ? new Map() : extractModuleFacts(source, path);
390-
cache.files.set(path, cached);
391+
if (cache.generation === gen) {
392+
cache.files.set(path, cached);
393+
}
391394
return cached;
392395
}
393396

@@ -458,7 +461,7 @@ export function createFactResolver(io) {
458461
* @returns {Promise<((request: string, imported: string) => any) | null>}
459462
*/
460463
export function createFactCache() {
461-
return { files: new Map(), resolved: new Map() };
464+
return { files: new Map(), resolved: new Map(), generation: 0 };
462465
}
463466

464467
/**
@@ -472,6 +475,7 @@ export function createFactCache() {
472475
export function invalidateFactCache(cache, path) {
473476
cache.files.delete(path);
474477
cache.resolved.clear();
478+
cache.generation++;
475479
}
476480

477481
export async function resolveHookStability(candidates, id, io) {

‎packages/rspack-plugin-octane/src/loader.js‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,30 +232,28 @@ export default function octaneLoader(source, inputSourceMap) {
232232
environment === 'server' && typeof compiler.clientReferenceForFile === 'function'
233233
? compiler.clientReferenceForFile(id)
234234
: null;
235-
const asyncCallback = this.async?.() ?? callback;
236-
const fail = (error) =>
237-
asyncCallback(error instanceof Error ? error : new Error(String(error)));
238235
// Hook facts apply to both environments, so they resolve first and are
239236
// threaded through whichever path this module takes.
240237
// Opt-in — see the measurement note in octane/compiler/vite.js.
241238
const factCandidates =
242239
options.crossModuleHookFacts === true && typeof this.getResolve === 'function'
243240
? findFactCandidates(String(source), id)
244241
: [];
242+
const serverRequests =
243+
environment === 'server' && currentReference === null && typeof this.getResolve === 'function'
244+
? compiler.findServerImportRequests(String(source), id)
245+
: [];
246+
const needsAsync = factCandidates.length > 0 || serverRequests.length > 0;
247+
const asyncCallback = needsAsync ? (this.async?.() ?? callback) : callback;
248+
const fail = (error) =>
249+
asyncCallback(error instanceof Error ? error : new Error(String(error)));
245250
const withHookStability = (hookStability) => {
246-
if (
247-
environment === 'server' &&
248-
currentReference === null &&
249-
typeof this.getResolve === 'function'
250-
) {
251-
const requests = compiler.findServerImportRequests(String(source), id);
252-
if (requests.length > 0) {
253-
resolveClientOnlyImports(this, compiler, source, id).then(
254-
(imports) => finish(imports, asyncCallback, hookStability),
255-
fail,
256-
);
257-
return;
258-
}
251+
if (serverRequests.length > 0) {
252+
resolveClientOnlyImports(this, compiler, source, id).then(
253+
(imports) => finish(imports, asyncCallback, hookStability),
254+
fail,
255+
);
256+
return;
259257
}
260258
finish([], asyncCallback, hookStability);
261259
};

0 commit comments

Comments
 (0)