From 61e9e86d693e46ecf63841bc5f6163bd009bd6f4 Mon Sep 17 00:00:00 2001 From: YDYK Date: Wed, 22 Apr 2026 17:35:50 +0900 Subject: [PATCH] fix(plugins): use module path for bundled jiti loads --- src/plugins/loader.jiti-filename.test.ts | 99 ++++++++++++++++++++++++ src/plugins/loader.ts | 2 +- 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/plugins/loader.jiti-filename.test.ts diff --git a/src/plugins/loader.jiti-filename.test.ts b/src/plugins/loader.jiti-filename.test.ts new file mode 100644 index 00000000000..9974bc0dce5 --- /dev/null +++ b/src/plugins/loader.jiti-filename.test.ts @@ -0,0 +1,99 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { importFreshModule } from "../../test/helpers/import-fresh.ts"; + +const tempDirs: string[] = []; + +function makeTempDir() { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-plugin-loader-")); + tempDirs.push(dir); + return dir; +} + +function writeBundledPluginFixture(id: string) { + const pluginRoot = makeTempDir(); + fs.writeFileSync( + path.join(pluginRoot, "openclaw.plugin.json"), + JSON.stringify( + { + id, + configSchema: { + type: "object", + additionalProperties: false, + properties: {}, + }, + }, + null, + 2, + ), + "utf-8", + ); + fs.writeFileSync( + path.join(pluginRoot, "index.cjs"), + `module.exports = { id: ${JSON.stringify(id)}, register() {} };`, + "utf-8", + ); + return pluginRoot; +} + +afterEach(() => { + vi.resetModules(); + vi.doUnmock("./jiti-loader-cache.js"); + delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; + for (const dir of tempDirs.splice(0)) { + fs.rmSync(dir, { recursive: true, force: true }); + } +}); + +describe("createPluginJitiLoader", () => { + it("uses the bundled plugin module path as the jiti filename", async () => { + const jitiLoaderCalls: Array<{ modulePath: string; jitiFilename?: string }> = []; + vi.doMock("./jiti-loader-cache.js", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + getCachedPluginJitiLoader: vi.fn((params) => { + jitiLoaderCalls.push({ + modulePath: params.modulePath, + jitiFilename: params.jitiFilename, + }); + return vi.fn(() => ({ + default: { + id: "demo", + register() {}, + }, + })); + }), + }; + }); + + const { loadOpenClawPlugins } = await importFreshModule( + import.meta.url, + "./loader.js?scope=jiti-filename", + ); + + const pluginRoot = writeBundledPluginFixture("demo"); + process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = pluginRoot; + + loadOpenClawPlugins({ + cache: false, + workspaceDir: pluginRoot, + onlyPluginIds: ["demo"], + config: { + plugins: { + entries: { + demo: { + enabled: true, + }, + }, + }, + }, + }); + + const bundledPluginLoad = jitiLoaderCalls.find((call) => call.modulePath.endsWith("index.cjs")); + expect(bundledPluginLoad).toBeDefined(); + expect(bundledPluginLoad?.jitiFilename).toBe(bundledPluginLoad?.modulePath); + }); +}); diff --git a/src/plugins/loader.ts b/src/plugins/loader.ts index 4011cfe7477..5e2ae543624 100644 --- a/src/plugins/loader.ts +++ b/src/plugins/loader.ts @@ -447,7 +447,7 @@ function createPluginJitiLoader(options: Pick