An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): integration verify starts npm the way the rest of the CLI does (#8019)
Joey6b350a2a3421
diff --git a/.changeset/cli-verify-npm-command.md b/.changeset/cli-verify-npm-command.mdnew file mode 100644index 0000000000..76b5843060--- /dev/null+++ b/.changeset/cli-verify-npm-command.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] `astryx integration verify` starts npm the way the rest of the CLI does: npm's own entry file, run with the Node that runs the CLI, when one is found. An `npm` earlier on the PATH that is not npm itself, such as a wrapper that refuses npm commands, no longer fails the check with "npm pack failed".++@josephfarinadiff --git a/packages/cli/api/integration/pack-check.lifecycle-output.test.mjs b/packages/cli/api/integration/pack-check.lifecycle-output.test.mjsindex cc74cee81e..fbe5e0476c 100644--- a/packages/cli/api/integration/pack-check.lifecycle-output.test.mjs+++ b/packages/cli/api/integration/pack-check.lifecycle-output.test.mjs@@ -1,6 +1,6 @@// Copyright (c) Meta Platforms, Inc. and affiliates.-import {afterEach, beforeEach, describe, expect, it} from 'vitest';+import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';import {spawnSync} from 'node:child_process';import * as fs from 'node:fs';import * as path from 'node:path';@@ -123,4 +123,27 @@ describe('integrationPackCheck with lifecycle script output', () => {expect(failure?.message).toContain('npm pack failed (exit 3)');expect(failure?.message).toContain('prepack exploded');});++ it.skipIf(process.platform === 'win32')(+ 'packs with npm itself when an earlier npm on the PATH refuses every command',+ async () => {+ writeThemePackage({});+ const refusing = path.join(tmpDir, '.refusing-bin');+ fs.mkdirSync(refusing);+ fs.writeFileSync(+ path.join(refusing, 'npm'),+ "#!/bin/sh\necho 'npm commands are not allowed on this machine'\nexit 1\n",+ {mode: 0o755},+ );+ vi.stubEnv('PATH', `${refusing}${path.delimiter}${process.env.PATH}`);+ try {+ const result = await integrationPackCheck({cwd: tmpDir});++ expect(result.data.issues).toEqual([]);+ expect(result.data.packable).toBe(true);+ } finally {+ vi.unstubAllEnvs();+ }+ },+ );});diff --git a/packages/cli/api/integration/pack-check.mjs b/packages/cli/api/integration/pack-check.mjsindex 621599aecd..3118e84439 100644--- a/packages/cli/api/integration/pack-check.mjs+++ b/packages/cli/api/integration/pack-check.mjs@@ -25,6 +25,7 @@ import * as os from 'node:os';import {fileURLToPath} from 'node:url';import {spawnSync} from 'node:child_process';import {assertWithin} from '../../foundation/fs/path-safety.mjs';+import {npmCommand} from '../../foundation/release/cache.mjs';import {commandTokensIn} from '../../foundation/doc-compiler/doc-syntax.mjs';import {resolvePackageDir} from '../../foundation/integrations/integrations.mjs';import {@@ -265,17 +266,22 @@ function npmPackErrorDetail(output) {/*** Run `npm pack --json` with output directed to `destDir` so no preexisting* tgz is overwritten. This intentionally runs the package lifecycle, matching- * the artifact `npm publish` would produce. Uses spawnSync with an args array —- * no shell — so paths with metacharacters cannot inject.+ * the artifact `npm publish` would produce. npm starts the way the CLI starts+ * it everywhere else (`npmCommand`): npm's own entry file run with this Node+ * when one is found, so an `npm` earlier on the PATH that is not npm itself+ * cannot answer for it. Uses spawnSync with an args array — no shell — so+ * paths with metacharacters cannot inject.** @param {string} packageDir* @param {string} destDir* @returns {{filename: string, fileCount: number, size: number, unpackedSize: number, packedPaths: Set<string>}}*/function runNpmPack(packageDir, destDir) {+ const npm = npmCommand();const result = spawnSync(- 'npm',+ npm.command,[+ ...npm.prefix,'pack','--json','--silent',@@ -284,7 +290,7 @@ function runNpmPack(packageDir, destDir) {'--foreground-scripts=false',`--pack-destination=${destDir}`,],- {cwd: packageDir, encoding: 'utf-8', timeout: 60_000},+ {cwd: packageDir, encoding: 'utf-8', shell: false, timeout: 60_000},);if (result.error) {throw new Error(`Could not start npm pack: ${result.error.message}`);diff --git a/packages/cli/api/integration/pack-check.test.mjs b/packages/cli/api/integration/pack-check.test.mjsindex 5d1faea9cd..c8a21e516b 100644--- a/packages/cli/api/integration/pack-check.test.mjs+++ b/packages/cli/api/integration/pack-check.test.mjs@@ -2125,10 +2125,13 @@ describe('integrationPackCheck', () => {).toThrow(/without a files array/);});- it('reports npm missing from PATH as pack_failed', async () => {+ it('reports pack_failed when no npm can be found', async () => {writePackage({files: ['astryx.integration.mjs', 'themes']});const originalPath = process.env.PATH;+ const originalExecPath = process.execPath;process.env.PATH = '';+ // A Node with no npm beside it, and no npm on the PATH.+ process.execPath = path.join(tmpDir, 'no-npm', 'bin', 'node');try {const result = await integrationPackCheck({cwd: tmpDir});expect(result.data.packable).toBe(false);@@ -2140,6 +2143,7 @@ describe('integrationPackCheck', () => {);} finally {process.env.PATH = originalPath;+ process.execPath = originalExecPath;}});