Skip to content

Commit ea4ab7d

Browse files
fmeumcopybara-github
authored andcommitted
Use case-insensitive comparison for Windows paths in runfiles.bash
When matching paths or path segments in the Bash runfiles library, use a case-insensitive comparison when running in MSYS2 as Bazel emits paths that can be capitalized differently. In particular, this fixes failures when the output base path contains uppercase letters. Closes #19602. PiperOrigin-RevId: 568323218 Change-Id: Ic4655b82b1acbaa9ed50118913d713fba3d7f5de
1 parent 4c15434 commit ea4ab7d

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

tools/bash/runfiles/runfiles.bash

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,21 @@ case "$(uname -s | tr [:upper:] [:lower:])" in
9898
msys*|mingw*|cygwin*)
9999
# matches an absolute Windows path
100100
export _RLOCATION_ISABS_PATTERN="^[a-zA-Z]:[/\\]"
101+
# Windows paths are case insensitive and Bazel and MSYS2 capitalize differently, so we can't
102+
# assume that all paths are in the same native case.
103+
export _RLOCATION_GREP_CASE_INSENSITIVE_ARGS=-i
101104
;;
102105
*)
103106
# matches an absolute Unix path
104107
export _RLOCATION_ISABS_PATTERN="^/[^/].*"
108+
export _RLOCATION_GREP_CASE_INSENSITIVE_ARGS=
105109
;;
106110
esac
107111

108-
# Does not exit with a non-zero exit code if no match is found.
112+
# Does not exit with a non-zero exit code if no match is found and performs a case-insensitive
113+
# search on Windows.
109114
function __runfiles_maybe_grep() {
110-
grep "$@" || test $? = 1;
115+
grep $_RLOCATION_GREP_CASE_INSENSITIVE_ARGS "$@" || test $? = 1;
111116
}
112117
export -f __runfiles_maybe_grep
113118

@@ -251,8 +256,13 @@ function runfiles_current_repository() {
251256
# If the runfiles directory exists, check if the caller's path is of the form
252257
# $RUNFILES_DIR/rlocation_path and if so, set $rlocation_path.
253258
if [[ -z "$rlocation_path" && -d "${RUNFILES_DIR:-/dev/null}" ]]; then
254-
local -r normalized_caller_path="$(echo "$caller_path" | sed 's|\\\\*|/|g')"
255-
local -r normalized_dir="$(echo "${RUNFILES_DIR%[\/]}" | sed 's|\\\\*|/|g')"
259+
normalized_caller_path="$(echo "$caller_path" | sed 's|\\\\*|/|g')"
260+
normalized_dir="$(echo "${RUNFILES_DIR%[\/]}" | sed 's|\\\\*|/|g')"
261+
if [[ -n "${_RLOCATION_GREP_CASE_INSENSITIVE_ARGS}" ]]; then
262+
# When comparing file paths insensitively, also normalize the case of the prefixes.
263+
normalized_caller_path=$(echo "$normalized_caller_path" | tr '[:upper:]' '[:lower:]')
264+
normalized_dir=$(echo "$normalized_dir" | tr '[:upper:]' '[:lower:]')
265+
fi
256266
if [[ "$normalized_caller_path" == "$normalized_dir"/* ]]; then
257267
rlocation_path=${normalized_caller_path:${#normalized_dir}}
258268
rlocation_path=${rlocation_path:1}

0 commit comments

Comments
 (0)