Skip to content

Commit 3de8f3a

Browse files
anmol0bTarquinen
andauthored
fix(tui): contain plugin slots and routes that render bare text (#58144)
Co-authored-by: Daniel Smolsky <[email protected]>
1 parent 17b79f4 commit 3de8f3a

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎packages/tui/src/plugin/render.tsx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
children,
23
createComponent,
34
createContext,
45
createMemo,
@@ -40,11 +41,24 @@ function PluginBoundary(props: ParentProps<{ id: string; where: string }>) {
4041
return null
4142
}}
4243
>
43-
{props.children}
44+
<PluginOutput>{props.children}</PluginOutput>
4445
</ErrorBoundary>
4546
)
4647
}
4748

49+
// Bare text is only valid under <text>. Left to the host's insert, it would
50+
// throw outside the boundary and crash the app, so reject it here instead.
51+
function PluginOutput(props: ParentProps) {
52+
const output = children(() => props.children)
53+
const nodes = createMemo(() => {
54+
const resolved = output.toArray()
55+
if (resolved.some((node) => typeof node === "string" || typeof node === "number"))
56+
throw new Error("rendered bare text; wrap it in <text>")
57+
return resolved
58+
})
59+
return <>{nodes()}</>
60+
}
61+
4862
export function PluginRoute(props: { readonly fallback: (id: string, name: string) => JSX.Element }) {
4963
const plugins = usePlugin()
5064
const route = useRoute()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Plugin } from "@opencode/plugin/tui"
2+
3+
// A slot that returns bare text next to a well-formed one (anomalyco/opencode#58112).
4+
export default Plugin.define({
5+
id: "fixture.slot-string",
6+
setup(context) {
7+
context.ui.slot({ append: "home.footer", render: () => "hello from a plugin" })
8+
context.ui.slot({ append: "home.footer", render: () => <text>Plugin still alive</text> })
9+
},
10+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { expect, test } from "bun:test"
2+
import { createAppFixture } from "./fixture/app"
3+
import { copyPlugin } from "./fixture/fixture"
4+
5+
test("a slot that renders bare text crashes only its own contribution", async () => {
6+
await using plugin = await copyPlugin("slot-string")
7+
await using setup = await createAppFixture({ config: { animations: false, plugins: [plugin.directory] } })
8+
await setup.ready
9+
await setup.waitForFrame(
10+
(frame) => frame.includes("Plugin still alive") && frame.includes("fixture.slot-string crashed in slot home.footer"),
11+
)
12+
expect(setup.captureCharFrame()).not.toContain("hello from a plugin")
13+
})

0 commit comments

Comments
 (0)