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): not load env files when NX_LOAD_DOT_ENV_FILES is false #23231

Merged
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
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 = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update run-many and affected please?

excludeTaskDependencies: false,
loadDotEnvFiles: process.env.NX_LOAD_DOT_ENV_FILES !== 'false',
} as {
excludeTaskDependencies: boolean;
loadDotEnvFiles: boolean;
}
Expand Down
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