From 569281fff95d8eebfa81a27e673c67ca1bfd5fd2 Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Mon, 16 Nov 2026 12:00:05 -0500 Subject: [PATCH 1/3] fix(cli): start Next.js on Windows with Node 22+ Run the Next.js CLI script with the current Node binary instead of spawning next.cmd, which Node rejects with EINVAL since the CVE-2024-27980 fix. Exit non-zero with an error when Next.js can't be found or fails to start, instead of exiting 0 silently. Fixes #2725 --- packages/faustwp-cli/src/index.ts | 25 +++++++--- .../faustwp-cli/src/utils/getNextBinPath.ts | 28 +++++++++++ packages/faustwp-cli/src/utils/index.ts | 1 + .../tests/utils/getNextBinPath.test.ts | 48 +++++++++++++++++++ 4 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 packages/faustwp-cli/src/utils/getNextBinPath.ts create mode 100644 packages/faustwp-cli/tests/utils/getNextBinPath.test.ts diff --git a/packages/faustwp-cli/src/index.ts b/packages/faustwp-cli/src/index.ts index 99a8e2d73..f0963cbd9 100644 --- a/packages/faustwp-cli/src/index.ts +++ b/packages/faustwp-cli/src/index.ts @@ -1,13 +1,14 @@ #!/usr/bin/env node import { spawnSync } from 'child_process'; import dotenv from 'dotenv-flow'; -import { debugLog } from './stdout/index.js'; +import { debugLog, errorLog } from './stdout/index.js'; import { healthCheck } from './healthCheck/index.js'; import { generatePossibleTypes } from './generatePossibleTypes.js'; import { generateGlobalStylesheet } from './generateGlobalStylesheet.js'; import { blockset } from './blockset.js'; import { getCliArgs, + getNextBinPath, getNextCliArgs, hasWpCredentials, isDebug, @@ -88,12 +89,24 @@ import { marshallTelemetryData, sendTelemetryData } from './telemetry/index.js'; * Spawn a child process using the args captured in argv and continue the * standard i/o for the Next.js CLI. */ - const nextjsCommand = process.platform === 'win32' ? 'next.cmd' : 'next'; + const nextBinPath = getNextBinPath(); - process.exit( - spawnSync(nextjsCommand, getNextCliArgs(), { + if (!nextBinPath) { + errorLog('Could not find Next.js. Make sure `next` is installed.'); + process.exit(1); + } + + const result = spawnSync( + process.execPath, + [nextBinPath, ...getNextCliArgs()], + { stdio: 'inherit', - encoding: 'utf8', - })?.status as number | undefined, + }, ); + + if (result.error) { + errorLog('Failed to start Next.js:', result.error); + } + + process.exit(result.status ?? 1); })(); diff --git a/packages/faustwp-cli/src/utils/getNextBinPath.ts b/packages/faustwp-cli/src/utils/getNextBinPath.ts new file mode 100644 index 000000000..2e32a9ad9 --- /dev/null +++ b/packages/faustwp-cli/src/utils/getNextBinPath.ts @@ -0,0 +1,28 @@ +import fs from 'fs'; +import { createRequire } from 'module'; +import path from 'path'; +import process from 'process'; + +/** + * Resolves the Next.js CLI script from the project's dependencies so it can + * be run with Node directly. Spawning `next.cmd` on Windows fails with EINVAL + * on Node versions patched for CVE-2024-27980. + * + * @returns The absolute path to the Next.js CLI script, or undefined if Next.js + * cannot be resolved. + */ +export const getNextBinPath = (cwd = process.cwd()) => { + try { + const pkgPath = createRequire(path.join(cwd, 'package.json')).resolve( + 'next/package.json', + ); + const { bin } = JSON.parse(fs.readFileSync(pkgPath, 'utf8')) as { + bin?: string | Record; + }; + const binPath = typeof bin === 'string' ? bin : bin?.next; + + return binPath ? path.resolve(path.dirname(pkgPath), binPath) : undefined; + } catch { + return undefined; + } +}; diff --git a/packages/faustwp-cli/src/utils/index.ts b/packages/faustwp-cli/src/utils/index.ts index f62a0fe98..09e221cb4 100644 --- a/packages/faustwp-cli/src/utils/index.ts +++ b/packages/faustwp-cli/src/utils/index.ts @@ -1,5 +1,6 @@ export { getCliArgs } from './getCliArgs.js'; export { getNextCliArgs } from './getNextCliArgs.js'; +export { getNextBinPath } from './getNextBinPath.js'; export { isDebug } from './isDebug.js'; export { getWpSecret } from './getWpSecret.js'; export { diff --git a/packages/faustwp-cli/tests/utils/getNextBinPath.test.ts b/packages/faustwp-cli/tests/utils/getNextBinPath.test.ts new file mode 100644 index 000000000..685a1508b --- /dev/null +++ b/packages/faustwp-cli/tests/utils/getNextBinPath.test.ts @@ -0,0 +1,48 @@ +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import { getNextBinPath } from '../../src/utils'; + +describe('utils/getNextBinPath', () => { + let cwd: string; + + beforeEach(() => { + cwd = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'faust-cli-'))); + }); + + afterEach(() => { + fs.rmSync(cwd, { recursive: true, force: true }); + }); + + const installNext = (bin: unknown) => { + const nextDir = path.join(cwd, 'node_modules', 'next'); + fs.mkdirSync(nextDir, { recursive: true }); + fs.writeFileSync( + path.join(nextDir, 'package.json'), + JSON.stringify({ name: 'next', bin }), + ); + return nextDir; + }; + + it('resolves the next bin from an object bin field', () => { + const nextDir = installNext({ next: './dist/bin/next' }); + + expect(getNextBinPath(cwd)).toBe(path.join(nextDir, 'dist', 'bin', 'next')); + }); + + it('resolves the next bin from a string bin field', () => { + const nextDir = installNext('./dist/bin/next'); + + expect(getNextBinPath(cwd)).toBe(path.join(nextDir, 'dist', 'bin', 'next')); + }); + + it('returns undefined when next is not installed', () => { + expect(getNextBinPath(cwd)).toBeUndefined(); + }); + + it('returns undefined when next has no bin', () => { + installNext(undefined); + + expect(getNextBinPath(cwd)).toBeUndefined(); + }); +}); From 525ce42d91b4392b63c44aa6780ae44173a95ea6 Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Mon, 16 Nov 2026 12:01:15 -0500 Subject: [PATCH 2/3] test(cli): cover how faust starts Next.js and exits Move the spawn into runNext so the exit-code handling behind #2725 can be tested. --- packages/faustwp-cli/src/index.ts | 30 +--------- packages/faustwp-cli/src/runNext.ts | 27 +++++++++ .../faustwp-cli/tests/runNext/runNext.test.ts | 59 +++++++++++++++++++ 3 files changed, 89 insertions(+), 27 deletions(-) create mode 100644 packages/faustwp-cli/src/runNext.ts create mode 100644 packages/faustwp-cli/tests/runNext/runNext.test.ts diff --git a/packages/faustwp-cli/src/index.ts b/packages/faustwp-cli/src/index.ts index f0963cbd9..b000b4ff1 100644 --- a/packages/faustwp-cli/src/index.ts +++ b/packages/faustwp-cli/src/index.ts @@ -1,14 +1,13 @@ #!/usr/bin/env node -import { spawnSync } from 'child_process'; import dotenv from 'dotenv-flow'; -import { debugLog, errorLog } from './stdout/index.js'; +import { debugLog } from './stdout/index.js'; import { healthCheck } from './healthCheck/index.js'; import { generatePossibleTypes } from './generatePossibleTypes.js'; import { generateGlobalStylesheet } from './generateGlobalStylesheet.js'; import { blockset } from './blockset.js'; +import { runNext } from './runNext.js'; import { getCliArgs, - getNextBinPath, getNextCliArgs, hasWpCredentials, isDebug, @@ -85,28 +84,5 @@ import { marshallTelemetryData, sendTelemetryData } from './telemetry/index.js'; printFaustVersion(); - /** - * Spawn a child process using the args captured in argv and continue the - * standard i/o for the Next.js CLI. - */ - const nextBinPath = getNextBinPath(); - - if (!nextBinPath) { - errorLog('Could not find Next.js. Make sure `next` is installed.'); - process.exit(1); - } - - const result = spawnSync( - process.execPath, - [nextBinPath, ...getNextCliArgs()], - { - stdio: 'inherit', - }, - ); - - if (result.error) { - errorLog('Failed to start Next.js:', result.error); - } - - process.exit(result.status ?? 1); + process.exit(runNext(getNextCliArgs())); })(); diff --git a/packages/faustwp-cli/src/runNext.ts b/packages/faustwp-cli/src/runNext.ts new file mode 100644 index 000000000..e3a3bc880 --- /dev/null +++ b/packages/faustwp-cli/src/runNext.ts @@ -0,0 +1,27 @@ +import { spawnSync } from 'child_process'; +import { errorLog } from './stdout/index.js'; +import { getNextBinPath } from './utils/index.js'; + +/** + * Runs the Next.js CLI with the given args, inheriting standard i/o. + * + * @returns The exit code to exit Faust with. + */ +export function runNext(args: string[]): number { + const nextBinPath = getNextBinPath(); + + if (!nextBinPath) { + errorLog('Could not find Next.js. Make sure `next` is installed.'); + return 1; + } + + const result = spawnSync(process.execPath, [nextBinPath, ...args], { + stdio: 'inherit', + }); + + if (result.error) { + errorLog('Failed to start Next.js:', result.error); + } + + return result.status ?? 1; +} diff --git a/packages/faustwp-cli/tests/runNext/runNext.test.ts b/packages/faustwp-cli/tests/runNext/runNext.test.ts new file mode 100644 index 000000000..8f720a499 --- /dev/null +++ b/packages/faustwp-cli/tests/runNext/runNext.test.ts @@ -0,0 +1,59 @@ +import { spawnSync, SpawnSyncReturns } from 'child_process'; +jest.mock('child_process'); +import { getNextBinPath } from '../../src/utils/getNextBinPath.js'; +jest.mock('../../src/utils/getNextBinPath.js', () => ({ + __esModule: true, + getNextBinPath: jest.fn(), +})); + +import { runNext } from '../../src/runNext'; + +const spawnSyncMock = spawnSync as unknown as jest.Mock< + Partial> +>; +const getNextBinPathMock = getNextBinPath as jest.Mock; + +describe('runNext', () => { + beforeEach(() => { + jest.spyOn(console, 'log').mockImplementation(() => {}); + getNextBinPathMock.mockReturnValue('/app/node_modules/next/dist/bin/next'); + }); + + afterEach(() => { + jest.restoreAllMocks(); + jest.clearAllMocks(); + }); + + it('runs the Next.js CLI script with the current Node binary', () => { + spawnSyncMock.mockReturnValueOnce({ status: 0 }); + + expect(runNext(['dev', '-p', '4040'])).toBe(0); + expect(spawnSyncMock).toHaveBeenCalledWith( + process.execPath, + ['/app/node_modules/next/dist/bin/next', 'dev', '-p', '4040'], + { stdio: 'inherit' }, + ); + }); + + it("returns the Next.js CLI's exit code", () => { + spawnSyncMock.mockReturnValueOnce({ status: 2 }); + + expect(runNext(['build'])).toBe(2); + }); + + it('returns 1 when Next.js cannot be found', () => { + getNextBinPathMock.mockReturnValueOnce(undefined); + + expect(runNext(['dev'])).toBe(1); + expect(spawnSyncMock).not.toHaveBeenCalled(); + }); + + it('returns 1 when the process fails to start', () => { + spawnSyncMock.mockReturnValueOnce({ + status: null, + error: new Error('spawnSync EINVAL'), + }); + + expect(runNext(['dev'])).toBe(1); + }); +}); From 5508361cb198f90bcf82a8c81abe029c838af79a Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Mon, 16 Nov 2026 12:02:19 -0500 Subject: [PATCH 3/3] refactor(cli): only read Next.js's object bin field Next.js has always published bin as an object. --- packages/faustwp-cli/src/utils/getNextBinPath.ts | 4 ++-- packages/faustwp-cli/tests/utils/getNextBinPath.test.ts | 8 +------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/faustwp-cli/src/utils/getNextBinPath.ts b/packages/faustwp-cli/src/utils/getNextBinPath.ts index 2e32a9ad9..fd7047c4a 100644 --- a/packages/faustwp-cli/src/utils/getNextBinPath.ts +++ b/packages/faustwp-cli/src/utils/getNextBinPath.ts @@ -17,9 +17,9 @@ export const getNextBinPath = (cwd = process.cwd()) => { 'next/package.json', ); const { bin } = JSON.parse(fs.readFileSync(pkgPath, 'utf8')) as { - bin?: string | Record; + bin?: { next?: string }; }; - const binPath = typeof bin === 'string' ? bin : bin?.next; + const binPath = bin?.next; return binPath ? path.resolve(path.dirname(pkgPath), binPath) : undefined; } catch { diff --git a/packages/faustwp-cli/tests/utils/getNextBinPath.test.ts b/packages/faustwp-cli/tests/utils/getNextBinPath.test.ts index 685a1508b..513de95a4 100644 --- a/packages/faustwp-cli/tests/utils/getNextBinPath.test.ts +++ b/packages/faustwp-cli/tests/utils/getNextBinPath.test.ts @@ -24,18 +24,12 @@ describe('utils/getNextBinPath', () => { return nextDir; }; - it('resolves the next bin from an object bin field', () => { + it('resolves the next bin', () => { const nextDir = installNext({ next: './dist/bin/next' }); expect(getNextBinPath(cwd)).toBe(path.join(nextDir, 'dist', 'bin', 'next')); }); - it('resolves the next bin from a string bin field', () => { - const nextDir = installNext('./dist/bin/next'); - - expect(getNextBinPath(cwd)).toBe(path.join(nextDir, 'dist', 'bin', 'next')); - }); - it('returns undefined when next is not installed', () => { expect(getNextBinPath(cwd)).toBeUndefined(); });