From be3a2e2eb64f6da5d04de1e8f9ffe667552ed7dc Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 22 Mar 2026 22:24:57 -0700 Subject: [PATCH] fix(plugin-sdk): fall back to src root alias files --- src/plugin-sdk/root-alias.cjs | 4 ++-- src/plugin-sdk/root-alias.test.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/plugin-sdk/root-alias.cjs b/src/plugin-sdk/root-alias.cjs index c4e890eaae1..9c1c1b9ab61 100644 --- a/src/plugin-sdk/root-alias.cjs +++ b/src/plugin-sdk/root-alias.cjs @@ -148,7 +148,7 @@ function loadMonolithicSdk() { } } - monolithicSdk = getJiti(false)(path.join(__dirname, "compat.ts")); + monolithicSdk = getJiti(false)(path.join(getPackageRoot(), "src", "plugin-sdk", "compat.ts")); return monolithicSdk; } @@ -175,7 +175,7 @@ function loadDiagnosticEventsModule() { } diagnosticEventsModule = getJiti(false)( - path.join(__dirname, "..", "infra", "diagnostic-events.ts"), + path.join(getPackageRoot(), "src", "infra", "diagnostic-events.ts"), ); return diagnosticEventsModule; } diff --git a/src/plugin-sdk/root-alias.test.ts b/src/plugin-sdk/root-alias.test.ts index 07c08b0e55a..5705c22e9a3 100644 --- a/src/plugin-sdk/root-alias.test.ts +++ b/src/plugin-sdk/root-alias.test.ts @@ -24,6 +24,7 @@ function loadRootAliasWithStubs(options?: { distExists?: boolean; env?: Record; monolithicExports?: Record; + aliasPath?: string; }) { let createJitiCalls = 0; let jitiLoadCalls = 0; @@ -48,6 +49,7 @@ function loadRootAliasWithStubs(options?: { __dirname: string, ) => void; const module = { exports: {} as Record }; + const aliasPath = options?.aliasPath ?? rootAliasPath; const localRequire = ((id: string) => { if (id === "node:path") { return path; @@ -78,7 +80,7 @@ function loadRootAliasWithStubs(options?: { } throw new Error(`unexpected require: ${id}`); }) as NodeJS.Require; - wrapper(module.exports, localRequire, module, rootAliasPath, path.dirname(rootAliasPath)); + wrapper(module.exports, localRequire, module, aliasPath, path.dirname(aliasPath)); return { moduleExports: module.exports, get createJitiCalls() { @@ -175,6 +177,32 @@ describe("plugin-sdk root alias", () => { expect(lazyModule.createJitiOptions.at(-1)?.tryNative).toBe(false); }); + it("falls back to src files even when the alias itself is loaded from dist", () => { + 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, + monolithicExports: { + onDiagnosticEvent: () => () => undefined, + slowHelper: () => "loaded", + }, + }); + + expect((lazyModule.moduleExports.slowHelper as () => string)()).toBe("loaded"); + expect(lazyModule.loadedSpecifiers).toContain( + path.join(packageRoot, "src", "plugin-sdk", "compat.ts"), + ); + expect( + typeof (lazyModule.moduleExports.onDiagnosticEvent as (listener: () => void) => () => void)( + () => undefined, + ), + ).toBe("function"); + expect(lazyModule.loadedSpecifiers).toContain( + path.join(packageRoot, "src", "infra", "diagnostic-events.ts"), + ); + }); + it("forwards delegateCompactionToRuntime through the compat-backed root alias", () => { const delegateCompactionToRuntime = () => "delegated"; const lazyModule = loadRootAliasWithStubs({