nrwl/nx

TypeError in nx:run-commands with `env` option

romainwg opened this issue · 1 comments

Current Behavior

When executing a custom nx run-command target in nx.json to run a script, specifying environment variables using the "env" option, the command fails with a TypeError: Cannot convert undefined or null to object. This occurs despite following the official documentation for passing environment variables to commands.

For example:

npx nx run default-hmi:docker-build-test

"docker-build-test": {
    "executor": "nx:run-commands",
    "options": {
      "command": "./scripts/docker-manager.sh build",
      "cwd": ".",
      "env": { "FOO": "BAR" }
    }
  }

Expected Behavior

The expected behavior is that the environment variable specified in the "env" option of the nx:run-commands executor should be correctly passed to the command being executed, allowing for successful execution of the script with the provided environment variables. No TypeErrors should occur when using documented features as expected.

The documentation indicates that you can use env or envFile; in fact, envFile works just as well as envFile.
See: https://nx.dev/nx-api/nx/executors/run-commands#env

env is set as object on the json schema:

GitHub Repo

There is no problem on nx 19.0.0-beta (nx-examples):
romainwg/nx-examples-env-issue@4b1d05a

But does not work on nx 18.1.3:
romainwg/nx-examples-env-issue@c45334a

Steps to Reproduce

  1. Define a custom target in nx.json using nx:run-commands that executes a shell script and uses the env option to specify environment variables.
  2. Run the custom target using the Nx CLI command npx nx run <project>:<target>.
  3. Observe the TypeError: Cannot convert undefined or null to object error being thrown during execution.

Nx Report

NX   Report complete - copy this into the issue template

Node   : 21.6.2
OS     : linux-x64
npm    : 10.2.4

nx                 : 18.1.3
@nx/js             : 18.1.3
@nx/jest           : 18.1.3
@nx/linter         : 18.1.3
@nx/eslint         : 18.1.3
@nx/workspace      : 18.1.3
@nx/cypress        : 18.1.3
@nx/devkit         : 18.1.3
@nx/eslint-plugin  : 18.1.3
@nx/node           : 18.1.3
@nx/plugin         : 18.1.3
@nx/react          : 18.1.3
@nx/storybook      : 18.1.3
@nrwl/tao          : 18.1.3
@nx/vite           : 18.1.3
@nx/web            : 18.1.3
typescript         : 5.3.3
---------------------------------------
Community plugins:
@webpro/nx-tsc : 0.0.1
---------------------------------------
Local workspace plugins:
         @hmi/plugin-check-unpinned-dependencies
         @hmi/plugin-check-envs-sync

Failure Logs

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    (Additional stack trace information corresponding to the error encountered)

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

it is fixed in pr #22426 which is available after 18.2.0. it does not work for 18.1.3. once you update to a version > 18.2.0, it should work.