From 3ccfd3c673ce66eb50e5feb5fdf9bb640d023cd3 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Tue, 14 Jan 2020 14:42:25 -0800 Subject: [PATCH] feat(builtin): add configuration_env_vars to npm_package_bin This behaves similarly to `configuration_env_vars` in `nodejs_binary` except it passes the env vars to the process via the `env` attribute of `ctx.actions.run`. Also add support for picking up env vars from `ctx.configuration.default_shell_env` if they are not found in `ctx.vars`. * `ctx.vars` will contain values from `--define=FOO=BAR` * `ctx.configuration.default_shell_env` will `--action_env=FOO=BAR` (but not from `--action_env=FOO`). --- internal/node/node.bzl | 5 +++++ internal/node/npm_package_bin.bzl | 2 ++ internal/providers/node_runtime_deps_info.bzl | 16 +++++++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/internal/node/node.bzl b/internal/node/node.bzl index 7121a302c7..b3a29cae65 100644 --- a/internal/node/node.bzl +++ b/internal/node/node.bzl @@ -178,8 +178,13 @@ def _nodejs_binary_impl(ctx): env_vars = "export BAZEL_TARGET=%s\n" % ctx.label env_vars += "export BAZEL_WORKSPACE=%s\n" % ctx.workspace_name for k in ctx.attr.configuration_env_vars + ctx.attr.default_env_vars: + # Check ctx.var first & if env var not in there then check + # ctx.configuration.default_shell_env. The former will contain values from --define=FOO=BAR + # and latter will contain values from --action_env=FOO=BAR (but not from --action_env=FOO). if k in ctx.var.keys(): env_vars += "export %s=\"%s\"\n" % (k, ctx.var[k]) + elif k in ctx.configuration.default_shell_env.keys(): + env_vars += "export %s=\"%s\"\n" % (k, ctx.configuration.default_shell_env[k]) expected_exit_code = 0 if hasattr(ctx.attr, "expected_exit_code"): diff --git a/internal/node/npm_package_bin.bzl b/internal/node/npm_package_bin.bzl index 7cc10704b0..ee86ada734 100644 --- a/internal/node/npm_package_bin.bzl +++ b/internal/node/npm_package_bin.bzl @@ -9,6 +9,7 @@ load("//internal/linker:link_node_modules.bzl", "module_mappings_aspect") _ATTRS = { "outs": attr.output_list(), "args": attr.string_list(mandatory = True), + "configuration_env_vars": attr.string_list(default = []), "data": attr.label_list(allow_files = True, aspects = [module_mappings_aspect, node_modules_aspect]), "output_dir": attr.bool(), "tool": attr.label( @@ -57,6 +58,7 @@ def _impl(ctx): inputs = inputs, outputs = outputs, arguments = [args], + configuration_env_vars = ctx.attr.configuration_env_vars, ) return [DefaultInfo(files = depset(outputs))] diff --git a/internal/providers/node_runtime_deps_info.bzl b/internal/providers/node_runtime_deps_info.bzl index d9cd60a590..67068fa48f 100644 --- a/internal/providers/node_runtime_deps_info.bzl +++ b/internal/providers/node_runtime_deps_info.bzl @@ -63,10 +63,20 @@ def run_node(ctx, inputs, arguments, executable, **kwargs): # To access runfiles, you must use a runfiles helper in the program instead add_arg(arguments, "--nobazel_patch_module_resolver") - # Forward the COMPILATION_MODE to node process as an environment variable env = kwargs.pop("env", {}) - if "COMPILATION_MODE" not in env.keys(): - env["COMPILATION_MODE"] = ctx.var["COMPILATION_MODE"] + + # Always forward the COMPILATION_MODE to node process as an environment variable + configuration_env_vars = kwargs.pop("configuration_env_vars", []) + ["COMPILATION_MODE"] + for var in configuration_env_vars: + if var not in env.keys(): + # If env is not explicitely specified, check ctx.var first & if env var not in there + # then check ctx.configuration.default_shell_env. The former will contain values from + # --define=FOO=BAR and latter will contain values from --action_env=FOO=BAR + # (but not from --action_env=FOO). + if var in ctx.var.keys(): + env[var] = ctx.var[var] + elif var in ctx.configuration.default_shell_env.keys(): + env[var] = ctx.configuration.default_shell_env[var] ctx.actions.run( inputs = inputs + extra_inputs,