Skip to content

Commit f20f1bb

Browse files
committed
fix: add runfiles root for system_python bootstrap
1 parent 846dfd0 commit f20f1bb

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

python/private/py_executable.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ def _create_executable(
341341
output_prefix = base_executable_name,
342342
imports = imports,
343343
runtime_details = runtime_details,
344+
add_runfiles_root_to_sys_path = (
345+
"1" if BootstrapImplFlag.get_value(ctx) == BootstrapImplFlag.SYSTEM_PYTHON else "0"
346+
),
344347
)
345348

346349
stage2_bootstrap = _create_stage2_bootstrap(
@@ -504,7 +507,7 @@ def _create_zip_main(ctx, *, stage2_bootstrap, runtime_details, venv):
504507
# * https://snarky.ca/how-virtual-environments-work/
505508
# * https://github.com/python/cpython/blob/main/Modules/getpath.py
506509
# * https://github.com/python/cpython/blob/main/Lib/site.py
507-
def _create_venv(ctx, output_prefix, imports, runtime_details):
510+
def _create_venv(ctx, output_prefix, imports, runtime_details, add_runfiles_root_to_sys_path):
508511
create_full_venv = BootstrapImplFlag.get_value(ctx) == BootstrapImplFlag.SCRIPT
509512
venv = "_{}.venv".format(output_prefix.lstrip("_"))
510513

@@ -592,6 +595,7 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
592595
template = runtime.site_init_template,
593596
output = site_init,
594597
substitutions = {
598+
"%add_runfiles_root_to_sys_path%": add_runfiles_root_to_sys_path,
595599
"%coverage_tool%": _get_coverage_tool_runfiles_path(ctx, runtime),
596600
"%import_all%": "True" if read_possibly_native_flag(ctx, "python_import_all_repositories") else "False",
597601
"%site_init_runfiles_path%": "{}/{}".format(ctx.workspace_name, site_init.short_path),

python/private/site_init_template.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
_SELF_RUNFILES_RELATIVE_PATH = "%site_init_runfiles_path%"
2727
# Runfiles-relative path to the coverage tool entry point, if any.
2828
_COVERAGE_TOOL = "%coverage_tool%"
29+
# True if the runfiles root should be added to sys.path
30+
_ADD_RUNFILES_ROOT_TO_SYS_PATH = "%add_runfiles_root_to_sys_path%" == "1"
2931

3032

3133
def _is_verbose():
@@ -147,6 +149,9 @@ def _maybe_add_path(path):
147149
sys.path.append(path)
148150
seen.add(path)
149151

152+
if _ADD_RUNFILES_ROOT_TO_SYS_PATH:
153+
_maybe_add_path(_RUNFILES_ROOT)
154+
150155
for rel_path in _IMPORTS_STR.split(":"):
151156
abs_path = os.path.join(_RUNFILES_ROOT, rel_path)
152157
_maybe_add_path(abs_path)

tests/bootstrap_impls/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ sh_py_run_test(
104104
target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT,
105105
)
106106

107+
py_reconfig_test(
108+
name = "bazel_tools_importable_system_python_test",
109+
srcs = ["bazel_tools_importable_test.py"],
110+
bootstrap_impl = "system_python",
111+
# Necessary because bazel_tools doesn't have __init__.py files.
112+
legacy_create_init = True,
113+
main = "bazel_tools_importable_test.py",
114+
deps = [
115+
"@bazel_tools//tools/python/runfiles",
116+
],
117+
)
118+
107119
py_reconfig_test(
108120
name = "sys_path_order_bootstrap_script_test",
109121
srcs = ["sys_path_order_test.py"],
@@ -121,6 +133,9 @@ py_reconfig_test(
121133
env = {"BOOTSTRAP": "system_python"},
122134
imports = ["./site-packages"],
123135
main = "sys_path_order_test.py",
136+
deps = [
137+
"@bazel_tools//tools/python/runfiles",
138+
],
124139
)
125140

126141
py_reconfig_test(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
import unittest
3+
4+
5+
class SysPathOrderTest(unittest.TestCase):
6+
def test_bazel_tools_importable(self):
7+
try:
8+
import bazel_tools
9+
import bazel_tools.tools.python
10+
import bazel_tools.tools.python.runfiles
11+
except ImportError as exc:
12+
raise AssertionError(
13+
"Failed to import bazel_tools.python.runfiles\n"
14+
+ "sys.path:\n"
15+
+ "\n".join(f"{i}: {v}" for i, v in enumerate(sys.path))
16+
) from exc
17+
18+
19+
if __name__ == "__main__":
20+
unittest.main()

0 commit comments

Comments
 (0)