Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): answer --json when a command fails to load (#6551)

Joey2a54bbeb962a
diff --git a/.changeset/cli-spec-load-failure-envelope.md b/.changeset/cli-spec-load-failure-envelope.md
new file mode 100644
index 0000000000..5218c2c8df
--- /dev/null
+++ b/.changeset/cli-spec-load-failure-envelope.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] When a command's module fails to load, running that command with `--json` now prints one error envelope (`ERR_UNKNOWN`, with the load error in the message) instead of printing nothing to stdout. Without `--json` the error is still printed to stderr, and the exit code is still 1 in both modes.
+
+@josephfarina
diff --git a/packages/cli/clients/cli/command-load-failure.test.mjs b/packages/cli/clients/cli/command-load-failure.test.mjs
new file mode 100644
index 0000000000..54e170526c
--- /dev/null
+++ b/packages/cli/clients/cli/command-load-failure.test.mjs
@@ -0,0 +1,83 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file A command whose module fails to import still answers: with --json, one
+ * error envelope on stdout; without it, the error on stderr. Exit 1 either way.
+ *
+ * The failure is injected with a module-load hook in a real subprocess, so the
+ * lazy command registry in index.mjs sees what a broken install would give it.
+ */
+
+import {describe, it, expect, beforeAll, afterAll} from 'vitest';
+import {spawnSync} from 'node:child_process';
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import {fileURLToPath, pathToFileURL} from 'node:url';
+
+const BIN = fileURLToPath(new URL('./bin/astryx.mjs', import.meta.url));
+
+/** @type {string} */
+let dir;
+/** @type {string} */
+let register;
+
+beforeAll(() => {
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-load-failure-'));
+ const hooks = path.join(dir, 'hooks.mjs');
+ fs.writeFileSync(
+ hooks,
+ [
+ 'export async function load(url, context, nextLoad) {',
+ " if (url.endsWith('/clients/cli/commands/blog.mjs')) {",
+ " throw new Error('simulated load failure');",
+ ' }',
+ ' return nextLoad(url, context);',
+ '}',
+ '',
+ ].join('\n'),
+ );
+ register = path.join(dir, 'register.mjs');
+ fs.writeFileSync(
+ register,
+ `import {register} from 'node:module';\nregister(${JSON.stringify(pathToFileURL(hooks).href)});\n`,
+ );
+});
+
+afterAll(() => {
+ fs.rmSync(dir, {recursive: true, force: true});
+});
+
+/** @param {string[]} args */
+function run(args) {
+ const res = spawnSync(
+ process.execPath,
+ ['--import', pathToFileURL(register).href, BIN, ...args],
+ {cwd: dir, encoding: 'utf-8', timeout: 20_000},
+ );
+ return {status: res.status, stdout: res.stdout, stderr: res.stderr};
+}
+
+describe('a command that fails to load', () => {
+ it('answers --json with one error envelope, exit 1', () => {
+ const {status, stdout, stderr} = run(['blog', '--json']);
+ expect(status).toBe(1);
+ expect(stderr).toBe('');
+ const envelope = JSON.parse(stdout);
+ expect(envelope).toMatchObject({apiVersion: 1, code: 'ERR_UNKNOWN'});
+ expect(envelope.error).toMatch(/blog.*simulated load failure/);
+ });
+
+ it('reports the failure on stderr without --json, exit 1', () => {
+ const {status, stdout, stderr} = run(['blog']);
+ expect(status).toBe(1);
+ expect(stdout).toBe('');
+ expect(stderr).toMatch(/blog.*failed to load.*simulated load failure/s);
+ });
+
+ it('leaves the other commands working', () => {
+ const {status, stdout} = run(['docs', '--json']);
+ expect(status).toBe(0);
+ expect(JSON.parse(stdout).type).toMatch(/^docs\./);
+ });
+});
diff --git a/packages/cli/clients/cli/index.mjs b/packages/cli/clients/cli/index.mjs
index fc92ab87ef..a763d1d455 100644
--- a/packages/cli/clients/cli/index.mjs
+++ b/packages/cli/clients/cli/index.mjs
@@ -557,16 +557,18 @@ export async function createProgram() {
mod[cmd.register](program);
} catch (e) {
// Command fails to load but CLI still works
+ const reason = /** @type {any} */ (e).message;
const stub = program
.command(cmd.name)
- .description(`(failed to load: ${/** @type {any} */ (e).message})`)
+ .description(`(failed to load: ${reason})`)
.action(() => {
// Nothing loaded, so nothing was answered — say that rather than
// leaving the run's result unreported.
debug.recordCommandResult(debug.NO_RESULT_SET);
- console.error(`Command "${cmd.name}" failed to load:`);
- console.error(/** @type {any} */ (e).message);
- process.exit(1);
+ // cliError, so --json still gets its one envelope.
+ cliError(`Command "${cmd.name}" failed to load: ${reason}`, {
+ code: ERROR_CODES.ERR_UNKNOWN,
+ });
});
markReportsResult(stub);
}