From 745271649d4ee60bbab2f44dcb95cb58d3021e3f Mon Sep 17 00:00:00 2001 From: DS <78942835+Tarquinen@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:11:37 -0500 Subject: [PATCH] fix(tui): contain plugin dialogs that throw while rendering (#62110) --- packages/tui/src/plugin/api.tsx | 7 ++++++- packages/tui/src/plugin/render.tsx | 13 +++++++------ .../tui/test/fixture/plugin/dialog-throw/tui.tsx | 11 +++++++++++ packages/tui/test/plugin-slot-output.test.tsx | 10 ++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 packages/tui/test/fixture/plugin/dialog-throw/tui.tsx diff --git a/packages/tui/src/plugin/api.tsx b/packages/tui/src/plugin/api.tsx index 35a4eac6e56..fd9316d1a45 100644 --- a/packages/tui/src/plugin/api.tsx +++ b/packages/tui/src/plugin/api.tsx @@ -25,6 +25,7 @@ import { DialogConfirm } from "../ui/dialog-confirm" import { DialogPrompt } from "../ui/dialog-prompt" import { DialogSelect } from "../ui/dialog-select" import { useDialog } from "../ui/dialog" +import { PluginBoundary } from "./render" import { useToast } from "../ui/toast" import { useAttention } from "../context/attention" import { useStorage } from "../context/storage" @@ -104,7 +105,11 @@ export function createPluginContext(input: { const provide = (render: () => JSX.Element) => ( {render()} ) - const dialogApi = createDialogApi(host.dialog, provide) + const dialogApi = createDialogApi(host.dialog, (render) => ( + host.dialog.clear()}> + {provide(render)} + + )) const toastApi: Toast = { show(options) { const toast = { diff --git a/packages/tui/src/plugin/render.tsx b/packages/tui/src/plugin/render.tsx index e7e93ee032c..43d7965139f 100644 --- a/packages/tui/src/plugin/render.tsx +++ b/packages/tui/src/plugin/render.tsx @@ -22,22 +22,23 @@ import { useToast } from "../ui/toast" import { errorMessage } from "../util/error" import { usePlugin } from "./context" -// Contain render-time plugin crashes: a throwing slot or route must not take -// down the app or the other plugins. The crash surfaces as one error toast. -function PluginBoundary(props: ParentProps<{ id: string; where: string }>) { +// Contain render-time plugin crashes: a throwing slot, route or dialog must not +// take down the app or the other plugins. The crash surfaces as one error toast. +export function PluginBoundary(props: ParentProps<{ id: string; where: string; onCrash?: () => void }>) { const toast = useToast() return ( { // One toast per crash: onMount is untracked, so prop updates while // the boundary is latched cannot re-toast. - onMount(() => + onMount(() => { toast.show({ variant: "error", title: "Plugin", message: `${props.id} crashed in ${props.where}: ${errorMessage(error)}`, - }), - ) + }) + props.onCrash?.() + }) return null }} > diff --git a/packages/tui/test/fixture/plugin/dialog-throw/tui.tsx b/packages/tui/test/fixture/plugin/dialog-throw/tui.tsx new file mode 100644 index 00000000000..3e45b5629c0 --- /dev/null +++ b/packages/tui/test/fixture/plugin/dialog-throw/tui.tsx @@ -0,0 +1,11 @@ +import { Plugin } from "@opencode/plugin/tui" + +// A dialog that throws while rendering (anomalyco/opencode#61101). +export default Plugin.define({ + id: "fixture.dialog-throw", + setup(context) { + context.ui.dialog.show(() => { + throw new Error("dialog exploded") + }) + }, +}) diff --git a/packages/tui/test/plugin-slot-output.test.tsx b/packages/tui/test/plugin-slot-output.test.tsx index 7aa20739ec9..ea2f8ec5021 100644 --- a/packages/tui/test/plugin-slot-output.test.tsx +++ b/packages/tui/test/plugin-slot-output.test.tsx @@ -11,3 +11,13 @@ test("a slot that renders bare text crashes only its own contribution", async () ) expect(setup.captureCharFrame()).not.toContain("hello from a plugin") }) + +test("a dialog that throws while rendering closes instead of crashing the app", async () => { + await using plugin = await copyPlugin("dialog-throw") + await using setup = await createAppFixture({ config: { animations: false, plugins: [plugin.directory] } }) + await setup.ready + await setup.waitForFrame((frame) => frame.includes("fixture.dialog-throw crashed in dialog")) + // The broken dialog is closed, so focus is back in the prompt. + await setup.mockInput.typeText("still usable") + await setup.waitForFrame((frame) => frame.includes("still usable")) +})