Skip to content

Commit

Permalink
fix(core): not load env files when NX_LOAD_DOT_ENV_FILES is false (#2…
Browse files Browse the repository at this point in the history
…3231)

<!-- 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)
  • Loading branch information
xiongemi authored and FrozenPandaz committed May 14, 2024
1 parent 9186c97 commit be5b8e7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/shared/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The following environment variables are ones that you can set to change the beha
| NX_BATCH_MODE | boolean | If set to `true`, Nx will run task(s) in batches for executors which support batches. |
| NX_SKIP_LOG_GROUPING | boolean | If set to `true`, Nx will not group command's logs on CI. |
| NX_MIGRATE_CLI_VERSION | string | The version of Nx to use for running the `nx migrate` command. If not set, it defaults to `latest`. |
| NX_LOAD_DOT_ENV_FILES | boolean | If set to 'false', Nx will not load any environment files (e.g. `.local.env`, `.env.local`) |

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

Expand Down
11 changes: 9 additions & 2 deletions packages/nx/src/command-line/affected/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ export async function affected(
extraTargetDependencies: Record<
string,
(TargetDependencyConfig | string)[]
> = {}
> = {},
extraOptions = {
excludeTaskDependencies: false,
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
} as {
excludeTaskDependencies: boolean;
loadDotEnvFiles: boolean;
}
): Promise<void> {
performance.mark('code-loading:end');
performance.measure('code-loading', 'init-local', 'code-loading:end');
Expand Down Expand Up @@ -84,7 +91,7 @@ export async function affected(
overrides,
null,
extraTargetDependencies,
{ excludeTaskDependencies: false, loadDotEnvFiles: true }
extraOptions
);
process.exit(status);
}
Expand Down
26 changes: 16 additions & 10 deletions packages/nx/src/command-line/release/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export async function releasePublish(
projectGraph,
nxJson,
Array.from(releaseGroupToFilteredProjects.get(releaseGroup)),
shouldExcludeTaskDependencies,
isCLI
isCLI,
{
excludeTaskDependencies: shouldExcludeTaskDependencies,
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
}
);
if (status !== 0) {
overallExitStatus = status || 1;
Expand All @@ -113,8 +116,11 @@ export async function releasePublish(
projectGraph,
nxJson,
releaseGroup.projects,
shouldExcludeTaskDependencies,
isCLI
isCLI,
{
excludeTaskDependencies: shouldExcludeTaskDependencies,
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
}
);
if (status !== 0) {
overallExitStatus = status || 1;
Expand All @@ -129,8 +135,11 @@ async function runPublishOnProjects(
projectGraph: ProjectGraph,
nxJson: NxJsonConfiguration,
projectNames: string[],
shouldExcludeTaskDependencies: boolean,
isCLI: boolean
isCLI: boolean,
extraOptions: {
excludeTaskDependencies: boolean;
loadDotEnvFiles: boolean;
}
): Promise<number> {
const projectsToRun: ProjectGraphProjectNode[] = projectNames.map(
(projectName) => projectGraph.nodes[projectName]
Expand Down Expand Up @@ -218,10 +227,7 @@ async function runPublishOnProjects(
overrides,
null,
{},
{
excludeTaskDependencies: shouldExcludeTaskDependencies,
loadDotEnvFiles: true,
}
extraOptions
);

if (status !== 0) {
Expand Down
5 changes: 4 additions & 1 deletion packages/nx/src/command-line/run-many/run-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export async function runMany(
string,
(TargetDependencyConfig | string)[]
> = {},
extraOptions = { excludeTaskDependencies: false, loadDotEnvFiles: true } as {
extraOptions = {
excludeTaskDependencies: false,
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
} as {
excludeTaskDependencies: boolean;
loadDotEnvFiles: boolean;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/nx/src/command-line/run/run-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export async function runOne(
string,
(TargetDependencyConfig | string)[]
> = {},
extraOptions = { excludeTaskDependencies: false, loadDotEnvFiles: true } as {
extraOptions = {
excludeTaskDependencies: false,
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
} as {
excludeTaskDependencies: boolean;
loadDotEnvFiles: boolean;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/nx/src/executors/run-commands/run-commands.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export default async function (
terminalOutput: string;
}> {
registerProcessListener();
await loadEnvVars(options.envFile);
if (process.env.NX_LOAD_DOT_ENV_FILES !== 'false') {
await loadEnvVars(options.envFile);
}
const normalized = normalizeOptions(options);

if (options.readyWhen && !options.parallel) {
Expand Down

0 comments on commit be5b8e7

Please sign in to comment.