Skip to content

Commit 404c7b3

Browse files
committed
fix: symlink root-level python files to the venv
As found in bazel-contrib#2882 testing, packages like `typing-extensions` which have `.py` files at the root of the `site-packages` folder don't work and it seems that the comment about `rules_python` being too eager is only half-correct. Since `namespace_pkgs` are no longer there, we can just include all of the files and if there are collisions, they will be highlighted as build errors. Now the following works: ``` bazel build //docs --@rules_python//python/config_settings:venvs_site_packages=yes ``` Stacked on bazel-contrib#2882. Work towards bazel-contrib#2156
1 parent 4f1798b commit 404c7b3

File tree

9 files changed

+34
-47
lines changed

9 files changed

+34
-47
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, execute
66
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma
7+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single
8+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single
99

1010
test --test_output=errors
1111

python/private/py_library.bzl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def _get_site_packages_symlinks(ctx):
253253

254254
repo_runfiles_dirname = None
255255
dirs_with_init = {} # dirname -> runfile path
256+
site_packages_symlinks = []
256257
for src in ctx.files.srcs:
257258
if src.extension not in PYTHON_FILE_EXTENSIONS:
258259
continue
@@ -261,16 +262,19 @@ def _get_site_packages_symlinks(ctx):
261262
continue
262263
path = path.removeprefix(site_packages_root)
263264
dir_name, _, filename = path.rpartition("/")
264-
if not dir_name:
265-
# This would be e.g. `site-packages/__init__.py`, which isn't valid
266-
# because it's not within a directory for an importable Python package.
267-
# However, the pypi integration over-eagerly adds a pkgutil-style
268-
# __init__.py file during the repo phase. Just ignore them for now.
269-
continue
270265

271-
if filename.startswith("__init__."):
266+
if dir_name and filename.startswith("__init__."):
272267
dirs_with_init[dir_name] = None
273268
repo_runfiles_dirname = runfiles_root_path(ctx, src.short_path).partition("/")[0]
269+
elif not dir_name:
270+
repo_runfiles_dirname = runfiles_root_path(ctx, src.short_path).partition("/")[0]
271+
272+
# This would be files that do not have directories and we just need to add
273+
# direct symlinks to them as is:
274+
site_packages_symlinks.append((
275+
paths.join(repo_runfiles_dirname, site_packages_root, filename),
276+
filename,
277+
))
274278

275279
# Sort so that we encounter `foo` before `foo/bar`. This ensures we
276280
# see the top-most explicit package first.
@@ -286,7 +290,6 @@ def _get_site_packages_symlinks(ctx):
286290
if not is_sub_package:
287291
first_level_explicit_packages.append(d)
288292

289-
site_packages_symlinks = []
290293
for dirname in first_level_explicit_packages:
291294
site_packages_symlinks.append((
292295
paths.join(repo_runfiles_dirname, site_packages_root, dirname),
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@rules_python//python:py_library.bzl", "py_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
py_library(
6+
name = "nspkg_single",
7+
srcs = glob(["site-packages/**/*.py"]),
8+
experimental_venvs_site_packages = "@rules_python//python/config_settings:venvs_site_packages",
9+
imports = [package_name() + "/site-packages"],
10+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# empty, will not be added to the site-packages dir
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__all__ = [
2+
"SOMETHING",
3+
]
4+
5+
SOMETHING = "nothing"

tests/venv_site_packages_libs/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
22
load("//tests/support:support.bzl", "SUPPORTS_BOOTSTRAP_SCRIPT")
33

4+
package(default_visibility = ["//visibility:public"])
5+
46
py_reconfig_test(
57
name = "venvs_site_packages_libs_test",
68
srcs = ["bin.py"],
@@ -13,5 +15,6 @@ py_reconfig_test(
1315
"//tests/venv_site_packages_libs/nspkg_beta",
1416
"@other//nspkg_delta",
1517
"@other//nspkg_gamma",
18+
"@other//nspkg_single",
1619
],
1720
)

tests/venv_site_packages_libs/bin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_imported_from_venv(self):
2626
self.assert_imported_from_venv("nspkg.subnspkg.beta")
2727
self.assert_imported_from_venv("nspkg.subnspkg.gamma")
2828
self.assert_imported_from_venv("nspkg.subnspkg.delta")
29+
self.assert_imported_from_venv("single_file")
2930

3031

3132
if __name__ == "__main__":

tests/venv_site_packages_libs/nspkg_alpha/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@rules_python//python:py_library.bzl", "py_library")
1+
load("//python:py_library.bzl", "py_library")
22

33
package(default_visibility = ["//visibility:public"])
44

tests/venv_site_packages_libs/venv_site_packages_pypi_test.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)