Skip to content

Commit

Permalink
refactor: register_node_modules_linker just writes the manifest
Browse files Browse the repository at this point in the history
This makes it easier to reuse in nodejs_binary
  • Loading branch information
alexeagle authored and gregmagolan committed Dec 10, 2019
1 parent 6c8e55d commit 3c41162
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
15 changes: 6 additions & 9 deletions internal/linker/link_node_modules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ def add_arg(args, arg):
else:
args.add(arg)

def register_node_modules_linker(ctx, args, inputs, data = []):
"""Helps an action to run node by setting up the node_modules linker as a pre-process
def write_node_modules_manifest(ctx, extra_data = []):
"""Writes a manifest file read by the linker, containing info about resolving runtime dependencies
Args:
ctx: Bazel's starlark execution context, used to get attributes and actions
args: Arguments being passed to the program; a linker argument will be appended
inputs: inputs being passed to the program; a linker input will be appended
data: labels to search for npm packages that need to be linked (ctx.attr.deps and ctx.attr.data will always be searched)
ctx: starlark rule execution context
extra_data: labels to search for npm packages that need to be linked (ctx.attr.deps and ctx.attr.data will always be searched)
"""

mappings = {
Expand All @@ -53,7 +51,7 @@ def register_node_modules_linker(ctx, args, inputs, data = []):
node_modules_root = ""

# Look through data/deps attributes to find...
for dep in data + getattr(ctx.attr, "data", []) + getattr(ctx.attr, "deps", []):
for dep in extra_data + getattr(ctx.attr, "data", []) + getattr(ctx.attr, "deps", []):
# ...the root directory for the third-party node_modules; we'll symlink the local "node_modules" to it
if NpmPackageInfo in dep:
possible_root = "/".join([dep[NpmPackageInfo].workspace, "node_modules"])
Expand All @@ -80,8 +78,7 @@ def register_node_modules_linker(ctx, args, inputs, data = []):
"workspace": ctx.workspace_name,
}
ctx.actions.write(modules_manifest, str(content))
add_arg(args, "--bazel_node_modules_manifest=%s" % modules_manifest.path)
inputs.append(modules_manifest)
return modules_manifest

def get_module_mappings(label, attrs, vars, rule_kind, srcs = [], workspace_name = None):
"""Returns the module_mappings from the given attrs.
Expand Down
17 changes: 8 additions & 9 deletions internal/linker/test/integration/rule.bzl
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"Minimal fixture for executing the linker's starlark code"

load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module_mappings_aspect", "register_node_modules_linker")
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module_mappings_aspect", "write_node_modules_manifest")

def _linked(ctx):
inputs = ctx.files.deps[:]
outputs = [ctx.outputs.out]
args = ctx.actions.args()
register_node_modules_linker(ctx, args, inputs)
args.add(ctx.outputs.out.path)
modules_manifest = write_node_modules_manifest(ctx)
ctx.actions.run(
inputs = inputs,
outputs = outputs,
inputs = ctx.files.deps + [modules_manifest],
outputs = [ctx.outputs.out],
executable = ctx.executable.program,
arguments = [args],
arguments = [
"--bazel_node_modules_manifest=%s" % modules_manifest.path,
ctx.outputs.out.path,
],
)

linked = rule(_linked, attrs = {
Expand Down
6 changes: 4 additions & 2 deletions internal/providers/node_runtime_deps_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

""" Custom provider that mimics the Runfiles, but doesn't incur the expense of creating the runfiles symlink tree"""

load("//internal/linker:link_node_modules.bzl", "add_arg", "register_node_modules_linker")
load("//internal/linker:link_node_modules.bzl", "add_arg", "write_node_modules_manifest")

NodeRuntimeDepsInfo = provider(
doc = """Stores runtime dependencies of a nodejs_binary or nodejs_test
Expand Down Expand Up @@ -54,7 +54,9 @@ def run_node(ctx, inputs, arguments, executable, **kwargs):
extra_inputs = exec_attr[NodeRuntimeDepsInfo].deps.to_list()
link_data = exec_attr[NodeRuntimeDepsInfo].pkgs

register_node_modules_linker(ctx, arguments, inputs, link_data)
modules_manifest = write_node_modules_manifest(ctx, link_data)
add_arg(arguments, "--bazel_node_modules_manifest=%s" % modules_manifest.path)
inputs.append(modules_manifest)

# By using the run_node helper, you suggest that your program
# doesn't implicitly use runfiles to require() things
Expand Down

0 comments on commit 3c41162

Please sign in to comment.