Skip to content

Commit 438b12e

Browse files
chowderrickeylevaignas
authored
fix: correctly obtain relative path required for the venv created by --bootstrap_impl=script (#2439)
Computing the relative path from the venv interpreter to the underlying interpreter was incorrectly using the actual interpreter's directory depth, not the venv interpreter's directory depth, when computing the distance from the venv interpreter to the runfiles root. The net effect is the correct relative path would only be computed for binaries with the same directory depth as the actual interpreter (e.g. 2). This went undetected in CI because the tests for this logic just happen to have the same directory depth as the actual interpreter used. To fix, compute the relative path to the runfiles root using the venv interpreter directory. Also added a test in a more nested directory to test this case. Along the way: * Change relative path computation to compute a minimum relative path. * Fix the internals to pass a runfiles-root relative path, not main-repo relative path, for the actual interpreter, as intended. Fixes #2169 --------- Co-authored-by: Richard Levasseur <[email protected]> Co-authored-by: Richard Levasseur <[email protected]> Co-authored-by: Ignas Anikevicius <[email protected]>
1 parent 4ee7e25 commit 438b12e

File tree

6 files changed

+191
-11
lines changed

6 files changed

+191
-11
lines changed

python/private/py_executable_bazel.bzl

+58-11
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def _create_executable(
323323

324324
def _create_zip_main(ctx, *, stage2_bootstrap, runtime_details, venv):
325325
python_binary = _runfiles_root_path(ctx, venv.interpreter.short_path)
326-
python_binary_actual = _runfiles_root_path(ctx, venv.interpreter_actual_path)
326+
python_binary_actual = venv.interpreter_actual_path
327327

328328
# The location of this file doesn't really matter. It's added to
329329
# the zip file as the top-level __main__.py file and not included
@@ -344,6 +344,37 @@ def _create_zip_main(ctx, *, stage2_bootstrap, runtime_details, venv):
344344
)
345345
return output
346346

347+
def relative_path(from_, to):
348+
"""Compute a relative path from one path to another.
349+
350+
Args:
351+
from_: {type}`str` the starting directory. Note that it should be
352+
a directory because relative-symlinks are relative to the
353+
directory the symlink resides in.
354+
to: {type}`str` the path that `from_` wants to point to
355+
356+
Returns:
357+
{type}`str` a relative path
358+
"""
359+
from_parts = from_.split("/")
360+
to_parts = to.split("/")
361+
362+
# Strip common leading parts from both paths
363+
n = min(len(from_parts), len(to_parts))
364+
for _ in range(n):
365+
if from_parts[0] == to_parts[0]:
366+
from_parts.pop(0)
367+
to_parts.pop(0)
368+
else:
369+
break
370+
371+
# Impossible to compute a relative path without knowing what ".." is
372+
if from_parts and from_parts[0] == "..":
373+
fail("cannot compute relative path from '%s' to '%s'", from_, to)
374+
375+
parts = ([".."] * len(from_parts)) + to_parts
376+
return paths.join(*parts)
377+
347378
# Create a venv the executable can use.
348379
# For venv details and the venv startup process, see:
349380
# * https://docs.python.org/3/library/venv.html
@@ -368,9 +399,15 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
368399
# in runfiles is always a symlink. An RBE implementation, for example,
369400
# may choose to write what symlink() points to instead.
370401
interpreter = ctx.actions.declare_symlink("{}/bin/{}".format(venv, py_exe_basename))
371-
interpreter_actual_path = runtime.interpreter.short_path
372-
parent = "/".join([".."] * (interpreter_actual_path.count("/") + 1))
373-
rel_path = parent + "/" + interpreter_actual_path
402+
403+
interpreter_actual_path = _runfiles_root_path(ctx, runtime.interpreter.short_path)
404+
rel_path = relative_path(
405+
# dirname is necessary because a relative symlink is relative to
406+
# the directory the symlink resides within.
407+
from_ = paths.dirname(_runfiles_root_path(ctx, interpreter.short_path)),
408+
to = interpreter_actual_path,
409+
)
410+
374411
ctx.actions.symlink(output = interpreter, target_path = rel_path)
375412
else:
376413
py_exe_basename = paths.basename(runtime.interpreter_path)
@@ -412,7 +449,7 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
412449

413450
return struct(
414451
interpreter = interpreter,
415-
# Runfiles-relative path or absolute path
452+
# Runfiles root relative path or absolute path
416453
interpreter_actual_path = interpreter_actual_path,
417454
files_without_interpreter = [pyvenv_cfg, pth, site_init],
418455
)
@@ -462,12 +499,22 @@ def _create_stage2_bootstrap(
462499
)
463500
return output
464501

465-
def _runfiles_root_path(ctx, path):
466-
# The ../ comes from short_path for files in other repos.
467-
if path.startswith("../"):
468-
return path[3:]
502+
def _runfiles_root_path(ctx, short_path):
503+
"""Compute a runfiles-root relative path from `File.short_path`
504+
505+
Args:
506+
ctx: current target ctx
507+
short_path: str, a main-repo relative path from `File.short_path`
508+
509+
Returns:
510+
{type}`str`, a runflies-root relative path
511+
"""
512+
513+
# The ../ comes from short_path is for files in other repos.
514+
if short_path.startswith("../"):
515+
return short_path[3:]
469516
else:
470-
return "{}/{}".format(ctx.workspace_name, path)
517+
return "{}/{}".format(ctx.workspace_name, short_path)
471518

472519
def _create_stage1_bootstrap(
473520
ctx,
@@ -487,7 +534,7 @@ def _create_stage1_bootstrap(
487534
python_binary_path = runtime_details.executable_interpreter_path
488535

489536
if is_for_zip and venv:
490-
python_binary_actual = _runfiles_root_path(ctx, venv.interpreter_actual_path)
537+
python_binary_actual = venv.interpreter_actual_path
491538
else:
492539
python_binary_actual = ""
493540

python/private/stage1_bootstrap_template.sh

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ fi
135135
if [[ ! -x "$python_exe" ]]; then
136136
if [[ ! -e "$python_exe" ]]; then
137137
echo >&2 "ERROR: Python interpreter not found: $python_exe"
138+
ls -l $python_exe >&2
138139
exit 1
139140
elif [[ ! -x "$python_exe" ]]; then
140141
echo >&2 "ERROR: Python interpreter not executable: $python_exe"

tests/bootstrap_impls/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility
1616
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test", "sh_py_run_test")
17+
load(":venv_relative_path_tests.bzl", "relative_path_test_suite")
1718

1819
_SUPPORTS_BOOTSTRAP_SCRIPT = select({
1920
"@platforms//os:windows": ["@platforms//:incompatible"],
@@ -87,3 +88,5 @@ sh_py_run_test(
8788
sh_src = "sys_executable_inherits_sys_path_test.sh",
8889
target_compatible_with = _SUPPORTS_BOOTSTRAP_SCRIPT,
8990
)
91+
92+
relative_path_test_suite(name = "relative_path_tests")
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility
2+
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
3+
4+
_SUPPORTS_BOOTSTRAP_SCRIPT = select({
5+
"@platforms//os:windows": ["@platforms//:incompatible"],
6+
"//conditions:default": [],
7+
}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"]
8+
9+
py_reconfig_test(
10+
name = "nested_dir_test",
11+
srcs = ["nested_dir_test.py"],
12+
bootstrap_impl = "script",
13+
main = "nested_dir_test.py",
14+
target_compatible_with = _SUPPORTS_BOOTSTRAP_SCRIPT,
15+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2024 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+
"""Test that the binary being a different directory depth than the underlying interpreter works."""
15+
16+
import unittest
17+
18+
19+
class RunsTest(unittest.TestCase):
20+
def test_runs(self):
21+
pass
22+
23+
24+
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
"Unit tests for relative_path computation"
16+
17+
load("@rules_testing//lib:test_suite.bzl", "test_suite")
18+
load("//python/private:py_executable_bazel.bzl", "relative_path") # buildifier: disable=bzl-visibility
19+
20+
_tests = []
21+
22+
def _relative_path_test(env):
23+
# Basic test cases
24+
25+
env.expect.that_str(
26+
relative_path(
27+
from_ = "a/b",
28+
to = "c/d",
29+
),
30+
).equals("../../c/d")
31+
32+
env.expect.that_str(
33+
relative_path(
34+
from_ = "a/b/c",
35+
to = "a/d",
36+
),
37+
).equals("../../d")
38+
env.expect.that_str(
39+
relative_path(
40+
from_ = "a/b/c",
41+
to = "a/b/c/d/e",
42+
),
43+
).equals("d/e")
44+
45+
# Real examples
46+
47+
# external py_binary uses external python runtime
48+
env.expect.that_str(
49+
relative_path(
50+
from_ = "other_repo~/python/private/_py_console_script_gen_py.venv/bin",
51+
to = "rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
52+
),
53+
).equals(
54+
"../../../../../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
55+
)
56+
57+
# internal py_binary uses external python runtime
58+
env.expect.that_str(
59+
relative_path(
60+
from_ = "_main/test/version_default.venv/bin",
61+
to = "rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
62+
),
63+
).equals(
64+
"../../../../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
65+
)
66+
67+
# external py_binary uses internal python runtime
68+
env.expect.that_str(
69+
relative_path(
70+
from_ = "other_repo~/python/private/_py_console_script_gen_py.venv/bin",
71+
to = "_main/python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
72+
),
73+
).equals(
74+
"../../../../../_main/python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
75+
)
76+
77+
# internal py_binary uses internal python runtime
78+
env.expect.that_str(
79+
relative_path(
80+
from_ = "_main/scratch/main.venv/bin",
81+
to = "_main/python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
82+
),
83+
).equals(
84+
"../../../python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
85+
)
86+
87+
_tests.append(_relative_path_test)
88+
89+
def relative_path_test_suite(*, name):
90+
test_suite(name = name, basic_tests = _tests)

0 commit comments

Comments
 (0)