Skip to content

Commit 6b1bc48

Browse files
authored
fix(cli): let plugin add install GUI-only packages (#59492)
1 parent 542e683 commit 6b1bc48

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎packages/cli/src/commands/handlers/plugin/add.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ export default Runtime.handler(
1818
return yield* Effect.fail(new Error("Plugin target must be an npm registry package or Git package specifier"))
1919
const npm = yield* Npm.Service
2020
const installed = yield* npm.add(input.package)
21-
const entrypoints = Host.resolve(installed)
22-
const target = configurationTarget(entrypoints.server, entrypoints.tui)
21+
const target = configurationTarget(Host.resolve(installed))
2322
if (!target)
24-
return yield* Effect.fail(new Error(`Plugin package has no server or TUI entrypoint: ${input.package}`))
23+
return yield* Effect.fail(new Error(`Plugin package has no server, GUI, or TUI entrypoint: ${input.package}`))
2524

2625
if (target === "server") {
2726
const global = yield* Global.Service
@@ -44,9 +43,11 @@ export default Runtime.handler(
4443
}),
4544
)
4645

47-
export function configurationTarget(server?: string, tui?: string) {
48-
if (server) return "server" as const
49-
if (tui) return "tui" as const
46+
// The server lists packages with a server or GUI part, and the TUI loads their TUI parts from that list; the desktop
47+
// app reads GUI parts only from the global opencode.json. Only a TUI-only package belongs in the TUI's own config.
48+
export function configurationTarget(entrypoints: Host.Entrypoints) {
49+
if (entrypoints.server || entrypoints.gui) return "server" as const
50+
if (entrypoints.tui) return "tui" as const
5051
}
5152

5253
export async function writePluginConfig(configPath: string, spec: string) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect, test } from "bun:test"
2+
import { mkdir, writeFile } from "node:fs/promises"
3+
import path from "node:path"
4+
import { Host } from "@opencode/plugin/host"
5+
import { configurationTarget } from "../src/commands/handlers/plugin/add"
6+
import { tmpdir } from "./fixture/tmpdir"
7+
8+
const name = "@fixture/plugin"
9+
const parts = {
10+
server: { ".": "./index.js" },
11+
tui: { "./tui": "./tui.js" },
12+
gui: { "./gui-experimental": "./gui-experimental.cjs" },
13+
}
14+
15+
test.each([
16+
{ parts: ["gui"] as const, target: "server" },
17+
{ parts: ["tui", "gui"] as const, target: "server" },
18+
{ parts: ["server"] as const, target: "server" },
19+
{ parts: ["server", "tui", "gui"] as const, target: "server" },
20+
{ parts: ["tui"] as const, target: "tui" },
21+
{ parts: [] as const, target: undefined },
22+
])("configures a package with $parts parts in the $target config", async (input) => {
23+
await using root = await tmpdir()
24+
const directory = path.join(root.path, "node_modules", name)
25+
const exports = Object.assign({}, ...input.parts.map((part) => parts[part]))
26+
await mkdir(directory, { recursive: true })
27+
await Promise.all([
28+
writeFile(path.join(directory, "package.json"), JSON.stringify({ name, exports })),
29+
...Object.values(exports).map((file) => writeFile(path.join(directory, String(file)), "")),
30+
])
31+
32+
expect(configurationTarget(Host.resolve({ directory, name }))).toBe(input.target)
33+
})

0 commit comments

Comments
 (0)