Plugins: resolve sdk aliases from the running CLI

This commit is contained in:
scoootscooob
2026-03-24 10:22:08 -07:00
committed by scoootscooob
parent 8633c7fa73
commit f163759167
2 changed files with 48 additions and 6 deletions

View File

@@ -60,6 +60,16 @@ function withCwd<T>(cwd: string, run: () => T): T {
}
}
function withArgv1<T>(argv1: string, run: () => T): T {
const originalArgv = process.argv;
process.argv = [originalArgv[0] ?? "node", argv1, ...originalArgv.slice(2)];
try {
return run();
} finally {
process.argv = originalArgv;
}
}
function createPluginSdkAliasFixture(params?: {
srcFile?: string;
distFile?: string;
@@ -461,6 +471,35 @@ describe("plugin sdk alias helpers", () => {
);
});
it("resolves plugin-sdk aliases for user-installed plugins via the running openclaw argv hint", () => {
const fixture = createPluginSdkAliasFixture({
srcFile: "channel-runtime.ts",
distFile: "channel-runtime.js",
packageExports: {
"./plugin-sdk/channel-runtime": { default: "./dist/plugin-sdk/channel-runtime.js" },
},
});
const sourceRootAlias = path.join(fixture.root, "src", "plugin-sdk", "root-alias.cjs");
fs.writeFileSync(sourceRootAlias, "module.exports = {};\n", "utf-8");
const externalPluginRoot = path.join(makeTempDir(), ".openclaw", "extensions", "demo");
const externalPluginEntry = path.join(externalPluginRoot, "index.ts");
mkdirSafe(externalPluginRoot);
fs.writeFileSync(externalPluginEntry, 'export const plugin = "demo";\n', "utf-8");
const aliases = withArgv1(path.join(fixture.root, "openclaw.mjs"), () =>
withCwd(externalPluginRoot, () =>
withEnv({ NODE_ENV: undefined }, () => buildPluginLoaderAliasMap(externalPluginEntry)),
),
);
expect(fs.realpathSync(aliases["openclaw/plugin-sdk"] ?? "")).toBe(
fs.realpathSync(sourceRootAlias),
);
expect(fs.realpathSync(aliases["openclaw/plugin-sdk/channel-runtime"] ?? "")).toBe(
fs.realpathSync(path.join(fixture.root, "src", "plugin-sdk", "channel-runtime.ts")),
);
});
it("does not resolve plugin-sdk alias files from cwd fallback when package root is not an OpenClaw root", () => {
const fixture = createPluginSdkAliasFixture({
srcFile: "channel-runtime.ts",