Skip to content

Commit 37f3099

Browse files
vvarprekram1-node
andauthored
fix: show toast error message on ConfigMarkdown parse error (anomalyco#8049)
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
1 parent ebc194c commit 37f3099

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

packages/opencode/src/config/config.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { BunProc } from "@/bun"
1919
import { Installation } from "@/installation"
2020
import { ConfigMarkdown } from "./markdown"
2121
import { existsSync } from "fs"
22+
import { Bus } from "@/bus"
23+
import { Session } from "@/session"
2224

2325
export namespace Config {
2426
const log = Log.create({ service: "config" })
@@ -231,8 +233,15 @@ export namespace Config {
231233
dot: true,
232234
cwd: dir,
233235
})) {
234-
const md = await ConfigMarkdown.parse(item)
235-
if (!md.data) continue
236+
const md = await ConfigMarkdown.parse(item).catch((err) => {
237+
const message = ConfigMarkdown.FrontmatterError.isInstance(err)
238+
? `${err.data.path}: ${err.data.message}`
239+
: `Failed to parse command ${item}`
240+
Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() })
241+
log.error("failed to load command", { command: item, err })
242+
return undefined
243+
})
244+
if (!md) continue
236245

237246
const patterns = ["/.opencode/command/", "/.opencode/commands/", "/command/", "/commands/"]
238247
const file = rel(item, patterns) ?? path.basename(item)
@@ -263,8 +272,15 @@ export namespace Config {
263272
dot: true,
264273
cwd: dir,
265274
})) {
266-
const md = await ConfigMarkdown.parse(item)
267-
if (!md.data) continue
275+
const md = await ConfigMarkdown.parse(item).catch((err) => {
276+
const message = ConfigMarkdown.FrontmatterError.isInstance(err)
277+
? `${err.data.path}: ${err.data.message}`
278+
: `Failed to parse agent ${item}`
279+
Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() })
280+
log.error("failed to load agent", { agent: item, err })
281+
return undefined
282+
})
283+
if (!md) continue
268284

269285
const patterns = ["/.opencode/agent/", "/.opencode/agents/", "/agent/", "/agents/"]
270286
const file = rel(item, patterns) ?? path.basename(item)
@@ -294,8 +310,15 @@ export namespace Config {
294310
dot: true,
295311
cwd: dir,
296312
})) {
297-
const md = await ConfigMarkdown.parse(item)
298-
if (!md.data) continue
313+
const md = await ConfigMarkdown.parse(item).catch((err) => {
314+
const message = ConfigMarkdown.FrontmatterError.isInstance(err)
315+
? `${err.data.path}: ${err.data.message}`
316+
: `Failed to parse mode ${item}`
317+
Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() })
318+
log.error("failed to load mode", { mode: item, err })
319+
return undefined
320+
})
321+
if (!md) continue
299322

300323
const config = {
301324
name: path.basename(item, ".md"),

packages/opencode/src/skill/skill.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import z from "zod"
2+
import path from "path"
23
import { Config } from "../config/config"
34
import { Instance } from "../project/instance"
45
import { NamedError } from "@opencode-ai/util/error"
@@ -7,6 +8,9 @@ import { Log } from "../util/log"
78
import { Global } from "@/global"
89
import { Filesystem } from "@/util/filesystem"
910
import { Flag } from "@/flag/flag"
11+
import { Bus } from "@/bus"
12+
import { TuiEvent } from "@/cli/cmd/tui/event"
13+
import { Session } from "@/session"
1014

1115
export namespace Skill {
1216
const log = Log.create({ service: "skill" })
@@ -42,10 +46,16 @@ export namespace Skill {
4246
const skills: Record<string, Info> = {}
4347

4448
const addSkill = async (match: string) => {
45-
const md = await ConfigMarkdown.parse(match)
46-
if (!md) {
47-
return
48-
}
49+
const md = await ConfigMarkdown.parse(match).catch((err) => {
50+
const message = ConfigMarkdown.FrontmatterError.isInstance(err)
51+
? `${err.data.path}: ${err.data.message}`
52+
: `Failed to parse skill ${match}`
53+
Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() })
54+
log.error("failed to load skill", { skill: match, err })
55+
return undefined
56+
})
57+
58+
if (!md) return
4959

5060
const parsed = Info.pick({ name: true, description: true }).safeParse(md.data)
5161
if (!parsed.success) return

0 commit comments

Comments
 (0)