Skip to content

Commit d3a6d37

Browse files
committed
got pip_repository test passing
1 parent 987c906 commit d3a6d37

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

examples/pip_repository_annotations/.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ try-import %workspace%/user.bazelrc
55
# is in examples/bzlmod as the `whl_mods` feature.
66
common --noenable_bzlmod
77
common --enable_workspace
8+
common --legacy_external_runfiles=false
89
common --incompatible_python_disallow_native_rules

examples/pip_repository_annotations/pip_repository_annotations_test.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
import unittest
2222
from pathlib import Path
2323

24-
print("sys.path:")
25-
for i, x in enumerate(sorted(sys.path)):
26-
print(x)
27-
28-
raise SystemExit(1)
29-
30-
from rules_python.python.runfiles import runfiles
24+
from python.runfiles import runfiles
3125

3226

3327
class PipRepositoryAnnotationsTest(unittest.TestCase):
@@ -41,7 +35,7 @@ def wheel_pkg_dir(self) -> str:
4135
def test_build_content_and_data(self):
4236
r = runfiles.Create()
4337
rpath = r.Rlocation(
44-
"pip_repository_annotations_example/external/{}/generated_file.txt".format(
38+
"{}/generated_file.txt".format(
4539
self.wheel_pkg_dir()
4640
)
4741
)
@@ -54,7 +48,7 @@ def test_build_content_and_data(self):
5448
def test_copy_files(self):
5549
r = runfiles.Create()
5650
rpath = r.Rlocation(
57-
"pip_repository_annotations_example/external/{}/copied_content/file.txt".format(
51+
"{}/copied_content/file.txt".format(
5852
self.wheel_pkg_dir()
5953
)
6054
)
@@ -67,7 +61,7 @@ def test_copy_files(self):
6761
def test_copy_executables(self):
6862
r = runfiles.Create()
6963
rpath = r.Rlocation(
70-
"pip_repository_annotations_example/external/{}/copied_content/executable{}".format(
64+
"{}/copied_content/executable{}".format(
7165
self.wheel_pkg_dir(),
7266
".exe" if platform.system() == "windows" else ".py",
7367
)
@@ -88,7 +82,7 @@ def test_data_exclude_glob(self):
8882
current_wheel_version = "0.38.4"
8983

9084
r = runfiles.Create()
91-
dist_info_dir = "pip_repository_annotations_example/external/{}/site-packages/wheel-{}.dist-info".format(
85+
dist_info_dir = "{}/site-packages/wheel-{}.dist-info".format(
9286
self.wheel_pkg_dir(),
9387
current_wheel_version,
9488
)
@@ -119,11 +113,10 @@ def test_extra(self):
119113
# This test verifies that annotations work correctly for pip packages with extras
120114
# specified, in this case requests[security].
121115
r = runfiles.Create()
122-
rpath = r.Rlocation(
123-
"pip_repository_annotations_example/external/{}/generated_file.txt".format(
124-
self.requests_pkg_dir()
125-
)
116+
path = "{}/generated_file.txt".format(
117+
self.requests_pkg_dir()
126118
)
119+
rpath = r.Rlocation(path)
127120
generated_file = Path(rpath)
128121
self.assertTrue(generated_file.exists())
129122

python/private/site_init_template.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ def _search_path(name):
124124
return None
125125

126126

127+
# NOTE: We do not add _RUNFILES_ROOT to sys.path for two reasons:
128+
# 1. Under workspace, it makes every external repository importable. If a Bazel
129+
# repository matches a Python import name, they conflict.
130+
# 2. Under bzlmod, the repo names in the runfiles directory aren't importable
131+
# Python names, so there's no point in adding the runfiles root to sys.path.
127132
def _setup_sys_path():
128133
seen = set(sys.path)
129134
python_path_entries = []

python/runfiles/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ py_library(
3636
## todo: this fixes the test, but I don't think its generally safe to do.
3737
## It adds the runfiles root, which makes _everything_ under it importable,
3838
## not just the rules_python directory
39+
##
40+
## What's happening is bootstrap=script isn't adding runfiles to the root.
41+
## This is probably because of bzlmod: the directory names of the repos
42+
## are mangled, so aren't usable anyways, so why add it to create such
43+
## a reliance?
44+
##
45+
## Ideally, we would have a directory where only specific sub-directories (e.g
46+
## rules_python) existed. There's nowhere in runfiles that naturally has that
47+
# though. The closest would be the venv's site-package directory, i.e. creating
48+
# a rules_python dir that symlinks back. This could be done using the
49+
# site_packages_symlinks.
50+
#
3951
##"../../..",
4052
],
4153
visibility = ["//visibility:public"],

0 commit comments

Comments
 (0)