Skip to content

Commit

Permalink
feat(builtin): add configuration_env_vars to npm_package_bin
Browse files Browse the repository at this point in the history
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`).
  • Loading branch information
gregmagolan committed Jan 14, 2020
1 parent 9ac0534 commit 3ccfd3c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
2 changes: 2 additions & 0 deletions internal/node/npm_package_bin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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))]

Expand Down
16 changes: 13 additions & 3 deletions internal/providers/node_runtime_deps_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3ccfd3c

Please sign in to comment.