Skip to content

Commit 9a7aa21

Browse files
fmeumcopybara-github
authored andcommitted
Fix Bash runfiles_current_repository for tools
Shell scripts invoked as tools in actions are usually invoked with relative paths starting with `bazel-out`, which resulted in them being misclassified as from the main repo. Closes #17279. PiperOrigin-RevId: 503968803 Change-Id: Idec5d4ce576f0e5b0c8297128cc33ac928d5b04b
1 parent fcb0077 commit 9a7aa21

File tree

2 files changed

+213
-1
lines changed

2 files changed

+213
-1
lines changed

src/test/shell/bazel/bazel_rules_test.sh

+212
Original file line numberDiff line numberDiff line change
@@ -1035,4 +1035,216 @@ function test_bash_runfiles_current_repository_test_nobuild_runfile_links() {
10351035
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
10361036
}
10371037

1038+
function test_bash_runfiles_current_repository_action_binary_main_repo() {
1039+
touch MODULE.bazel
1040+
1041+
mkdir -p pkg
1042+
cat > pkg/BUILD.bazel <<'EOF'
1043+
genrule(
1044+
name = "gen",
1045+
outs = ["out"],
1046+
tools = [":binary"],
1047+
cmd = "$(location :binary) && touch $@",
1048+
)
1049+
1050+
sh_binary(
1051+
name = "binary",
1052+
srcs = ["binary.sh"],
1053+
deps = [
1054+
"@bazel_tools//tools/bash/runfiles",
1055+
],
1056+
visibility = ["//visibility:public"],
1057+
)
1058+
EOF
1059+
1060+
cat > pkg/binary.sh <<'EOF'
1061+
#!/usr/bin/env bash
1062+
# --- begin runfiles.bash initialization v2 ---
1063+
# Copy-pasted from the Bazel Bash runfiles library v2.
1064+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
1065+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
1066+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
1067+
source "$0.runfiles/$f" 2>/dev/null || \
1068+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
1069+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
1070+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
1071+
# --- end runfiles.bash initialization v2 ---
1072+
1073+
echo "in pkg/binary.sh: '$(runfiles_current_repository)'"
1074+
EOF
1075+
chmod +x pkg/binary.sh
1076+
1077+
bazel build --enable_bzlmod //pkg:gen &>"$TEST_log" || fail "Build should succeed"
1078+
expect_log "in pkg/binary.sh: ''"
1079+
}
1080+
1081+
function test_bash_runfiles_current_repository_action_generated_binary_main_repo() {
1082+
touch MODULE.bazel
1083+
1084+
mkdir -p pkg
1085+
cat > pkg/BUILD.bazel <<'EOF'
1086+
genrule(
1087+
name = "gen",
1088+
outs = ["out"],
1089+
tools = [":binary"],
1090+
cmd = "$(location :binary) && touch $@",
1091+
)
1092+
1093+
genrule(
1094+
name = "copy_binary",
1095+
outs = ["gen_binary.sh"],
1096+
srcs = ["binary.sh"],
1097+
cmd = "cp $(location binary.sh) $@",
1098+
)
1099+
1100+
sh_binary(
1101+
name = "binary",
1102+
srcs = ["binary.sh"],
1103+
deps = [
1104+
"@bazel_tools//tools/bash/runfiles",
1105+
],
1106+
visibility = ["//visibility:public"],
1107+
)
1108+
EOF
1109+
1110+
cat > pkg/binary.sh <<'EOF'
1111+
#!/usr/bin/env bash
1112+
# --- begin runfiles.bash initialization v2 ---
1113+
# Copy-pasted from the Bazel Bash runfiles library v2.
1114+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
1115+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
1116+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
1117+
source "$0.runfiles/$f" 2>/dev/null || \
1118+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
1119+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
1120+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
1121+
# --- end runfiles.bash initialization v2 ---
1122+
1123+
echo "in copy of pkg/gen_binary.sh: '$(runfiles_current_repository)'"
1124+
EOF
1125+
chmod +x pkg/binary.sh
1126+
1127+
bazel build --enable_bzlmod //pkg:gen &>"$TEST_log" || fail "Build should succeed"
1128+
expect_log "in copy of pkg/gen_binary.sh: ''"
1129+
}
1130+
1131+
function test_bash_runfiles_current_repository_action_binary_external_repo() {
1132+
touch MODULE.bazel
1133+
1134+
cat >> WORKSPACE <<'EOF'
1135+
local_repository(
1136+
name = "other_repo",
1137+
path = "other_repo",
1138+
)
1139+
EOF
1140+
1141+
mkdir -p pkg
1142+
cat > pkg/BUILD.bazel <<'EOF'
1143+
genrule(
1144+
name = "gen",
1145+
outs = ["out"],
1146+
tools = ["@other_repo//pkg:binary"],
1147+
cmd = "$(location @other_repo//pkg:binary) && touch $@",
1148+
)
1149+
EOF
1150+
1151+
mkdir -p other_repo
1152+
touch other_repo/WORKSPACE
1153+
1154+
mkdir -p other_repo/pkg
1155+
cat > other_repo/pkg/BUILD.bazel <<'EOF'
1156+
sh_binary(
1157+
name = "binary",
1158+
srcs = ["binary.sh"],
1159+
deps = [
1160+
"@bazel_tools//tools/bash/runfiles",
1161+
],
1162+
visibility = ["//visibility:public"],
1163+
)
1164+
EOF
1165+
1166+
cat > other_repo/pkg/binary.sh <<'EOF'
1167+
#!/usr/bin/env bash
1168+
# --- begin runfiles.bash initialization v2 ---
1169+
# Copy-pasted from the Bazel Bash runfiles library v2.
1170+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
1171+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
1172+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
1173+
source "$0.runfiles/$f" 2>/dev/null || \
1174+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
1175+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
1176+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
1177+
# --- end runfiles.bash initialization v2 ---
1178+
1179+
echo "in external/other_repo/pkg/binary.sh: '$(runfiles_current_repository)'"
1180+
EOF
1181+
chmod +x other_repo/pkg/binary.sh
1182+
1183+
bazel build --enable_bzlmod //pkg:gen &>"$TEST_log" || fail "Build should succeed"
1184+
expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'"
1185+
}
1186+
1187+
function test_bash_runfiles_current_repository_action_generated_binary_external_repo() {
1188+
touch MODULE.bazel
1189+
1190+
cat >> WORKSPACE <<'EOF'
1191+
local_repository(
1192+
name = "other_repo",
1193+
path = "other_repo",
1194+
)
1195+
EOF
1196+
1197+
mkdir -p pkg
1198+
cat > pkg/BUILD.bazel <<'EOF'
1199+
genrule(
1200+
name = "gen",
1201+
outs = ["out"],
1202+
tools = ["@other_repo//pkg:binary"],
1203+
cmd = "$(location @other_repo//pkg:binary) && touch $@",
1204+
)
1205+
EOF
1206+
1207+
mkdir -p other_repo
1208+
touch other_repo/WORKSPACE
1209+
1210+
mkdir -p other_repo/pkg
1211+
cat > other_repo/pkg/BUILD.bazel <<'EOF'
1212+
genrule(
1213+
name = "copy_binary",
1214+
outs = ["gen_binary.sh"],
1215+
srcs = ["binary.sh"],
1216+
cmd = "cp $(location binary.sh) $@",
1217+
)
1218+
1219+
sh_binary(
1220+
name = "binary",
1221+
srcs = ["gen_binary.sh"],
1222+
deps = [
1223+
"@bazel_tools//tools/bash/runfiles",
1224+
],
1225+
visibility = ["//visibility:public"],
1226+
)
1227+
EOF
1228+
1229+
cat > other_repo/pkg/binary.sh <<'EOF'
1230+
#!/usr/bin/env bash
1231+
# --- begin runfiles.bash initialization v2 ---
1232+
# Copy-pasted from the Bazel Bash runfiles library v2.
1233+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
1234+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
1235+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
1236+
source "$0.runfiles/$f" 2>/dev/null || \
1237+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
1238+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
1239+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
1240+
# --- end runfiles.bash initialization v2 ---
1241+
1242+
echo "in copy of external/other_repo/pkg/binary.sh: '$(runfiles_current_repository)'"
1243+
EOF
1244+
chmod +x other_repo/pkg/binary.sh
1245+
1246+
bazel build --enable_bzlmod //pkg:gen &>"$TEST_log" || fail "Build should succeed"
1247+
expect_log "in copy of external/other_repo/pkg/binary.sh: 'other_repo'"
1248+
}
1249+
10381250
run_suite "rules test"

tools/bash/runfiles/runfiles.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ function runfiles_current_repository() {
261261
# The only shell script that is not executed from the runfiles directory (if it is populated)
262262
# is the sh_binary entrypoint. Parse its path under the execroot, using the last match to
263263
# allow for nested execroots (e.g. in Bazel integration tests).
264-
local -r repository=$(echo "$normalized_caller_path" | __runfiles_maybe_grep -E -o '/bazel-out/[^/]+/bin/external/[^/]+/' | tail -1 | rev | cut -d / -f 2 | rev)
264+
local -r repository=$(echo "$normalized_caller_path" | __runfiles_maybe_grep -E -o '(^|/)bazel-out/[^/]+/bin/external/[^/]+/' | tail -1 | rev | cut -d / -f 2 | rev)
265265
if [[ -n "$repository" ]]; then
266266
if [[ "${RUNFILES_LIB_DEBUG:-}" == 1 ]]; then
267267
echo >&2 "INFO[runfiles.bash]: runfiles_current_repository($idx): ($normalized_caller_path) lies in repository ($repository)"

0 commit comments

Comments
 (0)