Skip to content

Commit 1c894ea

Browse files
committed
fix(pypi): include RECORD file in installed wheel targets
Currently, `importlib.metadata.files()` returns `None` for packages installed via `rules_python`'s wheel rules. This happens because the `RECORD` file in the `.dist-info` directory is explicitly excluded from the `data` attribute of the generated `py_library` targets. To fix this, remove `**/*.dist-info/RECORD` from the `_data_exclude` list in `whl_library_targets.bzl`. This ensures that the `RECORD` file is preserved in the runfiles and available at runtime, enabling `importlib.metadata.files()` to correctly list the files in the package.
1 parent ee7c54b commit 1c894ea

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

python/private/pypi/whl_library_targets.bzl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,6 @@ def whl_library_targets(
374374
"**/*.py",
375375
"**/*.pyc",
376376
"**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNNN are created
377-
# RECORD is known to contain sha256 checksums of files which might include the checksums
378-
# of generated files produced when wheels are installed. The file is ignored to avoid
379-
# Bazel caching issues.
380-
"**/*.dist-info/RECORD",
381377
]
382378
for item in data_exclude:
383379
if item not in _data_exclude:

tests/venv_site_packages_libs/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,17 @@ py_reconfig_test(
8585
"@whl_with_data2//:pkg",
8686
],
8787
)
88+
89+
py_reconfig_test(
90+
name = "importlib_metadata_test",
91+
srcs = ["importlib_metadata_test.py"],
92+
bootstrap_impl = select({
93+
"@platforms//os:windows": "system_python",
94+
"//conditions:default": "script",
95+
}),
96+
main = "importlib_metadata_test.py",
97+
venvs_site_packages = "yes",
98+
deps = [
99+
"@whl_with_data1//:pkg",
100+
],
101+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import importlib.metadata
2+
import unittest
3+
4+
5+
class ImportlibMetadataTest(unittest.TestCase):
6+
7+
def test_importlib_metadata_files(self):
8+
files = importlib.metadata.files("whl-with-data1")
9+
self.assertIsNotNone(files, "importlib.metadata.files returned None")
10+
self.assertGreater(
11+
len(files), 0, "importlib.metadata.files returned empty list"
12+
)
13+
14+
# Verify it contains some expected files.
15+
# The RECORD file lists paths relative to the installation root (site-packages).
16+
# whl_with_data1-1.0.data/purelib/data_overlap.py should be installed as data_overlap.py
17+
# whl_with_data1-1.0.data/platlib/whl_with_data1/platlib_file.txt should be whl_with_data1/platlib_file.txt
18+
19+
file_names = [f.name for f in files]
20+
self.assertIn("data_overlap.py", file_names)
21+
22+
23+
if __name__ == "__main__":
24+
unittest.main()

0 commit comments

Comments
 (0)