Skip to content

Commit eaf2b03

Browse files
committed
fix: stop run-install after install failure
1 parent d060e0e commit eaf2b03

3 files changed

Lines changed: 107 additions & 18 deletions

File tree

dist/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/run-install.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { describe, it, expect, afterEach, vi } from "vite-plus/test";
2+
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { delimiter, join } from "node:path";
5+
import { runViteInstall } from "./run-install.js";
6+
import type { Inputs } from "./types.js";
7+
8+
const baseInputs: Inputs = {
9+
version: "latest",
10+
nodeVersion: undefined,
11+
nodeVersionFile: undefined,
12+
workingDirectory: undefined,
13+
runInstall: [],
14+
cache: false,
15+
cacheDependencyPath: undefined,
16+
registryUrl: undefined,
17+
scope: undefined,
18+
};
19+
20+
describe("runViteInstall", () => {
21+
let tempDir: string | undefined;
22+
23+
afterEach(() => {
24+
vi.unstubAllEnvs();
25+
if (tempDir) {
26+
rmSync(tempDir, { recursive: true, force: true });
27+
tempDir = undefined;
28+
}
29+
});
30+
31+
it("stops after the first failed install", async () => {
32+
tempDir = mkdtempSync(join(tmpdir(), "setup-vp-"));
33+
const binDir = join(tempDir, "bin");
34+
const appDir = join(tempDir, "packages", "app");
35+
const libDir = join(tempDir, "packages", "lib");
36+
const callsLog = join(tempDir, "calls.log");
37+
38+
mkdirSync(binDir);
39+
mkdirSync(appDir, { recursive: true });
40+
mkdirSync(libDir, { recursive: true });
41+
writeFileSync(join(appDir, ".fail-vp-install"), "");
42+
43+
writeFileSync(
44+
join(binDir, "vp"),
45+
[
46+
"#!/bin/sh",
47+
'printf "%s %s\\n" "$PWD" "$*" >> "$VP_CALLS_LOG"',
48+
'if [ -f ".fail-vp-install" ]; then',
49+
' echo "install failed" >&2',
50+
" exit 1",
51+
"fi",
52+
"exit 0",
53+
"",
54+
].join("\n"),
55+
);
56+
chmodSync(join(binDir, "vp"), 0o755);
57+
58+
writeFileSync(
59+
join(binDir, "vp.cmd"),
60+
[
61+
"@echo off",
62+
'echo %CD% %*>>"%VP_CALLS_LOG%"',
63+
'if exist ".fail-vp-install" (',
64+
" echo install failed 1>&2",
65+
" exit /b 1",
66+
")",
67+
"exit /b 0",
68+
"",
69+
].join("\r\n"),
70+
);
71+
72+
vi.stubEnv("GITHUB_WORKSPACE", tempDir);
73+
vi.stubEnv("PATH", `${binDir}${delimiter}${process.env.PATH ?? ""}`);
74+
vi.stubEnv("VP_CALLS_LOG", callsLog);
75+
76+
await expect(
77+
runViteInstall({
78+
...baseInputs,
79+
runInstall: [{ cwd: "packages/app" }, { cwd: "packages/lib" }],
80+
}),
81+
).rejects.toThrow(`Command "vp install" (cwd: ${appDir}) exited with code 1`);
82+
83+
const calls = readFileSync(callsLog, "utf8").trim().split(/\r?\n/);
84+
expect(calls).toHaveLength(1);
85+
expect(calls[0]).toContain(`${join("packages", "app")} install`);
86+
});
87+
});

src/run-install.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { startGroup, endGroup, setFailed, info, error as logError } from "@actions/core";
1+
import { startGroup, endGroup, info, error as logError } from "@actions/core";
22
import { getExecOutput } from "@actions/exec";
33
import type { Inputs } from "./types.js";
44
import { getConfiguredProjectDir, getInstallCwd } from "./utils.js";
@@ -25,28 +25,30 @@ export async function runViteInstall(inputs: Inputs): Promise<void> {
2525

2626
startGroup(`Running ${cmdStr} in ${cwd}...`);
2727

28+
let result: Awaited<ReturnType<typeof getExecOutput>>;
2829
try {
29-
const { exitCode, stdout, stderr } = await getExecOutput("vp", args, {
30+
result = await getExecOutput("vp", args, {
3031
cwd,
3132
ignoreReturnCode: true,
3233
});
33-
endGroup();
34-
35-
if (exitCode === 0) {
36-
info(`Successfully ran ${cmdStr}`);
37-
continue;
38-
}
39-
40-
const detail = stderr.trim() || stdout.trim();
41-
if (detail) {
42-
logError(tailOutput(detail, MAX_ERROR_TAIL), {
43-
title: `${cmdStr} failed`,
44-
});
45-
}
46-
setFailed(`Command "${cmdStr}" (cwd: ${cwd}) exited with code ${exitCode}`);
4734
} catch (error) {
4835
endGroup();
49-
setFailed(`Failed to run ${cmdStr}: ${String(error)}`);
36+
throw new Error(`Failed to run ${cmdStr}: ${String(error)}`);
37+
}
38+
39+
endGroup();
40+
41+
if (result.exitCode === 0) {
42+
info(`Successfully ran ${cmdStr}`);
43+
continue;
44+
}
45+
46+
const detail = result.stderr.trim() || result.stdout.trim();
47+
if (detail) {
48+
logError(tailOutput(detail, MAX_ERROR_TAIL), {
49+
title: `${cmdStr} failed`,
50+
});
5051
}
52+
throw new Error(`Command "${cmdStr}" (cwd: ${cwd}) exited with code ${result.exitCode}`);
5153
}
5254
}

0 commit comments

Comments
 (0)