Skip to content

Commit be5b8e7

Browse files
xiongemiFrozenPandaz
authored andcommitted
fix(core): not load env files when NX_LOAD_DOT_ENV_FILES is false (#23231)
<!-- 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` --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #23090 (cherry picked from commit 9122b85)
1 parent 9186c97 commit be5b8e7

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

docs/shared/reference/environment-variables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The following environment variables are ones that you can set to change the beha
2929
| NX_BATCH_MODE | boolean | If set to `true`, Nx will run task(s) in batches for executors which support batches. |
3030
| NX_SKIP_LOG_GROUPING | boolean | If set to `true`, Nx will not group command's logs on CI. |
3131
| NX_MIGRATE_CLI_VERSION | string | The version of Nx to use for running the `nx migrate` command. If not set, it defaults to `latest`. |
32+
| NX_LOAD_DOT_ENV_FILES | boolean | If set to 'false', Nx will not load any environment files (e.g. `.local.env`, `.env.local`) |
3233

3334
Nx will set the following environment variables so they can be accessible within the process even outside of executors and generators.
3435

packages/nx/src/command-line/affected/affected.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ export async function affected(
3030
extraTargetDependencies: Record<
3131
string,
3232
(TargetDependencyConfig | string)[]
33-
> = {}
33+
> = {},
34+
extraOptions = {
35+
excludeTaskDependencies: false,
36+
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
37+
} as {
38+
excludeTaskDependencies: boolean;
39+
loadDotEnvFiles: boolean;
40+
}
3441
): Promise<void> {
3542
performance.mark('code-loading:end');
3643
performance.measure('code-loading', 'init-local', 'code-loading:end');
@@ -84,7 +91,7 @@ export async function affected(
8491
overrides,
8592
null,
8693
extraTargetDependencies,
87-
{ excludeTaskDependencies: false, loadDotEnvFiles: true }
94+
extraOptions
8895
);
8996
process.exit(status);
9097
}

packages/nx/src/command-line/release/publish.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ export async function releasePublish(
9393
projectGraph,
9494
nxJson,
9595
Array.from(releaseGroupToFilteredProjects.get(releaseGroup)),
96-
shouldExcludeTaskDependencies,
97-
isCLI
96+
isCLI,
97+
{
98+
excludeTaskDependencies: shouldExcludeTaskDependencies,
99+
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
100+
}
98101
);
99102
if (status !== 0) {
100103
overallExitStatus = status || 1;
@@ -113,8 +116,11 @@ export async function releasePublish(
113116
projectGraph,
114117
nxJson,
115118
releaseGroup.projects,
116-
shouldExcludeTaskDependencies,
117-
isCLI
119+
isCLI,
120+
{
121+
excludeTaskDependencies: shouldExcludeTaskDependencies,
122+
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
123+
}
118124
);
119125
if (status !== 0) {
120126
overallExitStatus = status || 1;
@@ -129,8 +135,11 @@ async function runPublishOnProjects(
129135
projectGraph: ProjectGraph,
130136
nxJson: NxJsonConfiguration,
131137
projectNames: string[],
132-
shouldExcludeTaskDependencies: boolean,
133-
isCLI: boolean
138+
isCLI: boolean,
139+
extraOptions: {
140+
excludeTaskDependencies: boolean;
141+
loadDotEnvFiles: boolean;
142+
}
134143
): Promise<number> {
135144
const projectsToRun: ProjectGraphProjectNode[] = projectNames.map(
136145
(projectName) => projectGraph.nodes[projectName]
@@ -218,10 +227,7 @@ async function runPublishOnProjects(
218227
overrides,
219228
null,
220229
{},
221-
{
222-
excludeTaskDependencies: shouldExcludeTaskDependencies,
223-
loadDotEnvFiles: true,
224-
}
230+
extraOptions
225231
);
226232

227233
if (status !== 0) {

packages/nx/src/command-line/run-many/run-many.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ export async function runMany(
2525
string,
2626
(TargetDependencyConfig | string)[]
2727
> = {},
28-
extraOptions = { excludeTaskDependencies: false, loadDotEnvFiles: true } as {
28+
extraOptions = {
29+
excludeTaskDependencies: false,
30+
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
31+
} as {
2932
excludeTaskDependencies: boolean;
3033
loadDotEnvFiles: boolean;
3134
}

packages/nx/src/command-line/run/run-one.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ export async function runOne(
2727
string,
2828
(TargetDependencyConfig | string)[]
2929
> = {},
30-
extraOptions = { excludeTaskDependencies: false, loadDotEnvFiles: true } as {
30+
extraOptions = {
31+
excludeTaskDependencies: false,
32+
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
33+
} as {
3134
excludeTaskDependencies: boolean;
3235
loadDotEnvFiles: boolean;
3336
}

packages/nx/src/executors/run-commands/run-commands.impl.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ export default async function (
106106
terminalOutput: string;
107107
}> {
108108
registerProcessListener();
109-
await loadEnvVars(options.envFile);
109+
if (process.env.NX_LOAD_DOT_ENV_FILES !== 'false') {
110+
await loadEnvVars(options.envFile);
111+
}
110112
const normalized = normalizeOptions(options);
111113

112114
if (options.readyWhen && !options.parallel) {

0 commit comments

Comments
 (0)