Skip to content

Commit

Permalink
fix(core): not load env files when NX_LOAD_DOT_ENV_FILES is false
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed May 8, 2024
1 parent dc20a3b commit c195d63
Show file tree
Hide file tree
Showing 6 changed files with 40 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 @@ -32,7 +32,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 @@ -112,7 +119,7 @@ export async function affected(
overrides,
null,
extraTargetDependencies,
{ excludeTaskDependencies: false, loadDotEnvFiles: true }
extraOptions
);
process.exit(status);
}
Expand Down
29 changes: 19 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,14 @@ async function runPublishOnProjects(
projectGraph: ProjectGraph,
nxJson: NxJsonConfiguration,
projectNames: string[],
shouldExcludeTaskDependencies: boolean,
isCLI: boolean
isCLI: boolean,
extraOptions = {
excludeTaskDependencies: false,
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
} as {
excludeTaskDependencies: boolean;
loadDotEnvFiles: boolean;
}
): Promise<number> {
const projectsToRun: ProjectGraphProjectNode[] = projectNames.map(
(projectName) => projectGraph.nodes[projectName]
Expand Down Expand Up @@ -218,10 +230,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 c195d63

Please sign in to comment.