mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(tui): contain plugin dialogs that throw while rendering (#62110)
This commit is contained in:
@@ -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) => (
|
||||
<PluginContextProvider value={context}>{render()}</PluginContextProvider>
|
||||
)
|
||||
const dialogApi = createDialogApi(host.dialog, provide)
|
||||
const dialogApi = createDialogApi(host.dialog, (render) => (
|
||||
<PluginBoundary id={input.id} where="dialog" onCrash={() => host.dialog.clear()}>
|
||||
{provide(render)}
|
||||
</PluginBoundary>
|
||||
))
|
||||
const toastApi: Toast = {
|
||||
show(options) {
|
||||
const toast = {
|
||||
|
||||
@@ -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 (
|
||||
<ErrorBoundary
|
||||
fallback={(error) => {
|
||||
// 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
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -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")
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -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"))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user