You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the "nx:run-command" executor function is called from a custom executor, environment variables already set in the executor environment are overridden by environment variables in .env in the command's environment.
Expected Behavior
The following doc indicates that .env files do not override existing environment variables
{% callout type="warning" title="Order is important" %}
Nx will move through the above list, ignoring files it can't find, and loading environment variables
into the current process for the ones it can find. If it finds a variable that has already been loaded into the process,
it will ignore it. It does this for two reasons:
1. Developers can't accidentally overwrite important system level variables (like `NODE_ENV`)
2. Allows developers to create `.env.local` or `.local.env` files for their local environment and override any project
defaults set in `.env`
3. Allows developers to create target specific `.env.[target-name]` or `.[target-name].env` to overwrite environment variables for specific targets. For instance, you could increase the memory use for node processes only for build targets by setting `NODE_OPTIONS=--max-old-space-size=4096` in `.build.env`
I believe this is happening because the options.env property is unset when the @nxlv/python:run-commands executor function is called, and therefore is unset in the subsequent call of the nx:run-command executor function. In contrast, when the nx:run-command executor is used directly by a target, Nx appears to prepopulate options.env with the process's current environment variables when calling the executor function.
options.env is eventually passed as the env argument in processEnv (shown below). Immediately after line 493, res will contain both process environment variables and variables from .env, with the latter taking precedence. If env contains the process environment variables (as is the case when nx:run-command is used directly by a target), lines 495-498 will counter-override the environment variables from .env with the process environment variables. But this does not happen if env is empty.
if(localEnv.Path)res.Path=localEnv.Path;// Windows
if(color){
res.FORCE_COLOR=`${color}`;
}
returnres;
}
I think there are potentially two scenarios in which this behavior is WAI:
The nx:run-command executor function is not intended to be called from other executors in the first place, though https://nx.dev/extending-nx/recipes/compose-executors#compose-executors seems to indicate that executor functions are in general intended to be called from other executors.
I suppose one could stipulate that it is the responsibility of callers of the nx:run-command executor function to set the env option to include the currently-set environment variables, but that would seem to violate the principle of least astonishment, and I would argue should at the very least be clearly documented.
The text was updated successfully, but these errors were encountered:
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->
<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->
<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->
## Current Behavior
<!-- This is the behavior we have today -->
## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->
Fixes#27821
(cherry picked from commit caae4cf)
Current Behavior
When the "nx:run-command" executor function is called from a custom executor, environment variables already set in the executor environment are overridden by environment variables in
.env
in the command's environment.Expected Behavior
The following doc indicates that
.env
files do not override existing environment variablesnx/docs/shared/guides/define-environment-variables.md
Lines 35 to 54 in 0b99d1d
Also, existing environment variables are not overridden by
.env
if the "nx:run-command" executor is used directly by a target.GitHub Repo
https://github.com/ethantkoenig/nx-env-var-bug
Steps to Reproduce
Run
ENV_VAR=already-set nx run foo:echo-env-var
, and note that the output isfrom-file
(set in.env
) rather thanalready-set
.Nx Report
Failure Logs
No response
Package Manager Version
No response
Operating System
Additional Information
Note that the
foo:echo-env-var
target uses the@nxlv/python:run-commands
executor, which is a very thin wrapper aroundnx:run-command
.I believe this is happening because the
options.env
property is unset when the@nxlv/python:run-commands
executor function is called, and therefore is unset in the subsequent call of thenx:run-command
executor function. In contrast, when thenx:run-command
executor is used directly by a target, Nx appears to prepopulateoptions.env
with the process's current environment variables when calling the executor function.options.env
is eventually passed as theenv
argument inprocessEnv
(shown below). Immediately after line 493,res
will contain both process environment variables and variables from.env
, with the latter taking precedence. Ifenv
contains the process environment variables (as is the case whennx:run-command
is used directly by a target), lines 495-498 will counter-override the environment variables from.env
with the process environment variables. But this does not happen ifenv
is empty.nx/packages/nx/src/executors/run-commands/run-commands.impl.ts
Lines 480 to 508 in 0b99d1d
I think there are potentially two scenarios in which this behavior is WAI:
nx:run-command
executor function is not intended to be called from other executors in the first place, though https://nx.dev/extending-nx/recipes/compose-executors#compose-executors seems to indicate that executor functions are in general intended to be called from other executors.nx:run-command
executor function to set theenv
option to include the currently-set environment variables, but that would seem to violate the principle of least astonishment, and I would argue should at the very least be clearly documented.The text was updated successfully, but these errors were encountered: