Skip to content

Commit 285bba6

Browse files
committed
Merge branch 'main' into set-up-npm-auth-docs
2 parents 0541eb1 + 84d78c6 commit 285bba6

3 files changed

Lines changed: 83 additions & 6 deletions

File tree

.changeset/calm-files-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/action": patch
3+
---
4+
5+
Allow custom publish scripts to complete without a Changesets output file, warning that GitHub releases and git tags cannot be created when that file is missing.

src/run.test.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import path from "node:path";
2+
import * as core from "@actions/core";
23
import type { Changeset } from "@changesets/types";
34
import { writeChangeset } from "@changesets/write";
45
import { createFixture } from "fs-fixture";
5-
import { beforeEach, describe, expect, it, vi } from "vitest";
6+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
67
import { GitHub } from "./github.ts";
7-
import { runVersion } from "./run.ts";
8+
import { runPublish, runVersion } from "./run.ts";
89

910
vi.mock("@actions/github", () => ({
1011
context: {
@@ -20,6 +21,10 @@ vi.mock("@actions/github", () => ({
2021
graphql: mockedGraphql,
2122
}),
2223
}));
24+
vi.mock("@actions/core", async (importOriginal) => ({
25+
...(await importOriginal<typeof import("@actions/core")>()),
26+
warning: vi.fn(),
27+
}));
2328
vi.mock("@changesets/ghcommit");
2429

2530
let mockedGithubMethods = {
@@ -102,6 +107,59 @@ beforeEach(() => {
102107
vi.clearAllMocks();
103108
});
104109

110+
afterEach(() => {
111+
vi.unstubAllEnvs();
112+
});
113+
114+
describe("publish", () => {
115+
it("warns when a custom publish script does not create the output file", async () => {
116+
await using fixture = await createSimpleProjectFixture();
117+
const cwd = fixture.path;
118+
vi.stubEnv("RUNNER_TEMP", cwd);
119+
120+
const result = await runPublish({
121+
script: 'node -e "void 0"',
122+
github: createGithub(cwd),
123+
createGithubReleases: true,
124+
pushGitTags: true,
125+
cwd,
126+
});
127+
128+
expect(result).toEqual({ published: false, exitCode: 0 });
129+
expect(core.warning).toHaveBeenCalledWith(
130+
expect.stringContaining(
131+
"GitHub releases and git tags cannot be created without this output",
132+
),
133+
);
134+
});
135+
136+
it("throws when the built-in publish command does not create the output file", async () => {
137+
await using fixture = await createFixture({
138+
"node_modules/@changesets/cli/package.json": JSON.stringify({
139+
name: "@changesets/cli",
140+
type: "module",
141+
}),
142+
"node_modules/@changesets/cli/bin.js": "",
143+
"package.json": JSON.stringify({
144+
name: "simple-project",
145+
version: "1.0.0",
146+
}),
147+
"package-lock.json": "",
148+
});
149+
const cwd = fixture.path;
150+
vi.stubEnv("RUNNER_TEMP", cwd);
151+
152+
await expect(
153+
runPublish({
154+
github: createGithub(cwd),
155+
createGithubReleases: true,
156+
pushGitTags: true,
157+
cwd,
158+
}),
159+
).rejects.toThrow("Failed to read changesets output at");
160+
});
161+
});
162+
105163
describe("version", () => {
106164
it("creates simple PR", async () => {
107165
await using fixture = await createSimpleProjectFixture();

src/run.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ type ChangesetsOutputEvent = {
7979
packageName: string;
8080
};
8181

82+
class ChangesetsOutputReadError extends Error {}
83+
8284
type PublishResult =
8385
| {
8486
published: true;
@@ -113,9 +115,10 @@ async function readChangesetsOutput(outputPath: string) {
113115
try {
114116
rawOutput = await fs.readFile(outputPath, "utf8");
115117
} catch (err) {
116-
throw new Error(`Failed to read changesets output at ${outputPath}`, {
117-
cause: err,
118-
});
118+
throw new ChangesetsOutputReadError(
119+
`Failed to read changesets output at ${outputPath}`,
120+
{ cause: err },
121+
);
119122
}
120123

121124
const events: ChangesetsOutputEvent[] = [];
@@ -195,7 +198,18 @@ export async function runPublish({
195198

196199
let { packages, tool } = await getPackages(cwd);
197200
let packagesByName = new Map(packages.map((x) => [x.packageJson.name, x]));
198-
let output = await readChangesetsOutput(outputFile);
201+
let output: ChangesetsOutputEvent[];
202+
try {
203+
output = await readChangesetsOutput(outputFile);
204+
} catch (err) {
205+
if (!script || !(err instanceof ChangesetsOutputReadError)) {
206+
throw err;
207+
}
208+
core.warning(
209+
`${err.message}. GitHub releases and git tags cannot be created without this output. Ensure the custom publish script passes CHANGESETS_OUTPUT to the Changesets CLI.`,
210+
);
211+
output = [];
212+
}
199213
let releases = output.map((event) => {
200214
let pkg = packagesByName.get(event.packageName);
201215
if (pkg === undefined) {

0 commit comments

Comments
 (0)