diff --git a/docs/shared/reference/environment-variables.md b/docs/shared/reference/environment-variables.md index a3479d838c7ba..d1dc04c61f65e 100644 --- a/docs/shared/reference/environment-variables.md +++ b/docs/shared/reference/environment-variables.md @@ -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. diff --git a/packages/nx/src/command-line/affected/affected.ts b/packages/nx/src/command-line/affected/affected.ts index 3b24f70a8e130..24be3c9048648 100644 --- a/packages/nx/src/command-line/affected/affected.ts +++ b/packages/nx/src/command-line/affected/affected.ts @@ -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 { performance.mark('code-loading:end'); performance.measure('code-loading', 'init-local', 'code-loading:end'); @@ -84,7 +91,7 @@ export async function affected( overrides, null, extraTargetDependencies, - { excludeTaskDependencies: false, loadDotEnvFiles: true } + extraOptions ); process.exit(status); } diff --git a/packages/nx/src/command-line/release/publish.ts b/packages/nx/src/command-line/release/publish.ts index b42a9180a7fc3..08daf70787409 100644 --- a/packages/nx/src/command-line/release/publish.ts +++ b/packages/nx/src/command-line/release/publish.ts @@ -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; @@ -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; @@ -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 { const projectsToRun: ProjectGraphProjectNode[] = projectNames.map( (projectName) => projectGraph.nodes[projectName] @@ -218,10 +227,7 @@ async function runPublishOnProjects( overrides, null, {}, - { - excludeTaskDependencies: shouldExcludeTaskDependencies, - loadDotEnvFiles: true, - } + extraOptions ); if (status !== 0) { diff --git a/packages/nx/src/command-line/run-many/run-many.ts b/packages/nx/src/command-line/run-many/run-many.ts index f36c50525886d..6b56e5aafce55 100644 --- a/packages/nx/src/command-line/run-many/run-many.ts +++ b/packages/nx/src/command-line/run-many/run-many.ts @@ -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; } diff --git a/packages/nx/src/command-line/run/run-one.ts b/packages/nx/src/command-line/run/run-one.ts index cd376e54904ab..74797bdc083e7 100644 --- a/packages/nx/src/command-line/run/run-one.ts +++ b/packages/nx/src/command-line/run/run-one.ts @@ -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; } diff --git a/packages/nx/src/executors/run-commands/run-commands.impl.ts b/packages/nx/src/executors/run-commands/run-commands.impl.ts index b6f71b2060262..4675de6944f17 100644 --- a/packages/nx/src/executors/run-commands/run-commands.impl.ts +++ b/packages/nx/src/executors/run-commands/run-commands.impl.ts @@ -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) {