From f80bacb50c30995a0688f77f2ce6cd72fcb60aef Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 16 Feb 2022 20:22:36 +0000 Subject: [PATCH] fix(runfiles): use normalized paths when guarding runfiles root and node_modules on Windows --- internal/node/launcher.sh | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/internal/node/launcher.sh b/internal/node/launcher.sh index 4f0a7d01c6..bff693097c 100644 --- a/internal/node/launcher.sh +++ b/internal/node/launcher.sh @@ -13,6 +13,34 @@ # See the License for the specific language governing permissions and # limitations under the License. +# 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 { + # Determine if we are running on a Windows environment + # that can generate problematic paths (excludes WSL as it acts like unix) + case "$(uname -s)" in + CYGWIN*) local IS_WINDOWS=true ;; + MINGW*) local IS_WINDOWS=true ;; + MSYS_NT*) local IS_WINDOWS=true ;; + *) local IS_WINDOWS=false ;; + esac + + # In case we are running on Windows apply the paths transformations + # -process driver letter + # -convert path separator + # -lowercase everything + if $IS_WINDOWS ; then + local retval=$(sed -e 's#^\(.\):#/\L\1#' -e 's#\\#/#g' -e 's/[A-Z]/\L&/g' <<< "$1") + echo "$retval" + return + fi + + # otherwise doesnt change the given path and just return the argument as is + echo "$1" +} + # --- begin runfiles.bash initialization v2 --- # Copy-pasted from the Bazel Bash runfiles library v2. set -uo pipefail; f=build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel/tools/bash/runfiles/runfiles.bash @@ -49,7 +77,9 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ # Case 6a is handled like case 3. if [[ -n "${RUNFILES_MANIFEST_ONLY:-}" ]]; then # Windows only has a manifest file instead of symlinks. - RUNFILES=${RUNFILES_MANIFEST_FILE%/MANIFEST} + # 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) elif [[ -n "${TEST_SRCDIR:-}" ]]; then # Case 4, bazel has identified runfiles for us. RUNFILES="${TEST_SRCDIR:-}"