From 8b5cd90af1e6fa9de98337c263bbd10ff150592f Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 5 Sep 2019 15:10:55 -0700 Subject: [PATCH] feat(builtin): expose the new linker to node programs If the magic --bazel_node_modules_manifest flag is passed to a nodejs_binary, we peel it off and run the linker first This lets programs avoid having custom resolution logic. --- internal/linker/BUILD.bazel | 8 ++++++++ internal/node/node.bzl | 7 +++++++ internal/node/node_launcher.sh | 7 +++++++ 3 files changed, 22 insertions(+) diff --git a/internal/linker/BUILD.bazel b/internal/linker/BUILD.bazel index 41ca067b02..834f9c0d79 100644 --- a/internal/linker/BUILD.bazel +++ b/internal/linker/BUILD.bazel @@ -1,3 +1,5 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + exports_files(["link_node_modules.js"]) filegroup( @@ -10,3 +12,9 @@ filegroup( ], visibility = ["//:__pkg__"], ) + +bzl_library( + name = "bzl", + srcs = glob(["*.bzl"]), + visibility = ["//visibility:public"], +) diff --git a/internal/node/node.bzl b/internal/node/node.bzl index 15af9aac56..4d61940950 100644 --- a/internal/node/node.bzl +++ b/internal/node/node.bzl @@ -169,6 +169,8 @@ def _nodejs_binary_impl(ctx): # attempts to do bazel run fail("The node toolchain was not properly configured so %s cannot be executed. Make sure that target_tool_path or target_tool is set." % ctx.attr.name) + node_tool_files.append(ctx.file._link_modules_script) + if not ctx.outputs.templated_args_file: templated_args = ctx.attr.templated_args else: @@ -201,6 +203,7 @@ def _nodejs_binary_impl(ctx): ]), "TEMPLATED_env_vars": env_vars, "TEMPLATED_expected_exit_code": str(expected_exit_code), + "TEMPLATED_link_modules_script": _to_manifest_path(ctx, ctx.file._link_modules_script), "TEMPLATED_node": node_tool_info.target_tool_path, "TEMPLATED_repository_args": _to_manifest_path(ctx, ctx.file._repository_args), "TEMPLATED_script_path": script_path, @@ -425,6 +428,10 @@ The set of default environment variables is: default = Label("//internal/node:node_launcher.sh"), allow_single_file = True, ), + "_link_modules_script": attr.label( + default = Label("//internal/linker:link_node_modules.js"), + allow_single_file = True, + ), "_loader_template": attr.label( default = Label("//internal/node:node_loader.js"), allow_single_file = True, diff --git a/internal/node/node_launcher.sh b/internal/node/node_launcher.sh index 3a2bb376f2..e70f580d67 100644 --- a/internal/node/node_launcher.sh +++ b/internal/node/node_launcher.sh @@ -117,6 +117,7 @@ TEMPLATED_env_vars readonly node=$(rlocation "TEMPLATED_node") readonly repository_args=$(rlocation "TEMPLATED_repository_args") readonly script=$(rlocation "TEMPLATED_script_path") +readonly link_modules_script=$(rlocation "TEMPLATED_link_modules_script") source $repository_args @@ -125,11 +126,17 @@ NODE_OPTIONS=() ALL_ARGS=(TEMPLATED_args $NODE_REPOSITORY_ARGS "$@") for ARG in "${ALL_ARGS[@]}"; do case "$ARG" in + --bazel_node_modules_manifest=*) MODULES_MANIFEST="${ARG#--bazel_node_modules_manifest=}" ;; --node_options=*) NODE_OPTIONS+=( "${ARG#--node_options=}" ) ;; *) ARGS+=( "$ARG" ) esac done +# Link the first-party modules into node_modules directory before running the actual program +if [[ -n "$MODULES_MANIFEST" ]]; then + "${node}" "${link_modules_script}" "${MODULES_MANIFEST}" +fi + # The EXPECTED_EXIT_CODE lets us write bazel tests which assert that # a binary fails to run. Otherwise any failure would make such a test # fail before we could assert that we expected that failure.