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

Allow Nx, by default, to parse env files by target configuration name #14931

Closed
1 task done
Hywie opened this issue Feb 11, 2023 · 2 comments · Fixed by #14998
Closed
1 task done

Allow Nx, by default, to parse env files by target configuration name #14931

Hywie opened this issue Feb 11, 2023 · 2 comments · Fixed by #14998
Assignees
Labels

Comments

@Hywie
Copy link

Hywie commented Feb 11, 2023

Description

Currently, Nx uses the target name when searching for env files but does not take into account the configuration of the target.

The current implementation looks for:

  1. .env
  2. .local.env
  3. .env.local
  4. .[target-name].env
  5. .env.[target-name]
  6. apps/my-app/.env
  7. apps/my-app/.local.env
  8. apps/my-app/.env.local
  9. apps/my-app/.[target-name].env
  10. apps/my-app/.env.[target-name]

I suggest we include the configuration name when looking for env files. That would allow us to look for:

  1. .env
  2. .local.env
  3. .env.local
  4. .[target-name].env
  5. .[target-name].[target-configuration-name].env
  6. .env.[target-name]
  7. .env.[target-name].[target-configuration-name]
  8. apps/my-app/.env
  9. apps/my-app/.local.env
  10. apps/my-app/.env.local
  11. apps/my-app/.[target-name].env
  12. apps/my-app/.[target-name].[target-configuration-name].env
  13. apps/my-app/.env.[target-name]
  14. apps/my-app/.env.[target-name].[target-configuration-name]

Motivation

We often want to run a task that has multiple configurations with the ability for each configuration to have its own envFile.

For example, you may have multiple configurations (Testing, Staging, Production) for run-app. We want each configuration to use a different env file so they use and point at different URLs. We are currently unable to do this. To acheive something similar, we have to either:

  • Duplicate the tasks, but just change the target name. E.g. run-app-in-testing, run-app-in-staging etc.
  • Do some transformation on the .env file before running the task

If envFiles with both the target and configuration name were automatically picked up and parsed. It would solve a few issues around unnecessarily having multiple targets when configurations should be used or the need to transform env files.

It looks like this idea has been raised within other issues/ feat requests but not implemented:

Suggested Implementation

A change to getDotenvVariablesForTask() in packages/nx/src/tasks-runner/forked-process-task-runner.ts to include the above search patterns. This would allow the configuration name to be used automatically in all executables.

Alternate Implementations

Another alternative is for Nx packages to extend their options to allow an env file path.

Please do shout though if I have missed something and there is a better alternative than making a change to the task runner process, I'm not familiar with the Nx code base.

@Hywie Hywie changed the title Allow NX, by default, to parse env files by target configuration name Allow Nx, by default, to parse env files by target configuration name Feb 11, 2023
@AgentEnder AgentEnder added the scope: core core nx functionality label Feb 13, 2023
@AgentEnder AgentEnder self-assigned this Feb 13, 2023
@Hywie
Copy link
Author

Hywie commented Feb 15, 2023

Thank you @AgentEnder for making that change

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants