Skip to content

Commit 87da874

Browse files
kormidealexeagle
authored andcommitted
feat(builtin): perform make variable substitution in npm_package_bin env vars (bazel-contrib#3343)
1 parent 511d1d4 commit 87da874

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

docs/Built-ins.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2223,7 +2223,8 @@ Defaults to `[]`
22232223

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

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

22282229
Defaults to `{}`
22292230

internal/node/npm_package_bin.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def _impl(ctx):
6363
for a in ctx.attr.args:
6464
args.add_all([expand_variables(ctx, e, outs = ctx.outputs.outs, output_dir = ctx.attr.output_dir) for e in _expand_locations(ctx, a)])
6565

66+
envs = {}
67+
for k, v in ctx.attr.env.items():
68+
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)])
69+
6670
tool_outputs = []
6771
if ctx.outputs.stdout:
6872
tool_outputs.append(ctx.outputs.stdout)
@@ -81,7 +85,7 @@ def _impl(ctx):
8185
arguments = [args],
8286
configuration_env_vars = ctx.attr.configuration_env_vars,
8387
chdir = expand_variables(ctx, ctx.attr.chdir),
84-
env = ctx.attr.env,
88+
env = envs,
8589
stdout = ctx.outputs.stdout,
8690
stderr = ctx.outputs.stderr,
8791
exit_code_out = ctx.outputs.exit_code_out,
@@ -211,7 +215,8 @@ def npm_package_bin(
211215
args = ["/".join([".."] * _package_segments + ["$@"])],
212216
)
213217
```
214-
env: specifies additional environment variables to set when the target is executed
218+
env: specifies additional environment variables to set when the target is executed. The values of environment variables
219+
are subject to 'Make variable' substitution (see [args](#npm_package_bin-args)).
215220
**kwargs: additional undocumented keyword args
216221
"""
217222
if not tool:

internal/node/test/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,15 @@ npm_package_bin(
369369
"somearg$$",
370370
"some0arg",
371371
],
372+
env = {
373+
"OUTFILE": "$@",
374+
"COMPLATION_MODE": "$(COMPILATION_MODE)",
375+
"TARGET_CPU": "$(TARGET_CPU)",
376+
"BINDIR": "$(BINDIR)",
377+
"SOME_TEST_ENV": "$(SOME_TEST_ENV)",
378+
"SOMEARG$$": "somearg$$",
379+
"SOME0ARG": "some0arg",
380+
},
372381
tool = ":expand_variables",
373382
)
374383

@@ -378,6 +387,7 @@ jasmine_node_test(
378387
data = [":expand_variables.out"],
379388
templated_args = [
380389
"$(rootpath :expand_variables.out)",
390+
"$(execpath :expand_variables.out)",
381391
"$(COMPILATION_MODE)",
382392
"$(TARGET_CPU)",
383393
"$(BINDIR)",
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
const fs = require('fs');
22
const args = process.argv.slice(2);
3-
const outfile = args.shift();
4-
fs.writeFileSync(outfile, JSON.stringify(args, null, 2), 'utf-8');
3+
const outfile = args[0];
4+
const dump = {
5+
args,
6+
env: {
7+
OUTFILE: process.env.OUTFILE,
8+
COMPLATION_MODE: process.env.COMPLATION_MODE,
9+
TARGET_CPU: process.env.TARGET_CPU,
10+
BINDIR: process.env.BINDIR,
11+
SOME_TEST_ENV: process.env.SOME_TEST_ENV,
12+
SOMEARG$$: process.env.SOMEARG$$,
13+
SOME0ARG: process.env.SOME0ARG,
14+
}
15+
}
16+
fs.writeFileSync(outfile, JSON.stringify(dump, null, 2), 'utf-8');

internal/node/test/expand_variables.spec.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,21 @@ const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);
2020
const args = process.argv.slice(2);
2121
const out = JSON.parse(
2222
require('fs').readFileSync(runfiles.resolveWorkspaceRelative(args.shift()), 'utf-8'));
23-
const expected = args;
23+
const expected_args = args;
2424

2525
describe('nodejs_test templated_args variable expansion', function() {
2626
it('should match variable expansion in npm_package_bin args', function() {
27-
expect(out).toEqual(expected);
27+
expect(out.args).toEqual(expected_args);
28+
});
29+
it('should match variable expansion in npm_package_bin env vars', function() {
30+
expect(out.env).toEqual({
31+
OUTFILE: expected_args[0],
32+
COMPLATION_MODE: expected_args[1],
33+
TARGET_CPU: expected_args[2],
34+
BINDIR: expected_args[3],
35+
SOME_TEST_ENV: expected_args[4],
36+
SOMEARG$$: expected_args[5],
37+
SOME0ARG: expected_args[6],
38+
})
2839
});
2940
});

0 commit comments

Comments
 (0)