An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): refuse invalid input that used to silently succeed (#7149)
Joey18d37bd13c4c
diff --git a/.changeset/cli-refuse-invalid-silent-success.md b/.changeset/cli-refuse-invalid-silent-success.mdnew file mode 100644index 0000000000..3a545c98e3--- /dev/null+++ b/.changeset/cli-refuse-invalid-silent-success.md@@ -0,0 +1,11 @@+---+'@astryxdesign/cli': minor+---++[breaking] Refuse invalid input that used to silently succeed.++- incompatible-fix `IFIX-0007` / cleanup `CLN-0013`++Migration: pass a valid type or package, an integer limit, or the documented arguments, and catch `ERR_AMBIGUOUS_COMPONENT` for a multi-package name.++@josephfarinadiff --git a/packages/cli/api/build/build.mjs b/packages/cli/api/build/build.mjsindex 89b6eba7d2..e1c651bea6 100644--- a/packages/cli/api/build/build.mjs+++ b/packages/cli/api/build/build.mjs@@ -13,6 +13,8 @@* a query → build.kit (api/build/kit/kit.mjs)*/+import {AstryxError} from '../error.mjs';+import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';import {buildHelp} from './help/help.mjs';import {buildKit} from './kit/kit.mjs';@@ -31,5 +33,12 @@ export async function build(query, options = {}) {if (!query || !String(query).trim()) {return buildHelp();}+ if (options.limit != null && (!Number.isInteger(options.limit) || options.limit <= 0)) {+ throw new AstryxError(+ `Invalid limit "${options.limit}". Must be a positive integer.`,+ undefined,+ ERROR_CODES.ERR_INVALID_ARGUMENT,+ );+ }return buildKit(query, options);}diff --git a/packages/cli/api/component/_adapter.mjs b/packages/cli/api/component/_adapter.mjsindex 075435b935..55e5c995a5 100644--- a/packages/cli/api/component/_adapter.mjs+++ b/packages/cli/api/component/_adapter.mjs@@ -324,7 +324,7 @@ export class ComponentAmbiguityError extends AstryxError {super(`Component "${dirName}" is provided by multiple packages. Re-run with --package <pkg> to choose one.`,owners.map(o => ({name: o.package, reason: 'provides this component'})),- ERROR_CODES.ERR_UNKNOWN_COMPONENT,+ ERROR_CODES.ERR_AMBIGUOUS_COMPONENT,);this.candidates = owners.map(owner => ({package: owner.package,@@ -375,6 +375,8 @@ export function resolveCoreSourcePath(coreDir, dirName) {/*** Resolve a bare (unscoped) name to a `.doc.mjs`: core first, then back-compat* externals, then a conservative fuzzy search. Throws ERR_UNKNOWN_COMPONENT+ * for a genuinely unknown name, and ERR_AMBIGUOUS_COMPONENT when multiple+ * packages provide the same name* (with candidate suggestions when close) or ERR_NO_DOC.* @param {string} dirName - bare component name (no `XDS` prefix)* @param {{coreDir: string, cwd: string, name: string}} ctx - `name` is the caller's original (prefixed) input, used in error textdiff --git a/packages/cli/api/component/component.doc.mjs b/packages/cli/api/component/component.doc.mjsindex 4b8cfe5ae4..5e189fd703 100644--- a/packages/cli/api/component/component.doc.mjs+++ b/packages/cli/api/component/component.doc.mjs@@ -170,7 +170,11 @@ export const doc = {},{code: 'ERR_UNKNOWN_COMPONENT',- when: 'name is not a string, resolves to no known component, is provided by multiple packages (pass options.package), or is absent from the requested options.package',+ when: 'name is not a string, resolves to no known component, or is absent from the requested options.package',+ },+ {+ code: 'ERR_AMBIGUOUS_COMPONENT',+ when: 'the component is provided by multiple packages (pass options.package to disambiguate)',},{code: 'ERR_UNKNOWN_PACKAGE',diff --git a/packages/cli/api/template/list/list.mjs b/packages/cli/api/template/list/list.mjsindex 47dd96dacd..ae9a2940d8 100644--- a/packages/cli/api/template/list/list.mjs+++ b/packages/cli/api/template/list/list.mjs@@ -10,6 +10,8 @@*/import {pkgOf} from '../../../foundation/discovery/template-adapter.mjs';+import {AstryxError} from '../../error.mjs';+import {ERROR_CODES} from '../../../foundation/response/error-codes.mjs';/*** Project a discovered template set into the `template.list` envelope.@@ -19,10 +21,29 @@ import {pkgOf} from '../../../foundation/discovery/template-adapter.mjs';*/export function templateList(templates, options = {}) {const {type, package: packageFilter} = options;+ const VALID_TYPES = ['page', 'block'];let filtered = templates;- if (type) filtered = filtered.filter(t => t.type === type);- if (packageFilter)+ if (type) {+ if (!VALID_TYPES.includes(type)) {+ throw new AstryxError(+ `Unknown template type "${type}". Valid types: ${VALID_TYPES.join(', ')}.`,+ VALID_TYPES.map(t => ({name: t, reason: 'valid type'})),+ ERROR_CODES.ERR_INVALID_ARGUMENT,+ );+ }+ filtered = filtered.filter(t => t.type === type);+ }+ if (packageFilter) {+ const allPackages = [...new Set(templates.map(t => pkgOf(t)))];+ if (!allPackages.includes(packageFilter)) {+ throw new AstryxError(+ `No templates found in package "${packageFilter}".`,+ allPackages.sort().map(p => ({name: p, reason: 'has templates'})),+ ERROR_CODES.ERR_INVALID_ARGUMENT,+ );+ }filtered = filtered.filter(t => pkgOf(t) === packageFilter);+ }return {type: 'template.list',data: filtered.map(t => ({diff --git a/packages/cli/api/template/list/list.test.mjs b/packages/cli/api/template/list/list.test.mjsnew file mode 100644index 0000000000..6d2083eeed--- /dev/null+++ b/packages/cli/api/template/list/list.test.mjs@@ -0,0 +1,52 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.+import {describe, expect, it} from 'vitest';+import {templateList} from './list.mjs';++const t = (type, dirName, pkg) => ({+ type,+ dirName,+ name: dirName,+ description: dirName,+ ...(pkg ? {package: pkg} : {}),+});++const templates = [+ t('page', 'dashboard'),+ t('block', 'hero'),+ t('block', 'promo', '@acme/blocks'),+];++describe('templateList filters', () => {+ it('returns an empty list for a valid filter that matches nothing', () => {+ // @acme/blocks ships only blocks, so asking it for pages is a valid+ // filter with no match: an empty list, not an error.+ const response = templateList(templates, {+ type: 'page',+ package: '@acme/blocks',+ });+ expect(response.type).toBe('template.list');+ expect(response.data).toEqual([]);+ });++ it('refuses a type outside page or block', () => {+ expect(() => templateList(templates, {type: 'bogus'})).toThrow(+ /Unknown template type "bogus"/,+ );+ });++ it('refuses a package that provides no templates', () => {+ expect(() => templateList(templates, {package: '@nope/x'})).toThrow(+ /No templates found in package "@nope\/x"/,+ );+ });++ it('keeps a matching type and package filter', () => {+ const response = templateList(templates, {+ type: 'block',+ package: '@acme/blocks',+ });+ expect(response.data.map(item => item.id ?? item.dirName)).toEqual([+ 'promo',+ ]);+ });+});diff --git a/packages/cli/api/template/template.doc.mjs b/packages/cli/api/template/template.doc.mjsindex 9318cd7372..9cf59caabf 100644--- a/packages/cli/api/template/template.doc.mjs+++ b/packages/cli/api/template/template.doc.mjs@@ -48,7 +48,8 @@ export const doc = {{name: 'options.list',type: 'boolean',- description: 'List templates instead of scaffolding; refused with ERR_INVALID_ARGUMENT when a name or path is also given.',+ description:+ 'List templates instead of scaffolding; refused with ERR_INVALID_ARGUMENT when a name or path is also given.',default: 'false',},{@@ -89,7 +90,7 @@ export const doc = {type: 'string',description:'Destination (relative to cwd) to scaffold the template into. Its presence switches a named lookup into a copy. ' +- 'A path that ends in .tsx, .ts, .jsx, .js, .mjs, .cjs, .css, .scss, .json, .md or .html is the file to write; any other path is a directory, which receives page.tsx for a page template or the block\'s own file name for a block.',+ "A path that ends in .tsx, .ts, .jsx, .js, .mjs, .cjs, .css, .scss, .json, .md or .html is the file to write; any other path is a directory, which receives page.tsx for a page template or the block's own file name for a block.",},{name: 'options.overwrite',@@ -135,7 +136,7 @@ export const doc = {throws: [{code: 'ERR_INVALID_ARGUMENT',- when: '--cdn is combined with <name>, --list, --skeleton, --type, or --package; --list is combined with <name>, <path>, --skeleton, or --overwrite; --skeleton is combined with <path> or --overwrite',+ when: '--cdn is combined with <name>, --list, --skeleton, --type, or --package; --list is combined with <name>, <path>, --skeleton, or --overwrite; --skeleton is combined with <path> or --overwrite; or options.list is set with a type outside page or block, or with a package that provides no templates (a valid filter that matches nothing returns an empty list instead)',},{code: 'ERR_UNKNOWN_TEMPLATE',diff --git a/packages/cli/clients/cli/commands/build.doc.mjs b/packages/cli/clients/cli/commands/build.doc.mjsindex 33dfac0951..ea29a13406 100644--- a/packages/cli/clients/cli/commands/build.doc.mjs+++ b/packages/cli/clients/cli/commands/build.doc.mjs@@ -51,7 +51,7 @@ export const doc = {{code: 0, when: 'success (including zero matches)'},{code: 1,- when: 'invalid --type, a non-positive --limit, or a query when @astryxdesign/core cannot be found (the playbook needs no core)',+ when: 'invalid --type, a --limit that is not a positive integer, or a query when @astryxdesign/core cannot be found (the playbook needs no core)',},],related: ['search', 'template', 'component', 'hook', 'init'],diff --git a/packages/cli/clients/cli/commands/build.mjs b/packages/cli/clients/cli/commands/build.mjsindex b0492b3212..e1d96a01d4 100644--- a/packages/cli/clients/cli/commands/build.mjs+++ b/packages/cli/clients/cli/commands/build.mjs@@ -101,7 +101,7 @@ export function registerBuild(program) {// throws ERR_INVALID_ARGUMENT, so we pass NaN through rather than// pre-rejecting with a generic code here (parity with `search`).const limit =- options.limit != null ? Number.parseInt(options.limit, 10) : 60;+ options.limit != null ? Number(options.limit) : 60;/** @type {import('../../../api/build/build.type.mjs').BuildKitResponse} */let result;diff --git a/packages/cli/clients/cli/commands/component-ownership.test.mjs b/packages/cli/clients/cli/commands/component-ownership.test.mjsindex 400ddff3ce..4494825166 100644--- a/packages/cli/clients/cli/commands/component-ownership.test.mjs+++ b/packages/cli/clients/cli/commands/component-ownership.test.mjs@@ -223,7 +223,7 @@ describe('component() — integration ownership via config', () => {caught = e;}expect(caught).toBeTruthy();- expect(caught.code).toBe('ERR_UNKNOWN_COMPONENT');+ expect(caught.code).toBe('ERR_AMBIGUOUS_COMPONENT');const pkgs = (caught.suggestions ?? []).map(s => s.name);expect(pkgs).toContain(CORE_PACKAGE);expect(pkgs).toContain(INTEGRATION_NAME);@@ -288,7 +288,7 @@ describe('component() — integration ownership via config', () => {expect(result.data.results[1]).toMatchObject({selector: 'AppShell',status: 'ambiguous',- code: 'ERR_UNKNOWN_COMPONENT',+ code: 'ERR_AMBIGUOUS_COMPONENT',});expect(result.data.results[1].candidates).toEqual(expect.arrayContaining([diff --git a/packages/cli/clients/cli/commands/integration.mjs b/packages/cli/clients/cli/commands/integration.mjsindex dd562e2c25..d774135961 100644--- a/packages/cli/clients/cli/commands/integration.mjs+++ b/packages/cli/clients/cli/commands/integration.mjs@@ -101,7 +101,7 @@ export function registerIntegration(program) {// still parses its own options.integration.allowUnknownOption(true);- defineCommand(integration, integrationAddCommand, {+ const addCmd = defineCommand(integration, integrationAddCommand, {fn: integrationAddFn,action: async (kind, name, options) => {const json = program.opts().json || false;@@ -157,6 +157,7 @@ export function registerIntegration(program) {return NO_RESULT_SET;},});+ addCmd.allowExcessArguments(false);defineCommand(integration, integrationVerifyCommand, {fn: integrationPackCheckFn,diff --git a/packages/cli/clients/cli/commands/silent-success-refusals.test.mjs b/packages/cli/clients/cli/commands/silent-success-refusals.test.mjsnew file mode 100644index 0000000000..21e089cf32--- /dev/null+++ b/packages/cli/clients/cli/commands/silent-success-refusals.test.mjs@@ -0,0 +1,15 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.+import {describe, it, expect, beforeEach, afterEach} from 'vitest';+import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path';+import {runCli} from '../../../test-utils/run-cli.mjs';+let dir;+beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(),'astryx-ss-')); fs.writeFileSync(path.join(dir,'package.json'),JSON.stringify({name:'s',version:'1.0.0',dependencies:{'@astryxdesign/core':'0.6.3'}})); const c=path.join(dir,'node_modules','@astryxdesign','core'); fs.mkdirSync(c,{recursive:true}); fs.writeFileSync(path.join(c,'package.json'),JSON.stringify({name:'@astryxdesign/core',version:'0.6.3'})); });+afterEach(() => fs.rmSync(dir, {recursive:true,force:true}));+const json = async a => { const {status,stdout}=await runCli(['--json',...a],{cwd:dir}); return {status,body:JSON.parse(stdout)}; };+describe('refuse invalid input (IFIX-0007)', () => {+ it('template --type bogus', async () => { const {status,body}=await json(['template','--list','--type','bogus']); expect(status).toBe(1); expect(body.code).toBe('ERR_INVALID_ARGUMENT'); });+ it('template --package @nope/x', async () => { const {status,body}=await json(['template','--list','--package','@nope/x']); expect(status).toBe(1); expect(body.code).toBe('ERR_INVALID_ARGUMENT'); });+ it('template valid type+package filter keeps the matching templates', async () => { const {status,body}=await json(['template','--list','--type','block','--package','@astryxdesign/core']); expect(status).toBe(0); expect(body.type).toBe('template.list'); expect(body.data.length).toBeGreaterThan(0); expect(body.data.every(t => t.type === 'block')).toBe(true); });+ it('build --limit 2.5', async () => { const {status,body}=await json(['build','pricing','--limit','2.5']); expect(status).toBe(1); expect(body.code).toBe('ERR_INVALID_ARGUMENT'); });+ it('integration add excess args', async () => { fs.writeFileSync(path.join(dir,'package.json'),JSON.stringify({name:'@t/p',version:'1.0.0'})); const {status}=await json(['integration','add','agent-doc','Run','acme','verify']); expect(status).toBe(1); });+});diff --git a/packages/cli/clients/cli/commands/template.doc.mjs b/packages/cli/clients/cli/commands/template.doc.mjsindex b0cb16a6b4..5633337a74 100644--- a/packages/cli/clients/cli/commands/template.doc.mjs+++ b/packages/cli/clients/cli/commands/template.doc.mjs@@ -109,7 +109,7 @@ export const doc = {},{code: 1,- when: 'unknown or ambiguous template, no source, a path escape, or an existing target without --overwrite',+ when: 'unknown or ambiguous template, no source, a path escape, an existing target without --overwrite, or --list with a type outside page or block or a package that provides no templates',},],related: ['component', 'search', 'discover', 'init'],