Skip to content

Commit 7c0b979

Browse files
fix: exit when a package fails to install
The result of the package manager was ignored, so the CLI continued as if the package was installed. Claude-Session: https://claude.ai/code/session_013FhutRFjuQemBzekJMYcqL
1 parent e2f2dcb commit 7c0b979

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎.changeset/detect-missing-package-managers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack-cli": patch
33
---
44

5-
fix: detect missing package managers, without a lock file the CLI chose npm even when it wasn't installed instead of yarn or pnpm
5+
fix: detect missing package managers and failed installations, without a lock file the CLI chose npm even when it wasn't installed instead of yarn or pnpm, and it continued after a package failed to install

‎packages/webpack-cli/src/webpack-cli.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,17 @@ class WebpackCLI {
881881

882882
if (needInstall) {
883883
try {
884-
await this.#spawn(packageManager, commandArguments, { stdio: "inherit" });
884+
const { error, status } = await this.#spawn(packageManager, commandArguments, {
885+
stdio: "inherit",
886+
});
887+
888+
if (error) {
889+
throw error;
890+
}
891+
892+
if (status !== 0) {
893+
throw new Error(`'${commandToBeRun}' exited with code ${status}`);
894+
}
885895
} catch (error) {
886896
this.logger.error(error);
887897

‎test/api/install-package.test.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,42 @@ describe("installPackage", () => {
247247
expectInstallCommand("npm", ["install", "-D", "test-package"]);
248248
});
249249

250+
it.each([
251+
[
252+
"exits with an error",
253+
(child) => child.emit("close", 1),
254+
"'npm install -D test-package' exited with code 1",
255+
],
256+
[
257+
"fails to start",
258+
(child) => child.emit("error", new Error("spawn npm ENOENT")),
259+
"spawn npm ENOENT",
260+
],
261+
])("should exit when the installation %s", async (_name, fail, message) => {
262+
readlineQuestionMock.mockImplementation((_questionTest, cb) => cb("y"));
263+
getDefaultPackageManagerSpy.mockReturnValue("npm");
264+
spawnMock.mockImplementationOnce(() => {
265+
// eslint-disable-next-line unicorn/prefer-event-target -- Child processes are event emitters
266+
const child = new EventEmitter();
267+
268+
process.nextTick(() => fail(child));
269+
270+
return child;
271+
});
272+
273+
const mockExit = jest.spyOn(process, "exit").mockImplementation(() => {});
274+
const consoleMock = jest.spyOn(console, "error").mockImplementation(() => {});
275+
276+
await cli.installPackage("test-package");
277+
278+
expect(mockExit).toHaveBeenCalledWith(2);
279+
expect(consoleMock).toHaveBeenCalledTimes(1);
280+
expect(stripVTControlCharacters(consoleMock.mock.calls[0][0])).toContain(message);
281+
282+
mockExit.mockRestore();
283+
consoleMock.mockRestore();
284+
});
285+
250286
it("should not install if install is not confirmed", async () => {
251287
readlineQuestionMock.mockImplementation((_questionTest, cb) => cb("n"));
252288

0 commit comments

Comments
 (0)