fix(plugin): preserve sdk alias fallback for native loads · openclaw/openclaw@baecb6b
vincentkoc
·
2026-05-04
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1037,7 +1037,8 @@ describe("loadOpenClawPlugins", () => {
|
1037 | 1037 | }, |
1038 | 1038 | }); |
1039 | 1039 | |
1040 | | -expect(registry.plugins.find((entry) => entry.id === "discord")?.status).toBe("loaded"); |
| 1040 | +const record = registry.plugins.find((entry) => entry.id === "discord"); |
| 1041 | +expect(record?.status, record?.error).toBe("loaded"); |
1041 | 1042 | }); |
1042 | 1043 | it("registers standalone text transforms", () => { |
1043 | 1044 | useNoBundledPlugins(); |
@@ -6591,7 +6592,7 @@ module.exports = {
|
6591 | 6592 | }), |
6592 | 6593 | ); |
6593 | 6594 | const record = registry.plugins.find((entry) => entry.id === "legacy-root-import"); |
6594 | | -expect(record?.status).toBe("loaded"); |
| 6595 | +expect(record?.status, record?.error).toBe("loaded"); |
6595 | 6596 | }); |
6596 | 6597 | |
6597 | 6598 | it("supports legacy plugins subscribing to diagnostic events from the root sdk", async () => { |
@@ -6639,7 +6640,7 @@ module.exports = {
|
6639 | 6640 | const record = registry.plugins.find( |
6640 | 6641 | (entry) => entry.id === "legacy-root-diagnostic-listener", |
6641 | 6642 | ); |
6642 | | -expect(record?.status).toBe("loaded"); |
| 6643 | +expect(record?.status, record?.error).toBe("loaded"); |
6643 | 6644 | |
6644 | 6645 | emitDiagnosticEvent({ |
6645 | 6646 | type: "model.usage", |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -64,6 +64,19 @@ describe("tryNativeRequireJavaScriptModule", () => {
|
64 | 64 | ); |
65 | 65 | }); |
66 | 66 | |
| 67 | +it("declines missing dependency errors when source-transform fallback is available", () => { |
| 68 | +const dir = makeTempDir(); |
| 69 | +const modulePath = path.join(dir, "plugin.cjs"); |
| 70 | +fs.writeFileSync(modulePath, 'require("openclaw/plugin-sdk");\n', "utf8"); |
| 71 | + |
| 72 | +expect( |
| 73 | +tryNativeRequireJavaScriptModule(modulePath, { |
| 74 | +allowWindows: true, |
| 75 | +fallbackOnMissingDependency: true, |
| 76 | +}), |
| 77 | +).toEqual({ ok: false }); |
| 78 | +}); |
| 79 | + |
67 | 80 | it("propagates real module evaluation errors instead of falling back", () => { |
68 | 81 | const dir = makeTempDir(); |
69 | 82 | const modulePath = path.join(dir, "plugin.cjs"); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -33,7 +33,7 @@ function isSourceTransformFallbackError(error: unknown, modulePath: string): boo
|
33 | 33 | |
34 | 34 | export function tryNativeRequireJavaScriptModule( |
35 | 35 | modulePath: string, |
36 | | -options: { allowWindows?: boolean } = {}, |
| 36 | +options: { allowWindows?: boolean; fallbackOnMissingDependency?: boolean } = {}, |
37 | 37 | ): { ok: true; moduleExport: unknown } | { ok: false } { |
38 | 38 | if (process.platform === "win32" && options.allowWindows !== true) { |
39 | 39 | return { ok: false }; |
@@ -44,7 +44,15 @@ export function tryNativeRequireJavaScriptModule(
|
44 | 44 | try { |
45 | 45 | return { ok: true, moduleExport: nodeRequire(modulePath) }; |
46 | 46 | } catch (error) { |
47 | | -if (!isSourceTransformFallbackError(error, modulePath)) { |
| 47 | +const code = |
| 48 | +error && typeof error === "object" ? (error as { code?: unknown }).code : undefined; |
| 49 | +if ( |
| 50 | +!isSourceTransformFallbackError(error, modulePath) && |
| 51 | +!( |
| 52 | +options.fallbackOnMissingDependency === true && |
| 53 | +(code === "MODULE_NOT_FOUND" || code === "ERR_MODULE_NOT_FOUND") |
| 54 | +) |
| 55 | +) { |
48 | 56 | throw error; |
49 | 57 | } |
50 | 58 | return { ok: false }; |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -406,6 +406,7 @@ describe("getCachedPluginModuleLoader", () => {
|
406 | 406 | // allowWindows must be passed so the native fast path works on Windows too. |
407 | 407 | expect(nativeStub).toHaveBeenCalledWith("/repo/dist/extensions/demo/api.js", { |
408 | 408 | allowWindows: true, |
| 409 | +fallbackOnMissingDependency: true, |
409 | 410 | }); |
410 | 411 | expect(getPluginModuleLoaderStats()).toMatchObject({ |
411 | 412 | calls: 1, |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -282,7 +282,10 @@ function createPluginModuleLoader(params: {
|
282 | 282 | ...rest, |
283 | 283 | ); |
284 | 284 | } |
285 | | -const native = tryNativeRequireJavaScriptModule(target, { allowWindows: true }); |
| 285 | +const native = tryNativeRequireJavaScriptModule(target, { |
| 286 | +allowWindows: true, |
| 287 | +fallbackOnMissingDependency: true, |
| 288 | +}); |
286 | 289 | if (native.ok) { |
287 | 290 | pluginModuleLoaderStats.nativeHits += 1; |
288 | 291 | return native.moduleExport; |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -8,12 +8,7 @@ function writeRuntimeJsonFile(targetPath: string, value: unknown): void {
|
8 | 8 | |
9 | 9 | function writeRuntimeModuleWrapper(sourcePath: string, targetPath: string): void { |
10 | 10 | const relative = `./${path.relative(path.dirname(targetPath), sourcePath).split(path.sep).join("/")}`; |
11 | | -const content = [ |
12 | | -`export * from ${JSON.stringify(relative)};`, |
13 | | -`import * as moduleExports from ${JSON.stringify(relative)};`, |
14 | | -`export default moduleExports.default ?? moduleExports;`, |
15 | | -"", |
16 | | -].join("\n"); |
| 11 | +const content = [`export * from ${JSON.stringify(relative)};`, ""].join("\n"); |
17 | 12 | try { |
18 | 13 | if (fs.readFileSync(targetPath, "utf8") === content) { |
19 | 14 | return; |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。