Skip to content

Commit 053fc67

Browse files
authored
fix(core): ensure task environments are processed properly in dte (#30862)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> The new DTE APIs are missing a few things related to task environments. Firstly, even though it processes task envs.. they are not stored on the `processedTasks` map. Thus, the task env is not actually used to run tasks. Secondly, some environment variables are not set during dte.. which used to. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> The.2 things that are missing are now back. The map is populated with the task envs and the environment variables are set based on the args. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent 4323302 commit 053fc67

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

packages/nx/src/tasks-runner/init-tasks-runner.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { NxJsonConfiguration } from '../config/nx-json';
12
import { readNxJson } from '../config/nx-json';
23
import { NxArgs } from '../utils/command-line-utils';
34
import { createProjectGraphAsync } from '../project-graph/project-graph';
@@ -6,6 +7,7 @@ import {
67
constructLifeCycles,
78
getRunner,
89
invokeTasksRunner,
10+
setEnvVarsBasedOnArgs,
911
} from './run-command';
1012
import { InvokeRunnerTerminalOutputLifeCycle } from './life-cycles/invoke-runner-terminal-output-life-cycle';
1113
import { performance } from 'perf_hooks';
@@ -15,7 +17,6 @@ import { CompositeLifeCycle, LifeCycle, TaskResult } from './life-cycle';
1517
import { TaskOrchestrator } from './task-orchestrator';
1618
import { createTaskHasher } from '../hasher/create-task-hasher';
1719
import type { ProjectGraph } from '../config/project-graph';
18-
import type { NxJsonConfiguration } from '../config/nx-json';
1920
import { daemonClient } from '../daemon/client/client';
2021
import { RunningTask } from './running-tasks/running-task';
2122
import { TaskResultsLifeCycle } from './life-cycles/task-results-life-cycle';
@@ -133,14 +134,21 @@ async function createOrchestrator(
133134
}, {} as any),
134135
};
135136

137+
const nxArgs = {
138+
...options,
139+
parallel: tasks.length,
140+
lifeCycle: compositedLifeCycle,
141+
};
142+
setEnvVarsBasedOnArgs(nxArgs, true);
143+
136144
const orchestrator = new TaskOrchestrator(
137145
hasher,
138146
null,
139147
[],
140148
projectGraph,
141149
taskGraph,
142150
nxJson,
143-
{ ...options, parallel: tasks.length, lifeCycle: compositedLifeCycle },
151+
nxArgs,
144152
false,
145153
daemonClient,
146154
undefined,
@@ -149,7 +157,7 @@ async function createOrchestrator(
149157

150158
await orchestrator.init();
151159

152-
await Promise.all(tasks.map((task) => orchestrator.processTask(task.id)));
160+
orchestrator.processTasks(tasks.map((task) => task.id));
153161

154162
return orchestrator;
155163
}

packages/nx/src/tasks-runner/run-command.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,10 @@ async function confirmRunningTasksWithSyncFailures(): Promise<void> {
802802
}
803803
}
804804

805-
function setEnvVarsBasedOnArgs(nxArgs: NxArgs, loadDotEnvFiles: boolean) {
805+
export function setEnvVarsBasedOnArgs(
806+
nxArgs: NxArgs,
807+
loadDotEnvFiles: boolean
808+
) {
806809
if (
807810
nxArgs.outputStyle == 'stream' ||
808811
process.env.NX_BATCH_MODE === 'true' ||

packages/nx/src/tasks-runner/task-orchestrator.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,17 @@ export class TaskOrchestrator {
199199
);
200200
}
201201

202+
processTasks(taskIds: string[]) {
203+
for (const taskId of taskIds) {
204+
// Task is already handled or being handled
205+
if (!this.processedTasks.has(taskId)) {
206+
this.processedTasks.set(taskId, this.processTask(taskId));
207+
}
208+
}
209+
}
210+
202211
// region Processing Scheduled Tasks
203-
async processTask(taskId: string): Promise<NodeJS.ProcessEnv> {
212+
private async processTask(taskId: string): Promise<NodeJS.ProcessEnv> {
204213
const task = this.taskGraph.tasks[taskId];
205214
const taskSpecificEnv = getTaskSpecificEnv(task);
206215

@@ -245,12 +254,7 @@ export class TaskOrchestrator {
245254
for (const batch of scheduledBatches) {
246255
this.processedBatches.set(batch, this.processScheduledBatch(batch));
247256
}
248-
for (const taskId of scheduledTasks) {
249-
// Task is already handled or being handled
250-
if (!this.processedTasks.has(taskId)) {
251-
this.processedTasks.set(taskId, this.processTask(taskId));
252-
}
253-
}
257+
this.processTasks(scheduledTasks);
254258
}
255259

256260
// endregion Processing Scheduled Tasks

0 commit comments

Comments
 (0)