aspect-build / rules_js

High-performance Bazel rules for running Node.js tools and building JavaScript projects
https://docs.aspect.build/rules/aspect_rules_js
Apache License 2.0
299 stars 102 forks source link

[Bug]: js_binary does not pass environment variables set by bazel run --action_env #1429

Closed RobertDiebels closed 8 months ago

RobertDiebels commented 8 months ago

What happened?

When calling a js_binary with bazel run --action_env ENV_VAR=value the underlying node execution does not have an environment variable ENV_VAR available in process.env.ENV_VAR

Version

Development (host) and target OS/architectures:

Output of bazel --version: aspect 5.3.4

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file:

# Javascript Rules Module

bazel_dep(
    name = "aspect_rules_js",
    version = "1.34.1",
)

####### Node.js version #########
# By default you get the node version from DEFAULT_NODE_VERSION in @rules_nodejs//nodejs:repositories.bzl
# Optionally you can pin a different node version:
bazel_dep(
    name = "rules_nodejs",
    version = "5.8.2",
)

node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")

node.toolchain(node_version = "16.18.1")
#################################

npm = use_extension(
    "@aspect_rules_js//npm:extensions.bzl",
    "npm",
    dev_dependency = True,
)

npm.npm_translate_lock(
    name = "npm",
    #   Note: This is necessary only due to this dependency chain:
    #         ssh2 1.15.0
    #        └─┬ cpu-features 0.0.9
    #          └── buildcheck 0.0.6
    #
    #   If not enabled buildcheck will crash the build with the following error-message:
    #     "Unable to detect compiler type"
    no_optional = True,
    pnpm_lock = "//:pnpm-lock.yaml",
    verify_node_modules_ignored = "//:.bazelignore",
)

use_repo(npm, "npm")

# Typescript Rules Module

bazel_dep(
    name = "aspect_rules_ts",
    version = "2.1.0",
)

rules_ts_ext = use_extension(
    "@aspect_rules_ts//ts:extensions.bzl",
    "ext",
    dev_dependency = True,
)

rules_ts_ext.deps()

use_repo(rules_ts_ext, "npm_typescript")

# Bazel Utilities Module

bazel_dep(
    name = "aspect_bazel_lib",
    version = "2.1.0",
)

# SWC.rs as Typescript Transpiler Module

bazel_dep(
    name = "aspect_rules_swc",
    version = "1.1.0",
)

Language(s) and/or frameworks involved:

How to reproduce

- Create a ts_project action
- Add a single typescript file that logs `JSON.stringify(process.env, null, "/t");` to console to the ts_project
- Create a js_binary from the ts_project.
- Run the js_binary action with --action_env ENV_VAR=value
- Observe that there is no environment variable `ENV_VAR` in the `process.env` object.

Any other information?

Running this on Windows 10

Strum355 commented 8 months ago

--action_env doesnt get propagated during bazel run, see https://github.com/bazelbuild/bazel/issues/8578. --action_env does work in the case where js_binary is run with bazel build (via js_run_binary, and likely other ways)

RobertDiebels commented 8 months ago

@Strum355 Thanks for the reply. I've applied your suggestion in a previous setup I had which did indeed work. I was under the impression that --action_env would also work with bazel run due to the cli documentation stating: "Inherits all options from build.".

However, I just went through the documentation of the bazel CLI again and --action_env is not listed as a build option, nor a common option. So either the documentation is incorrect or --action_env shouldn't work with both commands. I'm assuming that aspect 5.3.4 has the same commands and options as bazel's cli v7.0 here because I can't find a mapping between the two.

In any case, thanks again for the reply, I've since managed to use Make variables by passing --define ENV_VAR=value and env = {"ENV_VAR="$(ENV_VAR)"} of the js_binary rule. This achieves what I had wanted to do (provide a username and password through environment variables).