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

feat(core): load environment variables from configuration name #17335

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 16 additions & 12 deletions docs/shared/guides/define-environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 24 additions & 12 deletions packages/nx/src/tasks-runner/forked-process-task-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,25 +424,37 @@ 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`),
...parseEnv(`${task.projectRoot}/.env.local`),
...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 {
Expand Down
28 changes: 24 additions & 4 deletions packages/nx/src/utils/project-graph-utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -22,9 +23,28 @@ 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}`),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think having a file there should define a configuration. Configurations should still be defined in the project configuration. If we'd like an easier way to define configurations, that is a separate discussion.

Can we remove this part from the changes here and consider them separately?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think having a file there should define a configuration. Configurations should still be defined in the project configuration. If we'd like an easier way to define configurations, that is a separate discussion.

Removing the requirement to redefine a configuration under every target is what myself and other customers are trying to get. With the current state of environment files in NX, a great deal of technical debt and configuration overhead is generated making it an unusable solution for most projects ultimately making NX very limiting.

join(workspaceRoot, `.${configuration}.env`),
join(workspaceRoot, project.data.root, `.env.${configuration}`),
join(workspaceRoot, project.data.root, `.${configuration}.env`),
join(
workspaceRoot,
project.data.root,
`.env.${target}.${configuration}`
),
join(
workspaceRoot,
project.data.root,
`.${target}.${configuration}.env`
),
].some(fileExists))
);
}

Expand Down