From 8b8a2e55d8cac42c26b9db1cb353a0f35c28efa5 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:00:22 +0900 Subject: [PATCH] feat(rules): add PyInfo.packages with the PyPI packages of a target `py_library` targets that have a `.dist-info/METADATA` file in their `data`, such as the ones `pip.parse` creates from wheels, record their normalized package name in the new `PyInfo.packages` depset, which is propagated transitively. Previously the name was only available via `venv_symlinks` when `--venvs_site_packages=yes` was set. This allows rules to check whether a target depends on a given package, e.g. so that a `py_test` can tell whether it already depends on `coverage`. --- news/py-info-packages.added.md | 3 ++ python/private/common.bzl | 6 ++- python/private/py_info.bzl | 30 ++++++++++- python/private/py_library.bzl | 15 +++--- .../py_library/py_library_tests.bzl | 54 +++++++++++++++++++ tests/support/py_info_subject.bzl | 11 ++++ 6 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 news/py-info-packages.added.md diff --git a/news/py-info-packages.added.md b/news/py-info-packages.added.md new file mode 100644 index 0000000000..782e926dca --- /dev/null +++ b/news/py-info-packages.added.md @@ -0,0 +1,3 @@ +(rules) Added {obj}`PyInfo.packages` with the normalized names of the PyPI +packages in the transitive closure of a target, e.g. so that a `py_test` can +tell whether it already depends on `coverage`. diff --git a/python/private/common.bzl b/python/private/common.bzl index 2d4c48d8d5..10411cba27 100644 --- a/python/private/common.bzl +++ b/python/private/common.bzl @@ -381,7 +381,8 @@ def create_py_info( implicit_pyc_files, implicit_pyc_source_files, imports, - venv_symlinks = []): + venv_symlinks = [], + packages = []): """Create PyInfo provider. Args: @@ -401,6 +402,8 @@ def create_py_info( imports: depset of strings; the import path values to propagate. venv_symlinks: {type}`list[VenvSymlinkEntry]` instances for symlinks to create in the consuming binary's venv. + packages: {type}`list[str]` the normalized names of the PyPI packages + this target provides. See {obj}`PyInfo.packages`. Returns: A tuple of the PyInfo instance and a depset of the @@ -409,6 +412,7 @@ def create_py_info( """ py_info = PyInfoBuilder.new() py_info.venv_symlinks.add(venv_symlinks) + py_info.packages.add(packages) py_info.direct_original_sources.add(original_sources) py_info.direct_pyc_files.add(required_pyc_files) py_info.direct_pyi_files.add(ctx.files.pyi_srcs) diff --git a/python/private/py_info.bzl b/python/private/py_info.bzl index baaace1949..f3bd5f81e5 100644 --- a/python/private/py_info.bzl +++ b/python/private/py_info.bzl @@ -377,7 +377,8 @@ def _PyInfo_init( direct_pyi_files = depset(), transitive_pyi_files = depset(), type_checking_info = None, - venv_symlinks = depset()): + venv_symlinks = depset(), + packages = depset()): _check_arg_type("transitive_sources", "depset", transitive_sources) # Verify it's postorder compatible, but retain is original ordering. @@ -399,6 +400,7 @@ def _PyInfo_init( _check_arg_type("direct_pyi_files", "depset", direct_pyi_files) _check_arg_type("transitive_pyi_files", "depset", transitive_pyi_files) + _check_arg_type("packages", "depset", packages) if type_checking_info != None: _check_arg_type("type_checking_info", "struct", type_checking_info) return { @@ -409,6 +411,7 @@ def _PyInfo_init( "has_py2_only_sources": has_py2_only_sources, "has_py3_only_sources": has_py3_only_sources, "imports": imports, + "packages": packages, "transitive_implicit_pyc_files": transitive_implicit_pyc_files, "transitive_implicit_pyc_source_files": transitive_implicit_pyc_source_files, "transitive_original_sources": transitive_original_sources, @@ -494,6 +497,21 @@ A depset of import path strings to be added to the `PYTHONPATH` of executable Python targets. These are accumulated from the transitive `deps`. The order of the depset is not guaranteed and may be changed in the future. It is recommended to use `default` order (the default). +""", + "packages": """ +:type: depset[str] + +The normalized names of the PyPI packages in the transitive closure of this +target, e.g. `google_cloud_storage`. The names are lowercased with runs of +`-`, `_` and `.` replaced by `_`, i.e. the same as the package name in the hub +repository that it would come from. + +A {obj}`py_library` contributes its package name if its `data` contains a +`.dist-info/METADATA` file, which is the case for the targets created from +wheels by {obj}`pip.parse`. + +:::{versionadded} VERSION_NEXT_FEATURE +::: """, "transitive_implicit_pyc_files": """ :type: depset[File] @@ -634,6 +652,13 @@ def _PyInfoBuilder_typedef(): :type: DepsetBuilder[str] ::: + :::{field} packages + :type: DepsetBuilder[str] + + ::::{versionadded} VERSION_NEXT_FEATURE + :::: + ::: + :::{field} transitive_implicit_pyc_files :type: DepsetBuilder[File] ::: @@ -698,6 +723,7 @@ def _new_raw_py_info_builder(type_checking_info = None): merge_targets = lambda *a, **k: _PyInfoBuilder_merge_targets(self, *a, **k), merge_type_checking = lambda *a, **k: _PyInfoBuilder_merge_type_checking(self, *a, **k), merge_uses_shared_libraries = lambda *a, **k: _PyInfoBuilder_merge_uses_shared_libraries(self, *a, **k), + packages = builders.DepsetBuilder(), set_has_py2_only_sources = lambda *a, **k: _PyInfoBuilder_set_has_py2_only_sources(self, *a, **k), set_has_py3_only_sources = lambda *a, **k: _PyInfoBuilder_set_has_py3_only_sources(self, *a, **k), set_uses_shared_libraries = lambda *a, **k: _PyInfoBuilder_set_uses_shared_libraries(self, *a, **k), @@ -890,6 +916,7 @@ def _merge_py_info_fields(self, transitive, *, direct = []): self.transitive_pyc_files.add(info.transitive_pyc_files) self.transitive_pyi_files.add(info.transitive_pyi_files) self.venv_symlinks.add(info.venv_symlinks) + self.packages.add(info.packages) def _PyInfoBuilder_merge_all(self, transitive, *, direct = []): """Merge other PyInfos into this PyInfo. @@ -1009,6 +1036,7 @@ def _build_py_info_fields(self, type_checking_info = None): has_py2_only_sources = self._has_py2_only_sources[0], has_py3_only_sources = self._has_py3_only_sources[0], imports = self.imports.build(), + packages = self.packages.build(), transitive_sources = self.transitive_sources.build(), uses_shared_libraries = self._uses_shared_libraries[0], direct_implicit_pyc_files = self.direct_implicit_pyc_files.build(), diff --git a/python/private/py_library.bzl b/python/private/py_library.bzl index 9b1e064853..8c22f69b5b 100644 --- a/python/private/py_library.bzl +++ b/python/private/py_library.bzl @@ -179,10 +179,12 @@ def py_library_impl(ctx): runfiles.add(collect_runfiles(ctx)) runfiles = runfiles.build(ctx) - imports = [] - venv_symlinks = [] - - imports, venv_symlinks = _get_imports_and_venv_symlinks(ctx) + package, version_str = get_package_and_version(ctx.files.data) + imports, venv_symlinks = _get_imports_and_venv_symlinks( + ctx, + package = package, + version_str = version_str, + ) cc_info = collect_cc_info(ctx) py_info, builtins_py_info = create_py_info( @@ -194,6 +196,7 @@ def py_library_impl(ctx): implicit_pyc_source_files = implicit_pyc_source_files, imports = imports, venv_symlinks = venv_symlinks, + packages = [package] if package else [], ) providers = [ @@ -263,12 +266,10 @@ def get_package_and_version(data_files): version.normalize(version_str), # will have no dashes either ) -def _get_imports_and_venv_symlinks(ctx): +def _get_imports_and_venv_symlinks(ctx, *, package, version_str): imports = depset() venv_symlinks = [] if VenvsSitePackages.is_enabled(ctx): - package, version_str = get_package_and_version(ctx.files.data) - # NOTE: Already a list, but buildifier thinks its a depset and # adds to_list() calls later. imports = list(ctx.attr.imports) diff --git a/tests/base_rules/py_library/py_library_tests.bzl b/tests/base_rules/py_library/py_library_tests.bzl index 80e3e1ba22..0c23df90d9 100644 --- a/tests/base_rules/py_library/py_library_tests.bzl +++ b/tests/base_rules/py_library/py_library_tests.bzl @@ -335,6 +335,60 @@ def _test_pyi_srcs_propagated_via_deps_impl(env, target): _tests.append(_test_pyi_srcs_propagated_via_deps) +def _test_packages_empty_without_dist_info(name, config): + rt_util.helper_target( + config.rule, + name = name + "_subject", + srcs = ["lib.py"], + data = ["data.txt"], + ) + analysis_test( + name = name, + target = name + "_subject", + impl = _test_packages_empty_without_dist_info_impl, + ) + +def _test_packages_empty_without_dist_info_impl(env, target): + info = env.expect.that_target(target).provider( + PyInfo, + factory = py_info_subject, + ) + info.packages().contains_exactly([]) + +_tests.append(_test_packages_empty_without_dist_info) + +def _test_packages_from_dist_info(name, config): + # Like the targets that `pip.parse` creates from wheels. + rt_util.helper_target( + config.rule, + name = name + "_subject", + srcs = ["site-packages/foo/__init__.py"], + data = ["site-packages/Foo.Bar-1.0.dist-info/METADATA"], + imports = ["site-packages"], + deps = [name + "_dep"], + ) + rt_util.helper_target( + config.rule, + name = name + "_dep", + srcs = ["dep/site-packages/dep/__init__.py"], + data = ["dep/site-packages/dep_pkg-2.0.dist-info/METADATA"], + imports = ["dep/site-packages"], + ) + analysis_test( + name = name, + target = name + "_subject", + impl = _test_packages_from_dist_info_impl, + ) + +def _test_packages_from_dist_info_impl(env, target): + info = env.expect.that_target(target).provider( + PyInfo, + factory = py_info_subject, + ) + info.packages().contains_exactly(["foo_bar", "dep_pkg"]) + +_tests.append(_test_packages_from_dist_info) + def py_library_test_suite(name): config = struct(rule = py_library, base_test_rule = py_library) native.test_suite( diff --git a/tests/support/py_info_subject.bzl b/tests/support/py_info_subject.bzl index c45848ae1a..9f919f7061 100644 --- a/tests/support/py_info_subject.bzl +++ b/tests/support/py_info_subject.bzl @@ -26,6 +26,7 @@ def _new_py_info_subject(info, *, meta): has_py2_only_sources = lambda *a, **k: _py_info_subject_has_py2_only_sources(self, *a, **k), has_py3_only_sources = lambda *a, **k: _py_info_subject_has_py3_only_sources(self, *a, **k), imports = lambda *a, **k: _py_info_subject_imports(self, *a, **k), + packages = lambda *a, **k: _py_info_subject_packages(self, *a, **k), transitive_original_sources = lambda *a, **k: _py_info_subject_transitive_original_sources(self, *a, **k), transitive_pyc_files = lambda *a, **k: _py_info_subject_transitive_pyc_files(self, *a, **k), transitive_pyi_files = lambda *a, **k: _py_info_subject_transitive_pyi_files(self, *a, **k), @@ -120,6 +121,16 @@ def _py_info_subject_imports(self): meta = self.meta.derive("imports()"), ) +def _py_info_subject_packages(self): + """Returns a `CollectionSubject` for the `packages` attribute. + + Method: PyInfoSubject.packages + """ + return subjects.collection( + self.actual.packages.to_list(), + meta = self.meta.derive("packages()"), + ) + def _py_info_subject_transitive_original_sources(self): """Returns a `DepsetFileSubject` for the `transitive_original_sources` attribute.