From a53be8f744a645d1b8986f8dc1d48ceeb0b66b46 Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Wed, 31 May 2023 11:54:31 -0400 Subject: [PATCH] feat(core): load environment variables from configuration name --- .../guides/define-environment-variables.md | 28 ++++++++------- e2e/nx-run/src/run.test.ts | 11 +++++- .../forked-process-task-runner.ts | 36 ++++++++++++------- packages/nx/src/utils/project-graph-utils.ts | 18 +++++++--- 4 files changed, 64 insertions(+), 29 deletions(-) diff --git a/docs/shared/guides/define-environment-variables.md b/docs/shared/guides/define-environment-variables.md index 7f9f33e1177741..e2dfd9125e5d7d 100644 --- a/docs/shared/guides/define-environment-variables.md +++ b/docs/shared/guides/define-environment-variables.md @@ -11,18 +11,22 @@ By default, Nx will load any environment variables you place in the following fi 1. `apps/my-app/.env.[target-name].[configuration-name]` 2. `apps/my-app/.[target-name].[configuration-name].env` -3. `apps/my-app/.env.[target-name]` -4. `apps/my-app/.[target-name].env` -5. `apps/my-app/.env.local` -6. `apps/my-app/.local.env` -7. `apps/my-app/.env` -8. `.env.[target-name].[configuration-name]` -9. `.[target-name].[configuration-name].env` -10. `.env.[target-name]` -11. `.[target-name].env` -12. `.local.env` -13. `.env.local` -14. `.env` +3. `apps/my-app/.env.[configuration-name]` +4. `apps/my-app/.[configuration-name].env` +5. `apps/my-app/.env.[target-name]` +6. `apps/my-app/.[target-name].env` +7. `apps/my-app/.env.local` +8. `apps/my-app/.local.env` +9. `apps/my-app/.env` +10. `.env.[target-name].[configuration-name]` +11. `.[target-name].[configuration-name].env` +12. `.env.[configuration-name]` +13. `.[configuration-name].env` +14. `.env.[target-name]` +15. `.[target-name].env` +16. `.local.env` +17. `.env.local` +18. `.env` {% callout type="warning" title="Order is important" %} Nx will move through the above list, ignoring files it can't find, and loading environment variables diff --git a/e2e/nx-run/src/run.test.ts b/e2e/nx-run/src/run.test.ts index a5228b9854d0e4..963a7d7e9d126a 100644 --- a/e2e/nx-run/src/run.test.ts +++ b/e2e/nx-run/src/run.test.ts @@ -189,19 +189,28 @@ describe('Nx Running Tests', () => { const myapp = uniq('app'); const target = uniq('script'); const expectedOutput = uniq('myEchoedString'); + const expectedEnvOutput = uniq('myEnvString'); runCLI(`generate @nx/web:app ${myapp}`); + updateFile( + `apps/${myapp}/.env.production`, + `ENV_VAR=${expectedEnvOutput}` + ); updateFile( `apps/${myapp}/package.json`, JSON.stringify({ name: myapp, scripts: { - [target]: `echo ${expectedOutput}`, + [target]: `echo ${expectedOutput} $ENV_VAR`, }, }) ); expect(runCLI(`${target} ${myapp}`)).toContain(expectedOutput); + expect(runCLI(`${target} ${myapp}`)).not.toContain(expectedEnvOutput); + expect(runCLI(`${target} ${myapp} --configuration production`)).toContain( + expectedEnvOutput + ); }, 10000); it('should run targets inferred from plugin-specified project files', () => { diff --git a/packages/nx/src/tasks-runner/forked-process-task-runner.ts b/packages/nx/src/tasks-runner/forked-process-task-runner.ts index 702d22beec8bbc..83f540a8c07185 100644 --- a/packages/nx/src/tasks-runner/forked-process-task-runner.ts +++ b/packages/nx/src/tasks-runner/forked-process-task-runner.ts @@ -415,10 +415,16 @@ export class ForkedProcessTaskRunner { ...parseEnv(`.${task.target.target}.env`), ...parseEnv(`.env.${task.target.target}`), ...(task.target.configuration - ? parseEnv(`.${task.target.target}.${task.target.configuration}.env`) - : {}), - ...(task.target.configuration - ? parseEnv(`.env.${task.target.target}.${task.target.configuration}`) + ? { + ...parseEnv(`.${task.target.configuration}.env`), + ...parseEnv( + `.${task.target.target}.${task.target.configuration}.env` + ), + ...parseEnv(`.env.${task.target.configuration}`), + ...parseEnv( + `.env.${task.target.target}.${task.target.configuration}` + ), + } : {}), ...parseEnv(`${task.projectRoot}/.env`), ...parseEnv(`${task.projectRoot}/.local.env`), @@ -426,14 +432,20 @@ export class ForkedProcessTaskRunner { ...parseEnv(`${task.projectRoot}/.${task.target.target}.env`), ...parseEnv(`${task.projectRoot}/.env.${task.target.target}`), ...(task.target.configuration - ? parseEnv( - `${task.projectRoot}/.${task.target.target}.${task.target.configuration}.env` - ) - : {}), - ...(task.target.configuration - ? parseEnv( - `${task.projectRoot}/.env.${task.target.target}.${task.target.configuration}` - ) + ? { + ...parseEnv( + `${task.projectRoot}/.${task.target.configuration}.env` + ), + ...parseEnv( + `${task.projectRoot}/.${task.target.target}.${task.target.configuration}.env` + ), + ...parseEnv( + `${task.projectRoot}/.env.${task.target.configuration}` + ), + ...parseEnv( + `${task.projectRoot}/.env.${task.target.target}.${task.target.configuration}` + ), + } : {}), }; } else { diff --git a/packages/nx/src/utils/project-graph-utils.ts b/packages/nx/src/utils/project-graph-utils.ts index 4bf89d30b59cdf..6d565d1ff13aa5 100644 --- a/packages/nx/src/utils/project-graph-utils.ts +++ b/packages/nx/src/utils/project-graph-utils.ts @@ -1,9 +1,10 @@ import { buildTargetFromScript, PackageJson } from './package-json'; import { join } from 'path'; import { ProjectGraph, ProjectGraphProjectNode } from '../config/project-graph'; -import { readJsonFile } from './fileutils'; +import { fileExists, readJsonFile } from './fileutils'; import { readCachedProjectGraph } from '../project-graph/project-graph'; import { TargetConfiguration } from '../config/workspace-json-project-json'; +import { workspaceRoot } from './workspace-root'; export function projectHasTarget( project: ProjectGraphProjectNode, @@ -22,9 +23,18 @@ export function projectHasTargetAndConfiguration( configuration: string ) { return ( - projectHasTarget(project, target) && - project.data.targets[target].configurations && - project.data.targets[target].configurations[configuration] + // Explicitly defined target + configuration + (projectHasTarget(project, target) && + project.data.targets[target].configurations && + project.data.targets[target].configurations[configuration]) || + // Inferred configuration from presence of .env files + (projectHasTarget(project, target) && + [ + join(workspaceRoot, `.env.${configuration}`), + join(workspaceRoot, `.${configuration}.env`), + join(workspaceRoot, project.data.root, `.${configuration}.env`), + join(workspaceRoot, project.data.root, `.${configuration}.env`), + ].some(fileExists)) ); }