From e8887fc67ad24b6813c93087aab30f378a5f3dc5 Mon Sep 17 00:00:00 2001 From: Phon Date: Mon, 16 Nov 2026 00:40:31 +0530 Subject: [PATCH] fix(core): reject plugin effects that return a non-Effect --- packages/core/src/plugin.ts | 18 ++++++++- .../core/test/plugin/effect-return.test.ts | 37 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 packages/core/test/plugin/effect-return.test.ts diff --git a/packages/core/src/plugin.ts b/packages/core/src/plugin.ts index d6b11356ed18..1e1cd8e7f555 100644 --- a/packages/core/src/plugin.ts +++ b/packages/core/src/plugin.ts @@ -77,7 +77,23 @@ const layer = Layer.effect( }) }) const host = yield* PluginHost.make({ list }, plugin.id).pipe(Effect.provideContext(hostContext)) - const exit = yield* Effect.suspend(() => plugin.effect(host)).pipe( + // A plugin is user code and is never type-checked, so an `async` effect (or one + // returning a hook map) passes the loader's "is a function" check and arrives here + // as a Promise instead of an Effect. Running it would hang activation forever with + // no error, so reject the non-Effect return and mark the plugin failed instead. + const exit = yield* Effect.suspend((): Effect.Effect => { + const effect = plugin.effect(host) + const result: unknown = effect + if (Effect.isEffect(result)) return effect + const received = + result instanceof Promise ? "a Promise" : result === null ? "null" : typeof result + return Effect.fail( + new Error( + `Plugin "${plugin.id}" effect must return an Effect, but returned ${received}. ` + + `Return an Effect from "effect", or use "setup" for an async function.`, + ), + ) + }).pipe( grouped, inherit, Effect.updateContext((context: Context.Context) => diff --git a/packages/core/test/plugin/effect-return.test.ts b/packages/core/test/plugin/effect-return.test.ts new file mode 100644 index 000000000000..a3bcb37e3b29 --- /dev/null +++ b/packages/core/test/plugin/effect-return.test.ts @@ -0,0 +1,37 @@ +import { expect } from "bun:test" +import { Plugin } from "@opencode/core/plugin" +import { Effect } from "effect" +import { testEffect } from "../lib/effect" +import { PluginTestLayer } from "./fixture" + +const it = testEffect(PluginTestLayer) + +it.live("fails a plugin whose effect returns a Promise instead of hanging activation", () => + Effect.gen(function* () { + const plugins = yield* Plugin.Service + // User plugin code is never type-checked: an async `effect` passes the loader's + // "is a function" check, so simulate it with a cast instead of `define`. + const generations = [ + { + id: "hang", + revision: "test", + effect: (async () => ({})) as unknown as Plugin.Generation["effect"], + }, + { + id: "hang-hooks", + revision: "test", + effect: (async () => ({ "tool.execute.before": async () => {} })) as unknown as Plugin.Generation["effect"], + }, + ] satisfies Plugin.Generation[] + // Activation must finish promptly; without the fix it never returns. + yield* plugins.activate(generations).pipe(Effect.timeout("5 seconds")) + const list = yield* plugins.list() + for (const id of ["hang", "hang-hooks"]) { + const entry = list.find((item) => item.id === Plugin.ID.make(id)) + expect(entry?.state.status).toBe("failed") + expect(entry?.state).toMatchObject({ error: expect.stringContaining("must return an Effect") }) + } + // Readiness settles too, so the provider catalog stays available. + yield* plugins.awaitActivation.pipe(Effect.timeout("5 seconds")) + }), +)