Skip to content

Commit

Permalink
feat: add depset support to run_node inputs, matching ctx.action.run
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhw authored and alexeagle committed Jul 10, 2020
1 parent 9180eea commit ee584f8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
14 changes: 11 additions & 3 deletions internal/providers/node_runtime_deps_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ def run_node(ctx, inputs, arguments, executable, **kwargs):
exec_exec = getattr(ctx.executable, executable)

outputs = kwargs.pop("outputs", [])
extra_inputs = []
extra_inputs = depset()
link_data = []
if (NodeRuntimeDepsInfo in exec_attr):
extra_inputs = exec_attr[NodeRuntimeDepsInfo].deps.to_list()
extra_inputs = exec_attr[NodeRuntimeDepsInfo].deps
link_data = exec_attr[NodeRuntimeDepsInfo].pkgs

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

# ctx.actions.run accepts both lists and a depset for inputs. Coerce the original inputs to a
# depset if they're a list, so that extra inputs can be combined in a performant manner.
inputs_depset = depset(transitive = [
depset(direct = inputs) if type(inputs) == "list" else inputs,
extra_inputs,
depset(direct = [modules_manifest]),
])

ctx.actions.run(
outputs = outputs,
inputs = inputs + extra_inputs + [modules_manifest],
inputs = inputs_depset,
arguments = arguments,
executable = exec_exec,
env = env,
Expand Down
7 changes: 7 additions & 0 deletions internal/providers/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ nodejs_binary(
entry_point = "js-write-file.js",
)

nodejs_binary(
name = "cloner_bin",
entry_point = "js-clone-file.js",
)

js_write_file(
name = "write_file",
content = "test file content",
Expand All @@ -32,5 +37,7 @@ js_write_file(
for file in [
"out",
"out2",
"out3",
"out4",
]
]
9 changes: 9 additions & 0 deletions internal/providers/test/js-clone-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const readFileSync = require('fs').readFileSync;
const writeFileSync = require('fs').writeFileSync;

const inputPath = process.argv[2];
const outputPath = process.argv[3];

const content = readFileSync(inputPath, 'utf8');

writeFileSync(outputPath, content, {encoding: 'utf8'});
39 changes: 39 additions & 0 deletions internal/providers/test/run_node_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,53 @@ def _js_write_file_impl(ctx):
outputs = [ctx.outputs.out2],
)

content_txt = ctx.actions.declare_file("content.txt")
ctx.actions.write(
output = content_txt,
content = ctx.attr.content,
)

run_node(
ctx = ctx,
executable = "_clone",
mnemonic = "cloner",
# Pass inputs as a list.
inputs = [content_txt],
arguments = [
content_txt.path,
ctx.outputs.out3.path,
],
outputs = [ctx.outputs.out3],
)

run_node(
ctx = ctx,
executable = "_clone",
mnemonic = "cloner",
# Pass inputs as a depset.
inputs = depset(direct = [content_txt]),
arguments = [
content_txt.path,
ctx.outputs.out4.path,
],
outputs = [ctx.outputs.out4],
)

js_write_file = rule(
implementation = _js_write_file_impl,
outputs = {
"out": "out.txt",
"out2": "out2.txt",
"out3": "out3.txt",
"out4": "out4.txt",
},
attrs = {
"content": attr.string(),
"_clone": attr.label(
default = Label("//internal/providers/test:cloner_bin"),
cfg = "host",
executable = True,
),
"_writer": attr.label(
default = Label("//internal/providers/test:writer_bin"),
cfg = "host",
Expand Down

0 comments on commit ee584f8

Please sign in to comment.