Skip to content

Commit

Permalink
fix(builtin): use updated rules_js launcher logic to source RUNFILES (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide authored Sep 27, 2022
1 parent 805116e commit c725169
Showing 1 changed file with 65 additions and 51 deletions.
116 changes: 65 additions & 51 deletions internal/node/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.

# It helps to determine if we are running on a Windows environment (excludes WSL as it acts like Unix)
function isWindows {
function is_windows {
case "$(uname -s)" in
CYGWIN*) local IS_WINDOWS=1 ;;
MINGW*) local IS_WINDOWS=1 ;;
Expand All @@ -29,14 +29,13 @@ function isWindows {
# It helps to normalizes paths when running on Windows.
#
# Example:
# C:/Users/XUser/_bazel_XUser/7q7kkv32/execroot/A/b/C -> /c/users/xuser/_bazel_xuser/7q7kkv32/execroot/a/b/c
function normalizeWindowsPath {
# Apply the followings paths transformations to normalize paths on Windows
# -process driver letter
# -convert path separator
# -lowercase everything
echo $(sed -e 's#^\(.\):#/\L\1#' -e 's#\\#/#g' -e 's/[A-Z]/\L&/g' <<< "$1")
return
# C:/Users/XUser/_bazel_XUser/7q7kkv32/execroot/A/b/C -> /c/Users/XUser/_bazel_XUser/7q7kkv32/execroot/A/b/C
function normalize_windows_path {
# Apply the followings paths transformations to normalize paths on Windows
# -process driver letter
# -convert path separator
sed -e 's#^\(.\):#/\L\1#' -e 's#\\#/#g' <<< "$1"
return
}

# --- begin runfiles.bash initialization v2 ---
Expand Down Expand Up @@ -73,51 +72,66 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
# blaze.
# Case 5a is handled like case 1.
# Case 6a is handled like case 3.
if [[ -n "${RUNFILES_MANIFEST_ONLY:-}" ]]; then
# Windows only has a manifest file instead of symlinks.
if [[ $(isWindows) -eq "1" ]]; then
# If Windows normalizing the path and case insensitive removing the `/MANIFEST` part of the path
NORMALIZED_RUNFILES_MANIFEST_FILE_PATH=$(normalizeWindowsPath $RUNFILES_MANIFEST_FILE)
RUNFILES=$(sed 's|\/MANIFEST$||i' <<< $NORMALIZED_RUNFILES_MANIFEST_FILE_PATH)
else
RUNFILES=${RUNFILES_MANIFEST_FILE%/MANIFEST}
fi
elif [[ -n "${TEST_SRCDIR:-}" ]]; then
# Case 4, bazel has identified runfiles for us.
RUNFILES="${TEST_SRCDIR:-}"
else
case "$0" in
/*) self="$0" ;;
*) self="$PWD/$0" ;;
esac
while true; do
if [[ -e "$self.runfiles" ]]; then
RUNFILES="$self.runfiles"
break
fi

if [[ $self == *.runfiles/* ]]; then
RUNFILES="${self%%.runfiles/*}.runfiles"
# don't break; this is a last resort for case 6b
fi

if [[ ! -L "$self" ]]; then
break;
if [ "${TEST_SRCDIR:-}" ]; then
# Case 4, bazel has identified runfiles for us.
RUNFILES="$TEST_SRCDIR"
elif [ "${RUNFILES_MANIFEST_FILE:-}" ]; then
if [ "$(is_windows)" -eq "1" ]; then
# If Windows, normalize the path
NORMALIZED_RUNFILES_MANIFEST_FILE=$(normalize_windows_path "$RUNFILES_MANIFEST_FILE")
else
NORMALIZED_RUNFILES_MANIFEST_FILE="$RUNFILES_MANIFEST_FILE"
fi

readlink="$(readlink "$self")"
if [[ "$readlink" = /* ]]; then
self="$readlink"
if [[ "${NORMALIZED_RUNFILES_MANIFEST_FILE}" == *.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=${NORMALIZED_RUNFILES_MANIFEST_FILE%_manifest}
elif [[ "${NORMALIZED_RUNFILES_MANIFEST_FILE}" == */MANIFEST ]]; then
# Older versions of Bazel put the manifest file named MANIFEST in the runfiles directory
RUNFILES=${NORMALIZED_RUNFILES_MANIFEST_FILE%/MANIFEST}
else
# resolve relative symlink
self="${self%%/*}/$readlink"
echo "\n>>>> FAIL: Unexpected RUNFILES_MANIFEST_FILE value $RUNFILES_MANIFEST_FILE. <<<<\n\n" >&2
exit 1
fi
done

if [[ -z "$RUNFILES" ]]; then
echo " >>>> FAIL: RUNFILES environment variable is not set. <<<<" >&2
exit 1
fi
else
case "$0" in
/*) self="$0" ;;
*) self="$PWD/$0" ;;
esac
while true; do
if [ -e "$self.runfiles" ]; then
RUNFILES="$self.runfiles"
break
fi

if [[ "$self" == *.runfiles/* ]]; then
RUNFILES="${self%%.runfiles/*}.runfiles"
# don't break; this is a last resort for case 6b
fi

if [ ! -L "$self" ]; then
break;
fi

readlink="$(readlink "$self")"
if [[ "$readlink" == /* ]]; then
self="$readlink"
else
# resolve relative symlink
self="${self%%/*}/$readlink"
fi
done

if [ -z "${RUNFILES:-}" ]; then
echo "\n>>>> FAIL: RUNFILES environment variable is not set. <<<<\n\n" >&2
exit 1
fi
fi
if [ "${RUNFILES:0:1}" != "/" ]; then
# Ensure 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"
fi
export RUNFILES
# --- end RUNFILES initialization ---
Expand Down

0 comments on commit c725169

Please sign in to comment.