diff --git a/src/plugin-sdk/root-alias.cjs b/src/plugin-sdk/root-alias.cjs index 9c1c1b9ab61..09f4453f70c 100644 --- a/src/plugin-sdk/root-alias.cjs +++ b/src/plugin-sdk/root-alias.cjs @@ -76,6 +76,20 @@ function getPackageRoot() { return path.resolve(__dirname, "..", ".."); } +function findDistChunkByPrefix(prefix) { + const distRoot = path.join(getPackageRoot(), "dist"); + try { + const entries = fs.readdirSync(distRoot, { withFileTypes: true }); + const match = entries.find( + (entry) => + entry.isFile() && entry.name.startsWith(`${prefix}-`) && entry.name.endsWith(".js"), + ); + return match ? path.join(distRoot, match.name) : null; + } catch { + return null; + } +} + function listPluginSdkExportedSubpaths() { const packageRoot = getPackageRoot(); if (pluginSdkSubpathsCache.has(packageRoot)) { @@ -157,7 +171,7 @@ function loadDiagnosticEventsModule() { return diagnosticEventsModule; } - const distCandidate = path.resolve( + const directDistCandidate = path.resolve( __dirname, "..", "..", @@ -165,12 +179,17 @@ function loadDiagnosticEventsModule() { "infra", "diagnostic-events.js", ); - if (!shouldPreferSourceInTests && fs.existsSync(distCandidate)) { - try { - diagnosticEventsModule = getJiti(true)(distCandidate); - return diagnosticEventsModule; - } catch { - // Fall through to source path if dist is unavailable or stale. + if (!shouldPreferSourceInTests) { + const distCandidate = + (fs.existsSync(directDistCandidate) && directDistCandidate) || + findDistChunkByPrefix("diagnostic-events"); + if (distCandidate) { + try { + diagnosticEventsModule = getJiti(true)(distCandidate); + return diagnosticEventsModule; + } catch { + // Fall through to source path if dist is unavailable or stale. + } } } diff --git a/src/plugin-sdk/root-alias.test.ts b/src/plugin-sdk/root-alias.test.ts index 5705c22e9a3..cd26489740a 100644 --- a/src/plugin-sdk/root-alias.test.ts +++ b/src/plugin-sdk/root-alias.test.ts @@ -22,6 +22,7 @@ type EmptySchema = { function loadRootAliasWithStubs(options?: { distExists?: boolean; + distEntries?: string[]; env?: Record; monolithicExports?: Record; aliasPath?: string; @@ -62,7 +63,18 @@ function loadRootAliasWithStubs(options?: { "./plugin-sdk/group-access": { default: "./dist/plugin-sdk/group-access.js" }, }, }), - existsSync: () => options?.distExists ?? false, + existsSync: (targetPath: string) => { + if (targetPath.endsWith(path.join("dist", "infra", "diagnostic-events.js"))) { + return options?.distExists ?? false; + } + return options?.distExists ?? false; + }, + readdirSync: () => + (options?.distEntries ?? []).map((name) => ({ + name, + isFile: () => true, + isDirectory: () => false, + })), }; } if (id === "jiti") { @@ -203,6 +215,32 @@ describe("plugin-sdk root alias", () => { ); }); + it("prefers hashed dist diagnostic events chunks before falling back to src", () => { + const packageRoot = path.dirname(path.dirname(rootAliasPath)); + const distAliasPath = path.join(packageRoot, "dist", "plugin-sdk", "root-alias.cjs"); + const lazyModule = loadRootAliasWithStubs({ + aliasPath: distAliasPath, + distExists: false, + distEntries: ["diagnostic-events-W3Hz61fI.js"], + monolithicExports: { + onDiagnosticEvent: () => () => undefined, + slowHelper: () => "loaded", + }, + }); + + expect( + typeof (lazyModule.moduleExports.onDiagnosticEvent as (listener: () => void) => () => void)( + () => undefined, + ), + ).toBe("function"); + expect(lazyModule.loadedSpecifiers).toContain( + path.join(packageRoot, "dist", "diagnostic-events-W3Hz61fI.js"), + ); + expect(lazyModule.loadedSpecifiers).not.toContain( + path.join(packageRoot, "src", "infra", "diagnostic-events.ts"), + ); + }); + it("forwards delegateCompactionToRuntime through the compat-backed root alias", () => { const delegateCompactionToRuntime = () => "delegated"; const lazyModule = loadRootAliasWithStubs({