Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void, Error, Scope.Scope> => {
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<never>) =>
Expand Down
37 changes: 37 additions & 0 deletions packages/core/test/plugin/effect-return.test.ts
Original file line number Diff line number Diff line change
@@ -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"))
}),
)
Loading