You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix Bash runfiles calling repository lookup for directly run scripts
When an `sh_binary` is run directly from `bazel-bin`, its path won't be contained in the manifest and will not match a `bazel-out` regex. Instead, make the path absolute and then match after the `bazel-bin` to extract the binaries repository name.
Also improve the error message when `runfiles_current_repository` can find neither the runfiles manifest nor the runfiles directory.
Work towards fixing #19796 (comment)Closes#19810.
PiperOrigin-RevId: 574208158
Change-Id: I45d6b503e8f34e13177d57ca64c202640306cfb8
# its return value is ignored if passed to rlocation.
228
228
functionrunfiles_current_repository() {
229
229
local -r idx=${1:-1}
230
-
local -r caller_path="${BASH_SOURCE[$idx]}"
230
+
local -r raw_caller_path="${BASH_SOURCE[$idx]}"
231
+
# Make the caller path absolute if needed to handle the case where the script is run directly
232
+
# from bazel-bin, with working directory a subdirectory of bazel-bin.
233
+
if [[ "$raw_caller_path"=~$_RLOCATION_ISABS_PATTERN ]];then
234
+
local -r caller_path="$raw_caller_path"
235
+
else
236
+
local -r caller_path="$(cd $(dirname "$raw_caller_path"); pwd)/$(basename "$raw_caller_path")"
237
+
fi
231
238
if [[ "${RUNFILES_LIB_DEBUG:-}"== 1 ]];then
232
239
echo>&2"INFO[runfiles.bash]: runfiles_current_repository($idx): caller's path is ($caller_path)"
233
240
fi
@@ -245,6 +252,19 @@ function runfiles_current_repository() {
245
252
if [[ "${RUNFILES_LIB_DEBUG:-}"== 1 ]];then
246
253
echo>&2"ERROR[runfiles.bash]: runfiles_current_repository($idx): ($normalized_caller_path) is not the target of an entry in the runfiles manifest ($RUNFILES_MANIFEST_FILE)"
247
254
fi
255
+
# The binary may also be run directly from bazel-bin or bazel-out.
echo>&2"INFO[runfiles.bash]: runfiles_current_repository($idx): ($normalized_caller_path) lies in repository ($repository)"
301
+
echo>&2"INFO[runfiles.bash]: runfiles_current_repository($idx): ($normalized_caller_path) lies in repository ($repository) (parsed exec path)"
281
302
fi
282
303
echo"$repository"
283
304
else
284
305
if [[ "${RUNFILES_LIB_DEBUG:-}"== 1 ]];then
285
-
echo>&2"INFO[runfiles.bash]: runfiles_current_repository($idx): ($normalized_caller_path) lies in the main repository"
306
+
echo>&2"INFO[runfiles.bash]: runfiles_current_repository($idx): ($normalized_caller_path) lies in the main repository (parsed exec path)"
286
307
fi
287
308
echo""
288
309
fi
@@ -294,6 +315,13 @@ function runfiles_current_repository() {
294
315
fi
295
316
fi
296
317
318
+
if [[ -z"$rlocation_path" ]];then
319
+
if [[ "${RUNFILES_LIB_DEBUG:-}"== 1 ]];then
320
+
echo>&2"ERROR[runfiles.bash]: runfiles_current_repository($idx): cannot determine repository for ($caller_path) since neither the runfiles directory (${RUNFILES_DIR:-}) nor the runfiles manifest (${RUNFILES_MANIFEST_FILE:-}) exist"
321
+
fi
322
+
return 1
323
+
fi
324
+
297
325
if [[ "${RUNFILES_LIB_DEBUG:-}"== 1 ]];then
298
326
echo>&2"INFO[runfiles.bash]: runfiles_current_repository($idx): ($caller_path) corresponds to rlocation path ($rlocation_path)"
0 commit comments