Skip to content

Commit

Permalink
fix(core): load env vars before lifecycle for batch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed Jun 21, 2023
1 parent f3f7406 commit 52faa17
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
36 changes: 33 additions & 3 deletions packages/nx/src/tasks-runner/run-command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe('getRunner', () => {

const { tasksRunner, runnerOptions } = getRunner(
{ runner: 'custom' },
nxJson
nxJson,
true
);

expect(tasksRunner).toEqual(mockRunner);
Expand All @@ -49,7 +50,8 @@ describe('getRunner', () => {

const { tasksRunner, runnerOptions } = getRunner(
{ runner: 'custom' },
nxJson
nxJson,
true
);
expect(tasksRunner).toBe(mockRunner);
expect(runnerOptions).toEqual({
Expand All @@ -69,8 +71,36 @@ describe('getRunner', () => {
},
};

const { tasksRunner } = getRunner({}, nxJson);
const { tasksRunner } = getRunner({}, nxJson, true);

expect(tasksRunner).toEqual(mockRunner);
});

it('should set args via env', () => {
jest.mock('custom-default-runner', () => mockRunner, {
virtual: true,
});

nxJson.tasksRunnerOptions = {
default: {
runner: 'custom-default-runner',
},
};
const args: Record<string, any> = {
targets: ['test'],
parallel: 3,
skipNxCache: false,
nxBail: false,
nxIgnoreCycles: false,
all: true,
projects: [],
};
const previousEnv = structuredClone({ ...process.env });
process.env.NX_BATCH_MODE = 'true';
getRunner(args, nxJson, true);
expect(process.env.NX_BATCH_MODE).toEqual('true');
expect(args.outputStyle).toEqual('stream');
process.env = previousEnv;
expect(process.env.NX_BATCH_MODE).toBeUndefined();
});
});
20 changes: 13 additions & 7 deletions packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ async function getTerminalOutputLifeCycle(
tasks: Task[],
nxArgs: NxArgs,
nxJson: NxJsonConfiguration,
overrides: Record<string, unknown>
overrides: Record<string, unknown>,
loadDotEnvFiles: boolean
): Promise<{ lifeCycle: LifeCycle; renderIsDone: Promise<void> }> {
const { runnerOptions } = getRunner(nxArgs, nxJson);
const { runnerOptions } = getRunner(nxArgs, nxJson, loadDotEnvFiles);
const isRunOne = initiatingProject != null;
const useDynamicOutput =
shouldUseDynamicLifeCycle(tasks, runnerOptions, nxArgs.outputStyle) &&
Expand Down Expand Up @@ -170,7 +171,8 @@ export async function runCommand(
tasks,
nxArgs,
nxJson,
overrides
overrides,
extraOptions.loadDotEnvFiles
);

const status = await invokeTasksRunner({
Expand Down Expand Up @@ -229,9 +231,11 @@ export async function invokeTasksRunner({
loadDotEnvFiles: boolean;
initiatingProject: string | null;
}) {
setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles);

const { tasksRunner, runnerOptions } = getRunner(nxArgs, nxJson);
const { tasksRunner, runnerOptions } = getRunner(
nxArgs,
nxJson,
loadDotEnvFiles
);

let hasher;
if (daemonClient.enabled()) {
Expand Down Expand Up @@ -370,11 +374,13 @@ function shouldUseDynamicLifeCycle(

export function getRunner(
nxArgs: NxArgs,
nxJson: NxJsonConfiguration
nxJson: NxJsonConfiguration,
loadDotEnvFiles: boolean
): {
tasksRunner: TasksRunner;
runnerOptions: any;
} {
setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles);
let runner = nxArgs.runner;
runner = runner || 'default';
if (!nxJson.tasksRunnerOptions) {
Expand Down

0 comments on commit 52faa17

Please sign in to comment.