Skip to content

Commit 464283d

Browse files
fix(parse): improve error messages for malformed changesets
Fixes changesets#175 This PR improves the error handling and messages when parsing malformed changeset files. The previous error messages were generic and didn't help users understand what went wrong. Changes: - Add validation for empty changeset files with helpful example - Improve error message when frontmatter is missing or invalid - Add specific error for invalid YAML in frontmatter, including the YAML error - Validate version types (major, minor, patch, none) with clear error message - Validate package names are non-empty strings - Include examples of correct format in error messages - Add tests for all new error scenarios The error messages now: - Explain what went wrong - Show what was received - Provide an example of the correct format Co-authored-by: Cursor <[email protected]>
1 parent 10d13ae commit 464283d

2 files changed

Lines changed: 92 additions & 18 deletions

File tree

‎packages/parse/src/index.test.ts‎

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,7 @@ describe("parsing a changeset", () => {
235235
Nice simple summary
236236
`;
237237

238-
expect(() => parse(changesetMd)).toThrowErrorMatchingInlineSnapshot(`
239-
"could not parse changeset - invalid frontmatter: ---
240-
"cool-package": minor
241-
--- fail
242-
243-
Nice simple summary"
244-
`);
238+
expect(() => parse(changesetMd)).toThrow("missing or invalid frontmatter");
245239
});
246240

247241
it("should throw when frontmatter hasn't a valid yml structure", () => {
@@ -252,12 +246,41 @@ describe("parsing a changeset", () => {
252246
Nice simple summary
253247
`;
254248

255-
expect(() => parse(changesetMd)).toThrowErrorMatchingInlineSnapshot(`
256-
"could not parse changeset - invalid frontmatter: ---
257-
: minor
258-
---
249+
expect(() => parse(changesetMd)).toThrow("could not parse changeset");
250+
expect(() => parse(changesetMd)).toThrow("invalid YAML");
251+
});
252+
253+
it("should throw when file is completely empty", () => {
254+
expect(() => parse("")).toThrow("file is empty");
255+
expect(() => parse(" ")).toThrow("file is empty");
256+
expect(() => parse("\n\n")).toThrow("file is empty");
257+
});
258+
259+
it("should throw when frontmatter is missing", () => {
260+
const changesetMd = "Just some content without frontmatter";
261+
expect(() => parse(changesetMd)).toThrow("missing or invalid frontmatter");
262+
});
263+
264+
it("should throw when version type is invalid", () => {
265+
const changesetMd = outdent`---
266+
"cool-package": invalid-type
267+
---
268+
269+
Nice simple summary
270+
`;
271+
272+
expect(() => parse(changesetMd)).toThrow('invalid version type "invalid-type"');
273+
expect(() => parse(changesetMd)).toThrow("major, minor, patch, none");
274+
});
275+
276+
it("should throw with helpful message when package name is empty", () => {
277+
const changesetMd = outdent`---
278+
"": minor
279+
---
280+
281+
Nice simple summary
282+
`;
259283

260-
Nice simple summary"
261-
`);
284+
expect(() => parse(changesetMd)).toThrow("invalid package name");
262285
});
263286
});

‎packages/parse/src/index.ts‎

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,53 @@ import { Release, VersionType } from "@changesets/types";
33

44
const mdRegex = /\s*---([^]*?)\n\s*---(\s*(?:\n|$)[^]*)/;
55

6+
function validateReleases(
7+
releases: Release[],
8+
contents: string
9+
): void {
10+
const validVersionTypes = ["major", "minor", "patch", "none"];
11+
12+
for (const release of releases) {
13+
if (typeof release.name !== "string" || release.name.trim() === "") {
14+
throw new Error(
15+
`could not parse changeset - invalid package name in frontmatter.\n` +
16+
`Expected a non-empty string for package name, but got: ${JSON.stringify(release.name)}\n` +
17+
`Make sure your changeset frontmatter follows this format:\n` +
18+
`---\n"package-name": patch\n---`
19+
);
20+
}
21+
22+
if (!validVersionTypes.includes(release.type)) {
23+
throw new Error(
24+
`could not parse changeset - invalid version type "${release.type}" for package "${release.name}".\n` +
25+
`Valid version types are: ${validVersionTypes.join(", ")}\n` +
26+
`Changeset contents:\n${contents}`
27+
);
28+
}
29+
}
30+
}
31+
632
export default function parseChangesetFile(contents: string): {
733
summary: string;
834
releases: Release[];
935
} {
36+
const trimmedContents = contents.trim();
37+
38+
if (!trimmedContents) {
39+
throw new Error(
40+
`could not parse changeset - file is empty.\n` +
41+
`Changesets must have frontmatter with package names and version types.\n` +
42+
`Example:\n---\n"package-name": patch\n---\n\nYour changeset summary here.`
43+
);
44+
}
45+
1046
const execResult = mdRegex.exec(contents);
1147
if (!execResult) {
1248
throw new Error(
13-
`could not parse changeset - invalid frontmatter: ${contents}`
49+
`could not parse changeset - missing or invalid frontmatter.\n` +
50+
`Changesets must start with frontmatter delimited by "---".\n` +
51+
`Example:\n---\n"package-name": patch\n---\n\nYour changeset summary here.\n\n` +
52+
`Received content:\n${trimmedContents.slice(0, 200)}${trimmedContents.length > 200 ? "..." : ""}`
1453
);
1554
}
1655
let [, roughReleases, roughSummary] = execResult;
@@ -23,6 +62,14 @@ export default function parseChangesetFile(contents: string): {
2362
| undefined;
2463

2564
if (yamlStuff) {
65+
if (typeof yamlStuff !== "object" || Array.isArray(yamlStuff)) {
66+
throw new Error(
67+
`could not parse changeset - frontmatter must be an object mapping package names to version types.\n` +
68+
`Expected format:\n---\n"package-name": patch\n---\n\n` +
69+
`Received:\n${roughReleases}`
70+
);
71+
}
72+
2673
releases = Object.entries(yamlStuff).map(([name, type]) => ({
2774
name,
2875
type,
@@ -31,14 +78,18 @@ export default function parseChangesetFile(contents: string): {
3178
releases = [];
3279
}
3380
} catch (e) {
81+
if (e instanceof Error && e.message.startsWith("could not parse changeset")) {
82+
throw e;
83+
}
3484
throw new Error(
35-
`could not parse changeset - invalid frontmatter: ${contents}`
85+
`could not parse changeset - invalid YAML in frontmatter.\n` +
86+
`The frontmatter between the "---" delimiters must be valid YAML.\n` +
87+
`YAML error: ${e instanceof Error ? e.message : String(e)}\n\n` +
88+
`Frontmatter content:\n${roughReleases}`
3689
);
3790
}
3891

39-
if (!releases) {
40-
throw new Error(`could not parse changeset - unknown error: ${contents}`);
41-
}
92+
validateReleases(releases, contents);
4293

4394
return { releases, summary };
4495
}

0 commit comments

Comments
 (0)