Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): load env vars before lifecycle for batch mode #17713

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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