diff --git a/packages/opencode/src/cli/error.ts b/packages/opencode/src/cli/error.ts index 569b186d55..d7120aa5e9 100644 --- a/packages/opencode/src/cli/error.ts +++ b/packages/opencode/src/cli/error.ts @@ -28,7 +28,7 @@ export function FormatError(input: unknown) { return `Directory "${input.data.dir}" in ${input.data.path} is not valid. Rename the directory to "${input.data.suggestion}" or remove it. This is a common typo.` } if (ConfigMarkdown.FrontmatterError.isInstance(input)) { - return `Failed to parse frontmatter in ${input.data.path}:\n${input.data.message}` + return input.data.message } if (Config.InvalidError.isInstance(input)) return [ diff --git a/packages/opencode/src/config/config.ts b/packages/opencode/src/config/config.ts index 134358ec3b..322ce273ab 100644 --- a/packages/opencode/src/config/config.ts +++ b/packages/opencode/src/config/config.ts @@ -235,7 +235,7 @@ export namespace Config { })) { const md = await ConfigMarkdown.parse(item).catch((err) => { const message = ConfigMarkdown.FrontmatterError.isInstance(err) - ? `${err.data.path}: ${err.data.message}` + ? err.data.message : `Failed to parse command ${item}` Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() }) log.error("failed to load command", { command: item, err }) @@ -274,7 +274,7 @@ export namespace Config { })) { const md = await ConfigMarkdown.parse(item).catch((err) => { const message = ConfigMarkdown.FrontmatterError.isInstance(err) - ? `${err.data.path}: ${err.data.message}` + ? err.data.message : `Failed to parse agent ${item}` Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() }) log.error("failed to load agent", { agent: item, err }) @@ -312,7 +312,7 @@ export namespace Config { })) { const md = await ConfigMarkdown.parse(item).catch((err) => { const message = ConfigMarkdown.FrontmatterError.isInstance(err) - ? `${err.data.path}: ${err.data.message}` + ? err.data.message : `Failed to parse mode ${item}` Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() }) log.error("failed to load mode", { mode: item, err }) diff --git a/packages/opencode/src/config/markdown.ts b/packages/opencode/src/config/markdown.ts index 1ec809586a..d1eeeac382 100644 --- a/packages/opencode/src/config/markdown.ts +++ b/packages/opencode/src/config/markdown.ts @@ -76,7 +76,7 @@ export namespace ConfigMarkdown { throw new FrontmatterError( { path: filePath, - message: `Failed to parse YAML frontmatter: ${err instanceof Error ? err.message : String(err)}`, + message: `${filePath}: Failed to parse YAML frontmatter: ${err instanceof Error ? err.message : String(err)}`, }, { cause: err }, ) diff --git a/packages/opencode/src/skill/skill.ts b/packages/opencode/src/skill/skill.ts index 95a599a547..6ae0e9fe88 100644 --- a/packages/opencode/src/skill/skill.ts +++ b/packages/opencode/src/skill/skill.ts @@ -48,7 +48,7 @@ export namespace Skill { const addSkill = async (match: string) => { const md = await ConfigMarkdown.parse(match).catch((err) => { const message = ConfigMarkdown.FrontmatterError.isInstance(err) - ? `${err.data.path}: ${err.data.message}` + ? err.data.message : `Failed to parse skill ${match}` Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() }) log.error("failed to load skill", { skill: match, err }) diff --git a/packages/opencode/test/config/fixtures/empty-frontmatter.md b/packages/opencode/test/config/fixtures/empty-frontmatter.md new file mode 100644 index 0000000000..95d5a80ed1 --- /dev/null +++ b/packages/opencode/test/config/fixtures/empty-frontmatter.md @@ -0,0 +1,4 @@ +--- +--- + +Content diff --git a/packages/opencode/test/config/fixtures/frontmatter.md b/packages/opencode/test/config/fixtures/frontmatter.md new file mode 100644 index 0000000000..27822d6218 --- /dev/null +++ b/packages/opencode/test/config/fixtures/frontmatter.md @@ -0,0 +1,28 @@ +--- +description: "This is a description wrapped in quotes" +# field: this is a commented out field that should be ignored +occupation: This man has the following occupation: Software Engineer +title: 'Hello World' +name: John "Doe" + +family: He has no 'family' +summary: > + This is a summary +url: https://example.com:8080/path?query=value +time: The time is 12:30:00 PM +nested: First: Second: Third: Fourth +quoted_colon: "Already quoted: no change needed" +single_quoted_colon: 'Single quoted: also fine' +mixed: He said "hello: world" and then left +empty: +dollar: Use $' and $& for special patterns +--- + +Content that should not be parsed: + +fake_field: this is not yaml +another: neither is this +time: 10:30:00 AM +url: https://should-not-be-parsed.com:3000 + +The above lines look like YAML but are just content. diff --git a/packages/opencode/test/config/fixtures/no-frontmatter.md b/packages/opencode/test/config/fixtures/no-frontmatter.md new file mode 100644 index 0000000000..39c9f3681a --- /dev/null +++ b/packages/opencode/test/config/fixtures/no-frontmatter.md @@ -0,0 +1 @@ +Content diff --git a/packages/opencode/test/config/markdown.test.ts b/packages/opencode/test/config/markdown.test.ts index 1d2e353489..b4263ee6b5 100644 --- a/packages/opencode/test/config/markdown.test.ts +++ b/packages/opencode/test/config/markdown.test.ts @@ -91,39 +91,7 @@ describe("ConfigMarkdown: normal template", () => { }) describe("ConfigMarkdown: frontmatter parsing", async () => { - const template = `--- -description: "This is a description wrapped in quotes" -# field: this is a commented out field that should be ignored -occupation: This man has the following occupation: Software Engineer -title: 'Hello World' -name: John "Doe" - -family: He has no 'family' -summary: > - This is a summary -url: https://example.com:8080/path?query=value -time: The time is 12:30:00 PM -nested: First: Second: Third: Fourth -quoted_colon: "Already quoted: no change needed" -single_quoted_colon: 'Single quoted: also fine' -mixed: He said "hello: world" and then left -empty: -dollar: Use $' and $& for special patterns ---- - -Content that should not be parsed: - -fake_field: this is not yaml -another: neither is this -time: 10:30:00 AM -url: https://should-not-be-parsed.com:3000 - -The above lines look like YAML but are just content. -` - - const matter = await import("gray-matter") - const preprocessed = ConfigMarkdown.preprocessFrontmatter(template) - const parsed = matter.default(preprocessed) + const parsed = await ConfigMarkdown.parse(import.meta.dir + "/fixtures/frontmatter.md") test("should parse without throwing", () => { expect(parsed).toBeDefined() @@ -202,3 +170,23 @@ The above lines look like YAML but are just content. expect(parsed.content).toContain("url: https://should-not-be-parsed.com:3000") }) }) + +describe("ConfigMarkdown: frontmatter parsing w/ empty frontmatter", async () => { + const result = await ConfigMarkdown.parse(import.meta.dir + "/fixtures/empty-frontmatter.md") + + test("should parse without throwing", () => { + expect(result).toBeDefined() + expect(result.data).toEqual({}) + expect(result.content.trim()).toBe("Content") + }) +}) + +describe("ConfigMarkdown: frontmatter parsing w/ no frontmatter", async () => { + const result = await ConfigMarkdown.parse(import.meta.dir + "/fixtures/no-frontmatter.md") + + test("should parse without throwing", () => { + expect(result).toBeDefined() + expect(result.data).toEqual({}) + expect(result.content.trim()).toBe("Content") + }) +})