From dfd0887f8b1d9cfbdb8d45bef41988386c13eef6 Mon Sep 17 00:00:00 2001 From: Jesse Schalken Date: Mon, 10 Jul 2023 14:34:53 +1000 Subject: [PATCH] fix entrypoint Custom MAIN Signed-off-by: Jordan Mele --- internal/node/launcher.sh | 20 +++++++------------- internal/node/loader.js | 3 +-- internal/node/node.bzl | 17 ++++------------- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/internal/node/launcher.sh b/internal/node/launcher.sh index 2ca4118ba..cc72fa5ea 100755 --- a/internal/node/launcher.sh +++ b/internal/node/launcher.sh @@ -152,6 +152,8 @@ else fi fi +MAIN="${RUNFILES}/TEMPLATED_entry_point_manifest" + # Export the location of the runfiles helpers script export BAZEL_NODE_RUNFILES_HELPER=$(rlocation "TEMPLATED_runfiles_helper_script") # Paths can be with lower and upper case on windows because of the msys64 package in the powershell @@ -214,6 +216,11 @@ for ARG in ${ALL_ARGS[@]+"${ALL_ARGS[@]}"}; do --bazel_node_working_dir=*) NODE_WORKING_DIR="${ARG#--bazel_node_working_dir=}" ;; # Let users pass through arguments to node itself --node_options=*) USER_NODE_OPTIONS+=( "${ARG#--node_options=}" ) ;; + # Legacy execroot references + --bazel_use_legacy_execroot_paths__variant_rlocation_main) \ + MAIN=$(rlocation "TEMPLATED_entry_point_manifest") ;; + --bazel_use_legacy_execroot_paths__variant_execroot_main) \ + MAIN="${PWD}/"TEMPLATED_entry_point_execroot_path ;; # Remaining argv is collected to pass to the program *) ARGS+=( "$ARG" ) esac @@ -348,22 +355,9 @@ if [ "$PATCH_REQUIRE" = true ]; then # Change the entry point to be the loader.js script so we run code before node MAIN=$(rlocation "TEMPLATED_loader_script") else - # Entry point is the user-supplied script - MAIN="${PWD}/"TEMPLATED_entry_point_execroot_path - # TODO: after we link-all-bins we should not need this extra lookup - if [[ ! -f "$MAIN" ]]; then - if [ "$FROM_EXECROOT" = true ]; then - MAIN="$EXECROOT/"TEMPLATED_entry_point_execroot_path - else - MAIN=TEMPLATED_entry_point_manifest_path - fi - fi # Always set up source-map-support using our vendored copy, just like the require_patch_script register_source_map_support=$(rlocation build_bazel_rules_nodejs/third_party/github.com/source-map-support/register.js) LAUNCHER_NODE_OPTIONS+=( "--require" "${register_source_map_support}" ) - if [[ -n "TEMPLATED_entry_point_main" ]]; then - MAIN="${MAIN}/"TEMPLATED_entry_point_main - fi fi # The EXPECTED_EXIT_CODE lets us write bazel tests which assert that diff --git a/internal/node/loader.js b/internal/node/loader.js index dd7bc0b4b..e073403bd 100755 --- a/internal/node/loader.js +++ b/internal/node/loader.js @@ -28,8 +28,7 @@ if (require.main === module) { // argv[0] == node, argv[1] == entry point. // NB: 'TEMPLATED_entry_point_path' & 'TEMPLATED_entry_point' below are replaced during the build process. var entryPointPath = 'TEMPLATED_entry_point_path'; - var entryPointMain = 'TEMPLATED_entry_point_main'; - var mainScript = process.argv[1] = entryPointMain ? `${entryPointPath}/${entryPointMain}` : entryPointPath; + var mainScript = process.argv[1] = entryPointPath; try { module.constructor._load(mainScript, this, /*isMain=*/true); } catch (e) { diff --git a/internal/node/node.bzl b/internal/node/node.bzl index 0f638a776..311bb6268 100755 --- a/internal/node/node.bzl +++ b/internal/node/node.bzl @@ -120,9 +120,7 @@ def _write_loader_script(ctx): substitutions = {} substitutions["TEMPLATED_entry_point_path"] = _ts_to_js(_to_manifest_path(ctx, _get_entry_point_file(ctx))) if DirectoryFilePathInfo in ctx.attr.entry_point: - substitutions["TEMPLATED_entry_point_main"] = ctx.attr.entry_point[DirectoryFilePathInfo].path - else: - substitutions["TEMPLATED_entry_point_main"] = "" + fail("Not supported") ctx.actions.expand_template( template = ctx.file._loader_template, @@ -317,18 +315,11 @@ fi "TEMPLATED_vendored_node": "" if is_builtin else strip_external(ctx.file._node.path), } - # TODO when we have "link_all_bins" we will only need to look in one place for the entry point - #if ctx.file.entry_point.is_source: - # substitutions["TEMPLATED_script_path"] = "\"%s\"" % _to_execroot_path(ctx, ctx.file.entry_point) - #else: - # substitutions["TEMPLATED_script_path"] = "$(rlocation \"%s\")" % _to_manifest_path(ctx, ctx.file.entry_point) - # For now we need to look in both places + substitutions["TEMPLATED_entry_point_manifest"] = _ts_to_js(_to_manifest_path(ctx, _get_entry_point_file(ctx))) + # Needed for tests using legacy implementation substitutions["TEMPLATED_entry_point_execroot_path"] = "\"%s\"" % _ts_to_js(_to_execroot_path(ctx, _get_entry_point_file(ctx))) - substitutions["TEMPLATED_entry_point_manifest_path"] = "$(rlocation \"%s\")" % _ts_to_js(_to_manifest_path(ctx, _get_entry_point_file(ctx))) if DirectoryFilePathInfo in ctx.attr.entry_point: - substitutions["TEMPLATED_entry_point_main"] = ctx.attr.entry_point[DirectoryFilePathInfo].path - else: - substitutions["TEMPLATED_entry_point_main"] = "" + fail("Not supported") ctx.actions.expand_template( template = ctx.file._launcher_template,