Skip to content

Commit ee584f8

Browse files
rzhwalexeagle
authored andcommitted
feat: add depset support to run_node inputs, matching ctx.action.run
1 parent 9180eea commit ee584f8

4 files changed

Lines changed: 66 additions & 3 deletions

File tree

internal/providers/node_runtime_deps_info.bzl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ def run_node(ctx, inputs, arguments, executable, **kwargs):
6767
exec_exec = getattr(ctx.executable, executable)
6868

6969
outputs = kwargs.pop("outputs", [])
70-
extra_inputs = []
70+
extra_inputs = depset()
7171
link_data = []
7272
if (NodeRuntimeDepsInfo in exec_attr):
73-
extra_inputs = exec_attr[NodeRuntimeDepsInfo].deps.to_list()
73+
extra_inputs = exec_attr[NodeRuntimeDepsInfo].deps
7474
link_data = exec_attr[NodeRuntimeDepsInfo].pkgs
7575

7676
mnemonic = kwargs.get("mnemonic")
@@ -114,9 +114,17 @@ def run_node(ctx, inputs, arguments, executable, **kwargs):
114114
env[var] = ctx.configuration.default_shell_env[var]
115115
env["BAZEL_NODE_MODULES_ROOT"] = _compute_node_modules_root(ctx)
116116

117+
# ctx.actions.run accepts both lists and a depset for inputs. Coerce the original inputs to a
118+
# depset if they're a list, so that extra inputs can be combined in a performant manner.
119+
inputs_depset = depset(transitive = [
120+
depset(direct = inputs) if type(inputs) == "list" else inputs,
121+
extra_inputs,
122+
depset(direct = [modules_manifest]),
123+
])
124+
117125
ctx.actions.run(
118126
outputs = outputs,
119-
inputs = inputs + extra_inputs + [modules_manifest],
127+
inputs = inputs_depset,
120128
arguments = arguments,
121129
executable = exec_exec,
122130
env = env,

internal/providers/test/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ nodejs_binary(
1717
entry_point = "js-write-file.js",
1818
)
1919

20+
nodejs_binary(
21+
name = "cloner_bin",
22+
entry_point = "js-clone-file.js",
23+
)
24+
2025
js_write_file(
2126
name = "write_file",
2227
content = "test file content",
@@ -32,5 +37,7 @@ js_write_file(
3237
for file in [
3338
"out",
3439
"out2",
40+
"out3",
41+
"out4",
3542
]
3643
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const readFileSync = require('fs').readFileSync;
2+
const writeFileSync = require('fs').writeFileSync;
3+
4+
const inputPath = process.argv[2];
5+
const outputPath = process.argv[3];
6+
7+
const content = readFileSync(inputPath, 'utf8');
8+
9+
writeFileSync(outputPath, content, {encoding: 'utf8'});

internal/providers/test/run_node_test.bzl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,53 @@ def _js_write_file_impl(ctx):
3131
outputs = [ctx.outputs.out2],
3232
)
3333

34+
content_txt = ctx.actions.declare_file("content.txt")
35+
ctx.actions.write(
36+
output = content_txt,
37+
content = ctx.attr.content,
38+
)
39+
40+
run_node(
41+
ctx = ctx,
42+
executable = "_clone",
43+
mnemonic = "cloner",
44+
# Pass inputs as a list.
45+
inputs = [content_txt],
46+
arguments = [
47+
content_txt.path,
48+
ctx.outputs.out3.path,
49+
],
50+
outputs = [ctx.outputs.out3],
51+
)
52+
53+
run_node(
54+
ctx = ctx,
55+
executable = "_clone",
56+
mnemonic = "cloner",
57+
# Pass inputs as a depset.
58+
inputs = depset(direct = [content_txt]),
59+
arguments = [
60+
content_txt.path,
61+
ctx.outputs.out4.path,
62+
],
63+
outputs = [ctx.outputs.out4],
64+
)
65+
3466
js_write_file = rule(
3567
implementation = _js_write_file_impl,
3668
outputs = {
3769
"out": "out.txt",
3870
"out2": "out2.txt",
71+
"out3": "out3.txt",
72+
"out4": "out4.txt",
3973
},
4074
attrs = {
4175
"content": attr.string(),
76+
"_clone": attr.label(
77+
default = Label("//internal/providers/test:cloner_bin"),
78+
cfg = "host",
79+
executable = True,
80+
),
4281
"_writer": attr.label(
4382
default = Label("//internal/providers/test:writer_bin"),
4483
cfg = "host",

0 commit comments

Comments
 (0)