diff --git a/e2e/nx-run/src/run.test.ts b/e2e/nx-run/src/run.test.ts index 02417680b415e..8c9d91d181a1c 100644 --- a/e2e/nx-run/src/run.test.ts +++ b/e2e/nx-run/src/run.test.ts @@ -190,6 +190,27 @@ describe('Nx Running Tests', () => { expect(output).toContain(app); }); }); + + it('should pass env option to nx:run-commands executor', () => { + const mylib = uniq('mylib'); + runCLI(`generate @nx/js:lib ${mylib}`); + + updateJson(`libs/${mylib}/project.json`, (c) => { + c.targets['echo'] = { + executor: 'nx:run-commands', + options: { + command: 'node -e "console.log(process.env.ONE)"', + env: { + ONE: 'TWO', + }, + }, + }; + return c; + }); + + const output = runCLI(`echo ${mylib}`); + expect(output).toContain('TWO'); + }); }); describe('Nx Bail', () => { diff --git a/packages/nx/src/tasks-runner/task-orchestrator.ts b/packages/nx/src/tasks-runner/task-orchestrator.ts index d0cfb832da674..4f2dcea1ed847 100644 --- a/packages/nx/src/tasks-runner/task-orchestrator.ts +++ b/packages/nx/src/tasks-runner/task-orchestrator.ts @@ -351,7 +351,7 @@ export class TaskOrchestrator { const temporaryOutputPath = this.cache.temporaryOutputPath(task); const streamOutput = shouldStreamOutput(task, this.initiatingProject); - const env = pipeOutput + let env = pipeOutput ? getEnvVariablesForTask( task, taskSpecificEnv, @@ -403,6 +403,12 @@ export class TaskOrchestrator { relative(task.projectRoot ?? workspaceRoot, process.cwd()), process.env.NX_VERBOSE_LOGGING === 'true' ); + if (combinedOptions.env) { + env = { + ...env, + ...combinedOptions.env, + }; + } if (streamOutput) { const args = getPrintableCommandArgsForTask(task); output.logCommand(args.join(' ')); diff --git a/packages/nx/src/utils/params.ts b/packages/nx/src/utils/params.ts index 701698e7fb48c..e83699d5571f3 100644 --- a/packages/nx/src/utils/params.ts +++ b/packages/nx/src/utils/params.ts @@ -306,7 +306,7 @@ export function validateObject( ) { Object.keys(opts).find((p) => { if ( - Object.keys(schema.properties).indexOf(p) === -1 && + Object.keys(schema.properties ?? {}).indexOf(p) === -1 && (!schema.patternProperties || !Object.keys(schema.patternProperties).some((pattern) => new RegExp(pattern).test(p)