Skip to content

Commit d713ba7

Browse files
lpulleyaignas
andauthored
fix: correctly find runfiles root for symlinks (#2665)
`$maybe_runfiles_root` doesn't seem to be a real variable. Based on the presence of the `while` loop, it seems that this code wants to try resolving the symlink one level at a time (`readlink`, not `realpath`) until it can find runfiles? --------- Co-authored-by: Ignas Anikevicius <[email protected]>
1 parent 1299307 commit d713ba7

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Unreleased changes template.
5757

5858
{#v0-0-0-fixed}
5959
### Fixed
60+
* (runfiles) ({obj}`--bootstrap_impl=script`) Follow symlinks when searching for runfiles.
6061
* Do not try to run `chmod` when downloading non-windows hermetic toolchain
6162
repositories on Windows. Fixes
6263
[#2660](https://github.com/bazel-contrib/rules_python/issues/2660).

python/private/stage1_bootstrap_template.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ else
8181
if [[ ! -L "$stub_filename" ]]; then
8282
break
8383
fi
84-
target=$(realpath $maybe_runfiles_root)
85-
stub_filename="$target"
84+
stub_filename=$(readlink $stub_filename)
8685
done
8786
echo >&2 "Unable to find runfiles directory for $1"
8887
exit 1

tests/bootstrap_impls/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ sh_py_run_test(
7070
venvs_use_declare_symlink = "no",
7171
)
7272

73+
sh_py_run_test(
74+
name = "run_binary_find_runfiles_test",
75+
py_src = "bin.py",
76+
sh_src = "run_binary_find_runfiles_test.sh",
77+
target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT,
78+
)
79+
7380
sh_py_run_test(
7481
name = "run_binary_bootstrap_script_zip_yes_test",
7582
bootstrap_impl = "script",
@@ -88,6 +95,14 @@ sh_py_run_test(
8895
target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT,
8996
)
9097

98+
sh_py_run_test(
99+
name = "run_binary_bootstrap_script_find_runfiles_test",
100+
bootstrap_impl = "script",
101+
py_src = "bin.py",
102+
sh_src = "run_binary_find_runfiles_test.sh",
103+
target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT,
104+
)
105+
91106
py_reconfig_test(
92107
name = "sys_path_order_bootstrap_script_test",
93108
srcs = ["sys_path_order_test.py"],
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# --- begin runfiles.bash initialization v3 ---
16+
# Copy-pasted from the Bazel Bash runfiles library v3.
17+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
18+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
19+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
20+
source "$0.runfiles/$f" 2>/dev/null || \
21+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
22+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
23+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
24+
# --- end runfiles.bash initialization v3 ---
25+
set +e
26+
27+
bin=$(rlocation $BIN_RLOCATION)
28+
if [[ -z "$bin" ]]; then
29+
echo "Unable to locate test binary: $BIN_RLOCATION"
30+
exit 1
31+
fi
32+
33+
bin_link_layer_1=$TEST_TMPDIR/link1
34+
ln -s "$bin" "$bin_link_layer_1"
35+
bin_link_layer_2=$TEST_TMPDIR/link2
36+
ln -s "$bin_link_layer_1" "$bin_link_layer_2"
37+
38+
result=$(RUNFILES_DIR='' RUNFILES_MANIFEST_FILE='' $bin)
39+
result_link_layer_1=$(RUNFILES_DIR='' RUNFILES_MANIFEST_FILE='' $bin_link_layer_1)
40+
result_link_layer_2=$(RUNFILES_DIR='' RUNFILES_MANIFEST_FILE='' $bin_link_layer_2)
41+
42+
if [[ "$result" != "$result_link_layer_1" ]]; then
43+
echo "Output from test does not match output when invoked via a link;"
44+
echo "Output from test:"
45+
echo "$result"
46+
echo "Output when invoked via a link:"
47+
echo "$result_link_layer_1"
48+
exit 1
49+
fi
50+
if [[ "$result" != "$result_link_layer_2" ]]; then
51+
echo "Output from test does not match output when invoked via a link to a link;"
52+
echo "Output from test:"
53+
echo "$result"
54+
echo "Output when invoked via a link to a link:"
55+
echo "$result_link_layer_2"
56+
exit 1
57+
fi
58+
59+
exit 0

0 commit comments

Comments
 (0)