Skip to content

Commit 7f61e98

Browse files
scelenlopezgi
authored andcommitted
Fix runfiles discovery (#522)
The launcher scripts were not able to find runfiles when executed deeper inside a runfile tree generated by another rule. This happens when another rule depends on the container image target and wants to execute the scripts from its own runfiles tree. Fix discovery to also account for this possibility, using similar heuristics as FindModuleSpace in the python launcher.
1 parent c9065d1 commit 7f61e98

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

container/incremental_load.sh.tpl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ set -eu
1919
# This is a generated file that loads all docker layers built by "docker_build".
2020

2121
function guess_runfiles() {
22-
pushd ${BASH_SOURCE[0]}.runfiles > /dev/null 2>&1
23-
pwd
24-
popd > /dev/null 2>&1
22+
if [ -d ${BASH_SOURCE[0]}.runfiles ]; then
23+
# Runfiles are adjacent to the current script.
24+
echo "$( cd ${BASH_SOURCE[0]}.runfiles && pwd )"
25+
else
26+
# The current script is within some other script's runfiles.
27+
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
28+
echo $mydir | sed -e 's|\(.*\.runfiles\)/.*|\1|'
29+
fi
2530
}
2631

2732
RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}"

container/push-tag.sh.tpl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
set -eu
1717

1818
function guess_runfiles() {
19-
pushd ${BASH_SOURCE[0]}.runfiles > /dev/null 2>&1
20-
pwd
21-
popd > /dev/null 2>&1
19+
if [ -d ${BASH_SOURCE[0]}.runfiles ]; then
20+
# Runfiles are adjacent to the current script.
21+
echo "$( cd ${BASH_SOURCE[0]}.runfiles && pwd )"
22+
else
23+
# The current script is within some other script's runfiles.
24+
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
25+
echo $mydir | sed -e 's|\(.*\.runfiles\)/.*|\1|'
26+
fi
2227
}
2328

2429
RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}"

contrib/push-all.sh.tpl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515

1616
set -eu
1717
function guess_runfiles() {
18-
pushd ${BASH_SOURCE[0]}.runfiles > /dev/null 2>&1
19-
pwd
20-
popd > /dev/null 2>&1
18+
if [ -d ${BASH_SOURCE[0]}.runfiles ]; then
19+
# Runfiles are adjacent to the current script.
20+
echo "$( cd ${BASH_SOURCE[0]}.runfiles && pwd )"
21+
else
22+
# The current script is within some other script's runfiles.
23+
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
24+
echo $mydir | sed -e 's|\(.*\.runfiles\)/.*|\1|'
25+
fi
2126
}
2227

2328
RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}"

testdata/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,14 @@ cc_image(
778778
binary = ":cc_binary",
779779
)
780780

781+
# Test that we can also load image from within another script.
782+
sh_binary(
783+
name = "cc_image_wrapper",
784+
srcs = ["cc_image_wrapper.sh"],
785+
args = ["$(location :cc_image)"],
786+
data = [":cc_image"],
787+
)
788+
781789
load(
782790
"//java:image.bzl",
783791
"java_image",

testdata/cc_image_wrapper.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
"$1" || { echo "FAIL!"; exit 1; }

testing/e2e.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ function test_cc_binary_as_image() {
233233
EXPECT_CONTAINS "$(bazel run "$@" testdata:cc_binary_as_image)" "Hello World"
234234
}
235235

236+
function test_cc_image_wrapper() {
237+
cd "${ROOT}"
238+
clear_docker
239+
EXPECT_CONTAINS "$(bazel run "$@" testdata:cc_image_wrapper)" "Hello World"
240+
}
241+
236242
function test_go_image() {
237243
cd "${ROOT}"
238244
clear_docker
@@ -375,6 +381,7 @@ test_cc_image -c opt
375381
test_cc_image -c dbg
376382
test_cc_binary_as_image -c opt
377383
test_cc_binary_as_image -c dbg
384+
test_cc_image_wrapper
378385
test_go_image -c opt
379386
test_go_image -c dbg
380387
test_go_image_busybox

0 commit comments

Comments
 (0)