refactor: no longer export RUNFILES in js_binary; instead only export JS_BINARY__RUNFILES
diff --git a/js/private/bash.bzl b/js/private/bash.bzl index 6095ec0..2c7c2e5 100644 --- a/js/private/bash.bzl +++ b/js/private/bash.bzl
@@ -1,13 +1,10 @@ "Bash snippets for js rules" -# TODO(2.0): Instead of setting a new RUNFILES env; just set RUNFILES_DIR if it is not set; -# needs testing to know if RUNFILES_DIR is set always set to the same value as RUNFILES -# when it is set. -# Bash snipped to initialize the RUNFILES environment variable. +# Bash snipped to initialize the JS_BINARY__RUNFILES environment variable. # Depends on there being a logf_fatal function defined. # NB: If this can be generalized fully in the future and not depend on logf_fatal # then it could be hoisted to bazel-lib where we have other bash snippets. -BASH_INITIALIZE_RUNFILES = r""" +BASH_INITIALIZE_JS_BINARY_RUNFILES = r""" # It helps to determine if we are running on a Windows environment (excludes WSL as it acts like Unix) case "$(uname -s)" in CYGWIN*) _IS_WINDOWS=1 ;; @@ -32,9 +29,9 @@ return } -# Set a RUNFILES environment variable to the root of the runfiles tree +# Set a JS_BINARY__RUNFILES environment variable to the root of the runfiles tree # since RUNFILES_DIR is not set by Bazel in all contexts. -# For example, `RUNFILES=/path/to/my_js_binary.sh.runfiles`. +# For example, `JS_BINARY__RUNFILES=/path/to/my_js_binary.sh.runfiles`. # # Call this program X. X was generated by a genrule and may be invoked # in many ways: @@ -57,17 +54,17 @@ # Case 6a is handled like case 3. if [ "${TEST_SRCDIR:-}" ]; then # Case 4, bazel has identified runfiles for us. - RUNFILES=$(_normalize_path "$TEST_SRCDIR") + JS_BINARY__RUNFILES=$(_normalize_path "$TEST_SRCDIR") elif [ "${RUNFILES_MANIFEST_FILE:-}" ]; then - RUNFILES=$(_normalize_path "$RUNFILES_MANIFEST_FILE") - if [[ "${RUNFILES}" == *.runfiles_manifest ]]; then + JS_BINARY__RUNFILES=$(_normalize_path "$RUNFILES_MANIFEST_FILE") + if [[ "${JS_BINARY__RUNFILES}" == *.runfiles_manifest ]]; then # Newer versions of Bazel put the manifest besides the runfiles with the suffix .runfiles_manifest. # For example, the runfiles directory is named my_binary.runfiles then the manifest is beside the # runfiles directory and named my_binary.runfiles_manifest - RUNFILES=${RUNFILES%_manifest} - elif [[ "${RUNFILES}" == */MANIFEST ]]; then + JS_BINARY__RUNFILES=${JS_BINARY__RUNFILES%_manifest} + elif [[ "${JS_BINARY__RUNFILES}" == */MANIFEST ]]; then # Older versions of Bazel put the manifest file named MANIFEST in the runfiles directory - RUNFILES=${RUNFILES%/MANIFEST} + JS_BINARY__RUNFILES=${JS_BINARY__RUNFILES%/MANIFEST} else logf_fatal "Unexpected RUNFILES_MANIFEST_FILE value $RUNFILES_MANIFEST_FILE" exit 1 @@ -79,12 +76,12 @@ esac while true; do if [ -e "$self.runfiles" ]; then - RUNFILES="$self.runfiles" + JS_BINARY__RUNFILES="$self.runfiles" break fi if [[ "$self" == *.runfiles/* ]]; then - RUNFILES="${self%%.runfiles/*}.runfiles" + JS_BINARY__RUNFILES="${self%%.runfiles/*}.runfiles" # don't break; this is a last resort for case 6b fi @@ -101,16 +98,16 @@ fi done - if [ -z "${RUNFILES:-}" ]; then - logf_fatal "RUNFILES environment variable is not set" + if [ -z "${JS_BINARY__RUNFILES:-}" ]; then + logf_fatal "JS_BINARY__RUNFILES environment variable is not set" exit 1 fi - RUNFILES=$(_normalize_path "$RUNFILES") + JS_BINARY__RUNFILES=$(_normalize_path "$JS_BINARY__RUNFILES") fi -if [ "${RUNFILES:0:1}" != "/" ]; then - # Ensure RUNFILES set above is an absolute path. It may be a path relative +if [ "${JS_BINARY__RUNFILES:0:1}" != "/" ]; then + # Ensure JS_BINARY__RUNFILES set above is an absolute path. It may be a path relative # to the PWD in case where RUNFILES_MANIFEST_FILE is used above. - RUNFILES="$PWD/$RUNFILES" + JS_BINARY__RUNFILES="$PWD/$JS_BINARY__RUNFILES" fi -""" +export JS_BINARY__RUNFILES"""
diff --git a/js/private/coverage/bundle/c8.js b/js/private/coverage/bundle/c8.js index f649816..ffa4013 100644 --- a/js/private/coverage/bundle/c8.js +++ b/js/private/coverage/bundle/c8.js
@@ -22,7 +22,10 @@ // TODO: can or should we instrument files from other repositories as well? // if so then the path.join call below will yield invalid paths since files will have external/wksp as their prefix. -const pwd = path.join(process.env.RUNFILES, process.env.TEST_WORKSPACE) +const pwd = path.join( + process.env.JS_BINARY__RUNFILES, + process.env.TEST_WORKSPACE +) process.chdir(pwd) new Report({
diff --git a/js/private/coverage/coverage.js b/js/private/coverage/coverage.js index fb5583a..55e6a30 100644 --- a/js/private/coverage/coverage.js +++ b/js/private/coverage/coverage.js
@@ -11046,7 +11046,10 @@ // TODO: can or should we instrument files from other repositories as well? // if so then the path.join call below will yield invalid paths since files will have external/wksp as their prefix. -const pwd = require$$0.join(process.env.RUNFILES, process.env.TEST_WORKSPACE); +const pwd = require$$0.join( + process.env.JS_BINARY__RUNFILES, + process.env.TEST_WORKSPACE +); process.chdir(pwd); new Report({
diff --git a/js/private/coverage/coverage.sh.tpl b/js/private/coverage/coverage.sh.tpl index 05ba71c..ab88d59 100644 --- a/js/private/coverage/coverage.sh.tpl +++ b/js/private/coverage/coverage.sh.tpl
@@ -15,31 +15,29 @@ } # ============================================================================== -# Initialize RUNFILES environment variable +# Initialize JS_BINARY__RUNFILES environment variable # ============================================================================== -{{initialize_runfiles}} +{{initialize_js_binary_runfiles}} # When --experimental_split_coverage_postprocessing is enabled, bazel creates # separate runfiles directory for the coverage merger. # When --experimental_split_coverage_postprocessing is disabled we observe the issue # in https://github.com/bazelbuild/bazel/issues/4033 if [ $SPLIT_COVERAGE_POST_PROCESSING == 1 ]; then - RUNFILES=$(_normalize_path "$LCOV_MERGER.runfiles") + JS_BINARY__RUNFILES=$(_normalize_path "$LCOV_MERGER.runfiles") fi -export RUNFILES - # ============================================================================== # Prepare to run coverage program # ============================================================================== -entry_point="$RUNFILES/{{workspace_name}}/{{entry_point_path}}" +entry_point="$JS_BINARY__RUNFILES/{{workspace_name}}/{{entry_point_path}}" if [ ! -f "$entry_point" ]; then printf "FATAL: the entry_point '%s' not found in runfiles" "$entry_point" exit 1 fi -node="$RUNFILES/{{workspace_name}}/{{node}}" +node="$JS_BINARY__RUNFILES/{{workspace_name}}/{{node}}" if [ ! -f "$node" ]; then logf_fatal "node binary '%s' not found in runfiles" "$node" exit 1
diff --git a/js/private/coverage/merger.bzl b/js/private/coverage/merger.bzl index d368138..b1d3ba8 100644 --- a/js/private/coverage/merger.bzl +++ b/js/private/coverage/merger.bzl
@@ -2,7 +2,7 @@ # Simple binary that call coverage.js with node toolchain load("@aspect_bazel_lib//lib:windows_utils.bzl", "create_windows_native_launcher_script") -load("//js/private:bash.bzl", "BASH_INITIALIZE_RUNFILES") +load("//js/private:bash.bzl", "BASH_INITIALIZE_JS_BINARY_RUNFILES") _ATTRS = { "entry_point": attr.label(default = Label("//js/private/coverage:coverage.js"), allow_single_file = [".js"]), @@ -32,7 +32,7 @@ output = bash_launcher, substitutions = { "{{entry_point_path}}": ctx.file.entry_point.short_path, - "{{initialize_runfiles}}": BASH_INITIALIZE_RUNFILES, + "{{initialize_js_binary_runfiles}}": BASH_INITIALIZE_JS_BINARY_RUNFILES, "{{node}}": node_path, "{{workspace_name}}": ctx.workspace_name, },
diff --git a/js/private/js_binary.bzl b/js/private/js_binary.bzl index 8cb1629..fd2412b 100644 --- a/js/private/js_binary.bzl +++ b/js/private/js_binary.bzl
@@ -21,7 +21,7 @@ load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "COPY_FILE_TO_BIN_TOOLCHAINS") load("@bazel_skylib//lib:dicts.bzl", "dicts") load(":js_helpers.bzl", "LOG_LEVELS", "envs_for_log_level", "gather_runfiles") -load(":bash.bzl", "BASH_INITIALIZE_RUNFILES") +load(":bash.bzl", "BASH_INITIALIZE_JS_BINARY_RUNFILES") _DOC = """Execute a program in the Node.js runtime. @@ -434,7 +434,7 @@ "{{entry_point_path}}": entry_point_path, "{{envs}}": "\n".join(envs), "{{fixed_args}}": " ".join(fixed_args_expanded), - "{{initialize_runfiles}}": BASH_INITIALIZE_RUNFILES, + "{{initialize_js_binary_runfiles}}": BASH_INITIALIZE_JS_BINARY_RUNFILES, "{{log_prefix_rule_set}}": log_prefix_rule_set, "{{log_prefix_rule}}": log_prefix_rule, "{{node_options}}": "\n".join(node_options),
diff --git a/js/private/js_binary.sh.tpl b/js/private/js_binary.sh.tpl index d985837..de9262d 100644 --- a/js/private/js_binary.sh.tpl +++ b/js/private/js_binary.sh.tpl
@@ -144,13 +144,9 @@ trap _exit EXIT # ============================================================================== -# Initialize RUNFILES environment variable +# Initialize JS_BINARY__RUNFILES environment variable # ============================================================================== -{{initialize_runfiles}} -# TODO(2.0): export only JS_BINARY__RUNFILES -export RUNFILES -JS_BINARY__RUNFILES="$RUNFILES" -export JS_BINARY__RUNFILES +{{initialize_js_binary_runfiles}} # ============================================================================== # Prepare to run main program
diff --git a/js/private/test/snapshots/wksp/launcher.sh b/js/private/test/snapshots/wksp/launcher.sh index 8f76884..1d4e3a1 100644 --- a/js/private/test/snapshots/wksp/launcher.sh +++ b/js/private/test/snapshots/wksp/launcher.sh
@@ -155,7 +155,7 @@ trap _exit EXIT # ============================================================================== -# Initialize RUNFILES environment variable +# Initialize JS_BINARY__RUNFILES environment variable # ============================================================================== # It helps to determine if we are running on a Windows environment (excludes WSL as it acts like Unix) @@ -182,9 +182,9 @@ return } -# Set a RUNFILES environment variable to the root of the runfiles tree +# Set a JS_BINARY__RUNFILES environment variable to the root of the runfiles tree # since RUNFILES_DIR is not set by Bazel in all contexts. -# For example, `RUNFILES=/path/to/my_js_binary.sh.runfiles`. +# For example, `JS_BINARY__RUNFILES=/path/to/my_js_binary.sh.runfiles`. # # Call this program X. X was generated by a genrule and may be invoked # in many ways: @@ -207,17 +207,17 @@ # Case 6a is handled like case 3. if [ "${TEST_SRCDIR:-}" ]; then # Case 4, bazel has identified runfiles for us. - RUNFILES=$(_normalize_path "$TEST_SRCDIR") + JS_BINARY__RUNFILES=$(_normalize_path "$TEST_SRCDIR") elif [ "${RUNFILES_MANIFEST_FILE:-}" ]; then - RUNFILES=$(_normalize_path "$RUNFILES_MANIFEST_FILE") - if [[ "${RUNFILES}" == *.runfiles_manifest ]]; then + JS_BINARY__RUNFILES=$(_normalize_path "$RUNFILES_MANIFEST_FILE") + if [[ "${JS_BINARY__RUNFILES}" == *.runfiles_manifest ]]; then # Newer versions of Bazel put the manifest besides the runfiles with the suffix .runfiles_manifest. # For example, the runfiles directory is named my_binary.runfiles then the manifest is beside the # runfiles directory and named my_binary.runfiles_manifest - RUNFILES=${RUNFILES%_manifest} - elif [[ "${RUNFILES}" == */MANIFEST ]]; then + JS_BINARY__RUNFILES=${JS_BINARY__RUNFILES%_manifest} + elif [[ "${JS_BINARY__RUNFILES}" == */MANIFEST ]]; then # Older versions of Bazel put the manifest file named MANIFEST in the runfiles directory - RUNFILES=${RUNFILES%/MANIFEST} + JS_BINARY__RUNFILES=${JS_BINARY__RUNFILES%/MANIFEST} else logf_fatal "Unexpected RUNFILES_MANIFEST_FILE value $RUNFILES_MANIFEST_FILE" exit 1 @@ -229,12 +229,12 @@ esac while true; do if [ -e "$self.runfiles" ]; then - RUNFILES="$self.runfiles" + JS_BINARY__RUNFILES="$self.runfiles" break fi if [[ "$self" == *.runfiles/* ]]; then - RUNFILES="${self%%.runfiles/*}.runfiles" + JS_BINARY__RUNFILES="${self%%.runfiles/*}.runfiles" # don't break; this is a last resort for case 6b fi @@ -251,22 +251,18 @@ fi done - if [ -z "${RUNFILES:-}" ]; then - logf_fatal "RUNFILES environment variable is not set" + if [ -z "${JS_BINARY__RUNFILES:-}" ]; then + logf_fatal "JS_BINARY__RUNFILES environment variable is not set" exit 1 fi - RUNFILES=$(_normalize_path "$RUNFILES") + JS_BINARY__RUNFILES=$(_normalize_path "$JS_BINARY__RUNFILES") fi -if [ "${RUNFILES:0:1}" != "/" ]; then - # Ensure RUNFILES set above is an absolute path. It may be a path relative +if [ "${JS_BINARY__RUNFILES:0:1}" != "/" ]; then + # Ensure JS_BINARY__RUNFILES set above is an absolute path. It may be a path relative # to the PWD in case where RUNFILES_MANIFEST_FILE is used above. - RUNFILES="$PWD/$RUNFILES" + JS_BINARY__RUNFILES="$PWD/$JS_BINARY__RUNFILES" fi - -# TODO(2.0): export only JS_BINARY__RUNFILES -export RUNFILES -JS_BINARY__RUNFILES="$RUNFILES" export JS_BINARY__RUNFILES # ==============================================================================