Skip to content

Commit

Permalink
feat(builtin): perform make variable substitution in npm_package_bin …
Browse files Browse the repository at this point in the history
…env vars (#3343)
  • Loading branch information
kormide authored Feb 26, 2022
1 parent 5a1cbfa commit dfe4392
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/Built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,8 @@ Defaults to `[]`

<h4 id="npm_package_bin-env">env</h4>

specifies additional environment variables to set when the target is executed
specifies additional environment variables to set when the target is executed. The values of environment variables
are subject to 'Make variable' substitution (see [args](#npm_package_bin-args)).

Defaults to `{}`

Expand Down
9 changes: 7 additions & 2 deletions internal/node/npm_package_bin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def _impl(ctx):
for a in ctx.attr.args:
args.add_all([expand_variables(ctx, e, outs = ctx.outputs.outs, output_dir = ctx.attr.output_dir) for e in _expand_locations(ctx, a)])

envs = {}
for k, v in ctx.attr.env.items():
envs[k] = " ".join([expand_variables(ctx, e, outs = ctx.outputs.outs, output_dir = ctx.attr.output_dir, attribute_name = "env") for e in _expand_locations(ctx, v)])

tool_outputs = []
if ctx.outputs.stdout:
tool_outputs.append(ctx.outputs.stdout)
Expand All @@ -83,7 +87,7 @@ def _impl(ctx):
arguments = [args],
configuration_env_vars = ctx.attr.configuration_env_vars,
chdir = expand_variables(ctx, ctx.attr.chdir),
env = ctx.attr.env,
env = envs,
stdout = ctx.outputs.stdout,
stderr = ctx.outputs.stderr,
exit_code_out = ctx.outputs.exit_code_out,
Expand Down Expand Up @@ -213,7 +217,8 @@ def npm_package_bin(
args = ["/".join([".."] * _package_segments + ["$@"])],
)
```
env: specifies additional environment variables to set when the target is executed
env: specifies additional environment variables to set when the target is executed. The values of environment variables
are subject to 'Make variable' substitution (see [args](#npm_package_bin-args)).
**kwargs: additional undocumented keyword args
"""
if not tool:
Expand Down
10 changes: 10 additions & 0 deletions internal/node/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,15 @@ npm_package_bin(
"somearg$$",
"some0arg",
],
env = {
"OUTFILE": "$@",
"COMPLATION_MODE": "$(COMPILATION_MODE)",
"TARGET_CPU": "$(TARGET_CPU)",
"BINDIR": "$(BINDIR)",
"SOME_TEST_ENV": "$(SOME_TEST_ENV)",
"SOMEARG$$": "somearg$$",
"SOME0ARG": "some0arg",
},
tool = ":expand_variables",
)

Expand All @@ -368,6 +377,7 @@ jasmine_node_test(
data = [":expand_variables.out"],
templated_args = [
"$(rootpath :expand_variables.out)",
"$(execpath :expand_variables.out)",
"$(COMPILATION_MODE)",
"$(TARGET_CPU)",
"$(BINDIR)",
Expand Down
16 changes: 14 additions & 2 deletions internal/node/test/expand_variables.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
const fs = require('fs');
const args = process.argv.slice(2);
const outfile = args.shift();
fs.writeFileSync(outfile, JSON.stringify(args, null, 2), 'utf-8');
const outfile = args[0];
const dump = {
args,
env: {
OUTFILE: process.env.OUTFILE,
COMPLATION_MODE: process.env.COMPLATION_MODE,
TARGET_CPU: process.env.TARGET_CPU,
BINDIR: process.env.BINDIR,
SOME_TEST_ENV: process.env.SOME_TEST_ENV,
SOMEARG$$: process.env.SOMEARG$$,
SOME0ARG: process.env.SOME0ARG,
}
}
fs.writeFileSync(outfile, JSON.stringify(dump, null, 2), 'utf-8');
15 changes: 13 additions & 2 deletions internal/node/test/expand_variables.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@ const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);
const args = process.argv.slice(2);
const out = JSON.parse(
require('fs').readFileSync(runfiles.resolveWorkspaceRelative(args.shift()), 'utf-8'));
const expected = args;
const expected_args = args;

describe('nodejs_test templated_args variable expansion', function() {
it('should match variable expansion in npm_package_bin args', function() {
expect(out).toEqual(expected);
expect(out.args).toEqual(expected_args);
});
it('should match variable expansion in npm_package_bin env vars', function() {
expect(out.env).toEqual({
OUTFILE: expected_args[0],
COMPLATION_MODE: expected_args[1],
TARGET_CPU: expected_args[2],
BINDIR: expected_args[3],
SOME_TEST_ENV: expected_args[4],
SOMEARG$$: expected_args[5],
SOME0ARG: expected_args[6],
})
});
});

0 comments on commit dfe4392

Please sign in to comment.