diff --git a/e2e/nx-run/src/run.test.ts b/e2e/nx-run/src/run.test.ts index 02417680b415e2..4f87736f3b55fe 100644 --- a/e2e/nx-run/src/run.test.ts +++ b/e2e/nx-run/src/run.test.ts @@ -190,6 +190,28 @@ describe('Nx Running Tests', () => { expect(output).toContain(app); }); }); + + it('should pass env option to nx:run-commands executor', () => { + const myapp = uniq('myapp'); + runCLI(`generate @nx/js:lib ${myapp}`); + + updateJson(`libs/${myapp}/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 ${myapp}`); + console.log('OUTPUT', output); + 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 d0cfb832da6747..4f2dcea1ed8472 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(' '));