From 0451fb77de97dbe03a9f5af6fc27158af105b835 Mon Sep 17 00:00:00 2001 From: Ryan Hughes Date: Sun, 11 Jan 2026 17:28:13 -0500 Subject: [PATCH] fix(tui): defer prompt.set() to prevent crash when using --prompt flag Fixes #7818 The parent's onMount runs before the child Prompt component's onMount, so the prompt ref is undefined when prompt.set() is called. Using queueMicrotask() defers execution until after the child's onMount completes and the ref is available. --- .../opencode/src/cli/cmd/tui/routes/home.tsx | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/opencode/src/cli/cmd/tui/routes/home.tsx b/packages/opencode/src/cli/cmd/tui/routes/home.tsx index 2155ab94744..ec6fab18b25 100644 --- a/packages/opencode/src/cli/cmd/tui/routes/home.tsx +++ b/packages/opencode/src/cli/cmd/tui/routes/home.tsx @@ -79,14 +79,18 @@ export function Home() { onMount(() => { randomizeTip() if (once) return - if (route.initialPrompt) { - prompt.set(route.initialPrompt) - once = true - } else if (args.prompt) { - prompt.set({ input: args.prompt, parts: [] }) - once = true - prompt.submit() - } + // Defer to allow child Prompt component's onMount to run first + queueMicrotask(() => { + if (once || !prompt) return + if (route.initialPrompt) { + prompt.set(route.initialPrompt) + once = true + } else if (args.prompt) { + prompt.set({ input: args.prompt, parts: [] }) + once = true + prompt.submit() + } + }) }) const directory = useDirectory()