Skip to content

Commit 65d1326

Browse files
fix: make bootstrap_impl=script compute correct directory when RUNFILES_MANIFEST_FILE set (bazel-contrib#2177)
The script-based bootstrap wasn't computing the correct runfiles directory when `RUNFILES_MANIFEST_FILE` was set. The path it computed stripped off the manifest file name, but didn't re-add the `.runfiles` suffix to point to the runfiles directory. To fix, just re-append the `.runfiles` suffix after it removes the manifest file name portion of the path. Reproducing this is a bit tricky and it's difficult to reproduce the necessary build flags in a test; all of the following must be met: * `--enable_runfiles=false`, but this cannot be set by transitions, only via command line * `--build_runfile_manifests=true` (this can be set in a transition, but see below) * Due to bazelbuild/bazel#7994, even if a manifest is created, the RUNFILES_MANIFEST_FILE env var won't be set _unless_ the test strategy is local (i.e. not sandboxed, which is the default). To work around those issues, the test just recreates the necessary envvar state and invokes the binary. The underlying files may not exist, but that's OK for the code paths were testing. Fixes bazel-contrib#2186 --------- Co-authored-by: Richard Levasseur <[email protected]>
1 parent 3fd5b83 commit 65d1326

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ A brief description of the categories of changes:
4444
stage2 bootstrap template.
4545
* (bzlmod) Properly handle relative path URLs in parse_simpleapi_html.bzl
4646
* (gazelle) Correctly resolve deps that have top-level module overlap with a gazelle_python.yaml dep module
47+
* (rules) Make `RUNFILES_MANIFEST_FILE`-based invocations work when used with
48+
{obj}`--bootstrap_impl=script`. This fixes invocations using non-sandboxed
49+
test execution with `--enable_runfiles=false --build_runfile_manifests=true`.
50+
([#2186](https://github.com/bazelbuild/rules_python/issues/2186)).
51+
4752

4853
### Added
4954
* (rules) Executables provide {obj}`PyExecutableInfo`, which contains

python/private/stage1_bootstrap_template.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ else
4444
echo "$RUNFILES_DIR"
4545
return 0
4646
elif [[ "${RUNFILES_MANIFEST_FILE:-}" = *".runfiles_manifest" ]]; then
47-
echo "${RUNFILES_MANIFEST_FILE%%.runfiles_manifest}"
47+
echo "${RUNFILES_MANIFEST_FILE%%.runfiles_manifest}.runfiles"
4848
return 0
4949
elif [[ "${RUNFILES_MANIFEST_FILE:-}" = *".runfiles/MANIFEST" ]]; then
50-
echo "${RUNFILES_MANIFEST_FILE%%.runfiles/MANIFEST}"
50+
echo "${RUNFILES_MANIFEST_FILE%%.runfiles/MANIFEST}.runfiles"
5151
return 0
5252
fi
5353

@@ -57,7 +57,6 @@ else
5757
if [[ "$stub_filename" != /* ]]; then
5858
stub_filename="$PWD/$stub_filename"
5959
fi
60-
6160
while true; do
6261
module_space="${stub_filename}.runfiles"
6362
if [[ -d "$module_space" ]]; then

tests/bootstrap_impls/run_binary_zip_no_test.sh

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,46 @@ if [[ -z "$bin" ]]; then
2929
echo "Unable to locate test binary: $BIN_RLOCATION"
3030
exit 1
3131
fi
32-
actual=$($bin 2>&1)
33-
34-
# How we detect if a zip file was executed from depends on which bootstrap
35-
# is used.
36-
# bootstrap_impl=script outputs RULES_PYTHON_ZIP_DIR=<somepath>
37-
# bootstrap_impl=system_python outputs file:.*Bazel.runfiles
38-
expected_pattern="Hello"
39-
if ! (echo "$actual" | grep "$expected_pattern" ) >/dev/null; then
40-
echo "expected output to match: $expected_pattern"
41-
echo "but got:\n$actual"
32+
33+
function test_invocation() {
34+
actual=$($bin)
35+
# How we detect if a zip file was executed from depends on which bootstrap
36+
# is used.
37+
# bootstrap_impl=script outputs RULES_PYTHON_ZIP_DIR=<somepath>
38+
# bootstrap_impl=system_python outputs file:.*Bazel.runfiles
39+
expected_pattern="Hello"
40+
if ! (echo "$actual" | grep "$expected_pattern" ) >/dev/null; then
41+
echo "Test case failed: $1"
42+
echo "expected output to match: $expected_pattern"
43+
echo "but got:\n$actual"
44+
exit 1
45+
fi
46+
}
47+
48+
# Test invocation with RUNFILES_DIR set
49+
unset RUNFILES_MANIFEST_FILE
50+
if [[ ! -e "$RUNFILES_DIR" ]]; then
51+
echo "Runfiles doesn't exist: $RUNFILES_DIR"
4252
exit 1
4353
fi
54+
test_invocation "using RUNFILES_DIR"
55+
56+
57+
orig_runfiles_dir="$RUNFILES_DIR"
58+
unset RUNFILES_DIR
59+
60+
# Test invocation using manifest within runfiles directory (output manifest)
61+
# NOTE: this file may not actually exist in our test, but that's OK; the
62+
# bootstrap just uses the path to find the runfiles directory.
63+
export RUNFILES_MANIFEST_FILE="$orig_runfiles_dir/MANIFEST"
64+
test_invocation "using RUNFILES_MANIFEST_FILE with output manifest"
65+
66+
# Test invocation using manifest outside runfiles (input manifest)
67+
# NOTE: this file may not actually exist in our test, but that's OK; the
68+
# bootstrap just uses the path to find the runfiles directory.
69+
export RUNFILES_MANIFEST_FILE="${orig_runfiles_dir%%.runfiles}.runfiles_manifest"
70+
test_invocation "using RUNFILES_MANIFEST_FILE with input manifest"
71+
72+
# Test invocation without any runfiles env vars set
73+
unset RUNFILES_MANIFEST_FILE
74+
test_invocation "using no runfiles env vars"

0 commit comments

Comments
 (0)