Skip to content

Commit e8ed172

Browse files
author
Thatgfsj
committed
fix(plugin): skip mismatched kind in readV1Plugin detect mode
When the server plugin loader scans modules with mode="detect", a plugin that exports only `tui` (and no `server`) currently falls through to the kind-specific check and throws a misleading "must default export an object with server()" error. The error is caught and logged, polluting startup logs for legitimate TUI-only plugins. Mirror the existing all-fields-missing guard with per-kind short-circuits so detect mode silently skips plugins that don't expose the requested kind's entrypoint. Adds a regression test covering TUI-only and server-only detection. Fixes anomalyco#31610
1 parent ff0382e commit e8ed172

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

‎packages/opencode/src/plugin/shared.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ export function readV1Plugin(
281281
throw new TypeError(`Plugin ${spec} must default export an object with ${kind}()`)
282282
}
283283
if (mode === "detect" && !("id" in value) && !("server" in value) && !("tui" in value)) return
284+
if (mode === "detect" && kind === "server" && !("server" in value)) return
285+
if (mode === "detect" && kind === "tui" && !("tui" in value)) return
284286

285287
const server = "server" in value ? value.server : undefined
286288
const tui = "tui" in value ? value.tui : undefined
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { readV1Plugin } from "../../src/plugin/shared"
3+
4+
// Regression coverage for #31610:
5+
// readV1Plugin in "detect" mode must not throw when the plugin module
6+
// exports a default object that does not include the requested kind's
7+
// entrypoint (e.g. a TUI-only plugin scanned by the server loader).
8+
9+
describe("plugin.shared.readV1Plugin detect mode", () => {
10+
test("returns undefined when a TUI-only plugin is scanned for kind=server", () => {
11+
// Given a plugin that only exports `tui` (no `server`)
12+
const mod = {
13+
default: {
14+
id: "tui-only",
15+
tui: () => ({}) as unknown,
16+
},
17+
}
18+
19+
// When the server loader asks to detect whether this is a server plugin
20+
const result = readV1Plugin(
21+
mod as unknown as Record<string, unknown>,
22+
"tui-only-plugin",
23+
"server",
24+
"detect",
25+
)
26+
27+
// Then it should silently skip instead of throwing
28+
expect(result).toBeUndefined()
29+
})
30+
31+
test("returns undefined when a server-only plugin is scanned for kind=tui", () => {
32+
const mod = {
33+
default: {
34+
id: "server-only",
35+
server: () => ({}) as unknown,
36+
},
37+
}
38+
39+
const result = readV1Plugin(
40+
mod as unknown as Record<string, unknown>,
41+
"server-only-plugin",
42+
"tui",
43+
"detect",
44+
)
45+
46+
expect(result).toBeUndefined()
47+
})
48+
49+
test("returns the value when the requested kind is present", () => {
50+
const server = () => ({ ok: true })
51+
const mod = {
52+
default: {
53+
id: "has-both-but-server",
54+
server,
55+
},
56+
}
57+
58+
const result = readV1Plugin(
59+
mod as unknown as Record<string, unknown>,
60+
"plugin",
61+
"server",
62+
"detect",
63+
)
64+
65+
expect(result).toEqual({ id: "has-both-but-server", server })
66+
})
67+
68+
test("strict mode still throws for TUI-only plugins loaded as server", () => {
69+
// Sanity check: strict mode is unchanged.
70+
const mod = {
71+
default: {
72+
id: "tui-only",
73+
tui: () => ({}) as unknown,
74+
},
75+
}
76+
77+
expect(() =>
78+
readV1Plugin(
79+
mod as unknown as Record<string, unknown>,
80+
"tui-only-plugin",
81+
"server",
82+
"strict",
83+
),
84+
).toThrow(/must default export an object with server/)
85+
})
86+
})

0 commit comments

Comments
 (0)