From 6ab3bb80e0292d29869fbc2f629135cd6fd382e5 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Fri, 19 Jul 2024 16:24:45 -0400 Subject: [PATCH] fix(core): allow overriding NX_PARALLEL with --parallel --- .../command-line/release/command-object.ts | 26 +++---------- .../target-project-locator.spec.ts | 11 +++--- .../nx/src/utils/command-line-utils.spec.ts | 39 +++++++++++++++++++ packages/nx/src/utils/command-line-utils.ts | 21 ++++++---- 4 files changed, 63 insertions(+), 34 deletions(-) diff --git a/packages/nx/src/command-line/release/command-object.ts b/packages/nx/src/command-line/release/command-object.ts index 9ccce78640359..7a66e50bfc151 100644 --- a/packages/nx/src/command-line/release/command-object.ts +++ b/packages/nx/src/command-line/release/command-object.ts @@ -10,6 +10,7 @@ import { withRunManyOptions, } from '../yargs-utils/shared-options'; import { VersionData } from './utils/shared'; +import { readParallelFromArgsAndEnv } from '../../utils/command-line-utils'; export interface NxReleaseArgs { groups?: string[]; @@ -355,27 +356,10 @@ const planCommand: CommandModule = { }; function coerceParallelOption(args: any) { - if (args['parallel'] === 'false' || args['parallel'] === false) { - return { - ...args, - parallel: 1, - }; - } else if ( - args['parallel'] === 'true' || - args['parallel'] === true || - args['parallel'] === '' - ) { - return { - ...args, - parallel: Number(args['maxParallel'] || args['max-parallel'] || 3), - }; - } else if (args['parallel'] !== undefined) { - return { - ...args, - parallel: Number(args['parallel']), - }; - } - return args; + return { + ...args, + parallel: readParallelFromArgsAndEnv(args), + }; } function withGitCommitAndGitTagOptions( diff --git a/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.spec.ts b/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.spec.ts index cded6c227b971..16199daf5b8d2 100644 --- a/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.spec.ts +++ b/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.spec.ts @@ -864,10 +864,11 @@ describe('TargetProjectLocator', () => { }); describe('isBuiltinModuleImport()', () => { - it('should return true for all node builtin modules', () => { - const allBuiltinModules = require('node:module').builtinModules; - allBuiltinModules.forEach((builtinModule) => { + const allBuiltinModules = require('node:module').builtinModules; + it.each(allBuiltinModules)( + `should return true for %s builtin module`, + (builtinModule) => { expect(isBuiltinModuleImport(builtinModule)).toBe(true); - }); - }); + } + ); }); diff --git a/packages/nx/src/utils/command-line-utils.spec.ts b/packages/nx/src/utils/command-line-utils.spec.ts index d5b66eefbb4b6..50436fd0ef87d 100644 --- a/packages/nx/src/utils/command-line-utils.spec.ts +++ b/packages/nx/src/utils/command-line-utils.spec.ts @@ -473,5 +473,44 @@ describe('splitArgs', () => { expect(parallel).toEqual(5); }); + + it('should be able to be specified in the environment', () => { + const { nxArgs } = withEnvironment( + { + NX_PARALLEL: '5', + }, + () => + splitArgsIntoNxArgsAndOverrides( + { + $0: '', + __overrides_unparsed__: [], + }, + 'affected', + {} as any, + {} as any + ) + ); + expect(nxArgs.parallel).toEqual(5); + }); + + it('should be able to override NX_PARALLEL with the parallel flag', () => { + const { nxArgs } = withEnvironment( + { + NX_PARALLEL: '5', + }, + () => + splitArgsIntoNxArgsAndOverrides( + { + $0: '', + __overrides_unparsed__: [], + parallel: '3', + }, + 'affected', + {} as any, + {} as any + ) + ); + expect(nxArgs.parallel).toEqual(3); + }); }); }); diff --git a/packages/nx/src/utils/command-line-utils.ts b/packages/nx/src/utils/command-line-utils.ts index c6241de5f809b..11928b3129792 100644 --- a/packages/nx/src/utils/command-line-utils.ts +++ b/packages/nx/src/utils/command-line-utils.ts @@ -177,25 +177,30 @@ export function splitArgsIntoNxArgsAndOverrides( normalizeNxArgsRunner(nxArgs, nxJson, options); + nxArgs['parallel'] = readParallelFromArgsAndEnv(args); + + return { nxArgs, overrides } as any; +} + +export function readParallelFromArgsAndEnv(args: { [k: string]: any }) { if (args['parallel'] === 'false' || args['parallel'] === false) { - nxArgs['parallel'] = 1; + return 1; } else if ( args['parallel'] === 'true' || args['parallel'] === true || args['parallel'] === '' || - process.env.NX_PARALLEL // dont require passing --parallel if NX_PARALLEL is set + // dont require passing --parallel if NX_PARALLEL is set, but allow overriding it + (process.env.NX_PARALLEL && args['parallel'] === undefined) ) { - nxArgs['parallel'] = Number( - nxArgs['maxParallel'] || - nxArgs['max-parallel'] || + return Number( + args['maxParallel'] || + args['max-parallel'] || process.env.NX_PARALLEL || 3 ); } else if (args['parallel'] !== undefined) { - nxArgs['parallel'] = Number(args['parallel']); + return Number(args['parallel']); } - - return { nxArgs, overrides } as any; } function normalizeNxArgsRunner(