Skip to content

Commit

Permalink
feat(core): load environment variables from configuration name (#17335)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Jun 28, 2023
1 parent 03ffa42 commit 199d621
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
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
21 changes: 20 additions & 1 deletion e2e/nx-run/src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,38 @@ 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}/package.json`,
JSON.stringify({
name: myapp,
scripts: {
[target]: `echo ${expectedOutput}`,
[target]: `echo ${expectedOutput} $ENV_VAR`,
},
nx: {
targets: {
[target]: {
configurations: {
production: {},
},
},
},
},
})
);

updateFile(
`apps/${myapp}/.env.production`,
`ENV_VAR=${expectedEnvOutput}`
);

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', () => {
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 @@ -433,25 +433,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

1 comment on commit 199d621

@vercel
Copy link

@vercel vercel bot commented on 199d621 Jun 28, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx.dev
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.