Skip to content

fix(pypi): rewrite RECORD file entries for extracted .data contents - #4025

Merged
rickeylev merged 19 commits into
bazel-contrib:mainfrom
rickeylev:verify_importlib_metadata_files
Aug 12, 2026
Merged

fix(pypi): rewrite RECORD file entries for extracted .data contents#4025
rickeylev merged 19 commits into
bazel-contrib:mainfrom
rickeylev:verify_importlib_metadata_files

Conversation

@rickeylev

@rickeylev rickeylev commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

Extracting wheels with .data/ subdirectories moves files to their
target directories and deletes .data/, but left .dist-info/RECORD
referencing deleted .data/ paths. This causes tools like
importlib.metadata.files() to fail locating or reading files.

Per PEP 427 and PEP 376, rewrite .dist-info/RECORD entries during
wheel extraction to match installed locations relative to
site-packages.

  • Also adds Starlark unit tests for RECORD rewriting and expands
    importlib_metadata_test.

Work towards #3024

…and are readable

Expand importlib_metadata_test to assert that all files returned by
importlib.metadata.files() can be found on disk and read both as binary and
UTF-8 text. Update test fixture RECORD files to list installed paths.
…und and read

Assert that each file returned by importlib.metadata.files() exists and
can be read, reproducing the issue with unrewritten RECORD files.
Verify that the list of files returned by importlib.metadata.files()
matches the expected set of paths from the distribution's RECORD.
Sort expected_paths list alphabetically in source for improved readability.
When extracting wheels containing .data directories (purelib, platlib,
scripts, headers, data), the files are moved into their respective
target scheme directories and .data is deleted.

Rewrite .dist-info/RECORD entries so that archive member paths prefixed
with .data/ are updated to their installed paths relative to site-packages,
allowing tools like importlib.metadata.files() to locate and read them.
Also add Starlark unit tests for the RECORD rewriting logic.
@rickeylev rickeylev changed the title test(venv): [repro] importlib_metadata_test fails to find/read wheel files fix(pypi): rewrite RECORD file entries for extracted .data contents Aug 10, 2026
Unify extraction destinations and RECORD path rewrite prefixes into a
single _DATA_CATEGORIES mapping in whl_extract.bzl. Also add a news
entry fragment for the bug fix in bazel-contrib#4025.
…_test

On Windows, virtual environments have a 2-level directory depth
(Lib/site-packages) compared to POSIX 3-level depth
(lib/pythonX.Y/site-packages), so relative paths in RECORD starting with
.. cannot resolve via locate() on Windows. Skip locate() on Windows
for files outside site-packages.
Virtual environment directory structure depth differs between Windows
(`Lib/site-packages`) and POSIX (`lib/pythonX.Y/site-packages`).
Because the host platform during repo extraction cannot determine the
target platform, RECORD files must be generated per target platform.

Move extracted RECORD files to `rewrite-record/` and rewrite relative
paths for `.data` directories at build time via `gen_wheel_record`.
Update wheel_record_rewriter_test.sh to run .ps1 scripts via powershell.exe
when executing under Windows MSYS2 bash.

Update importlib_metadata_test.py to assert data_prefix ('../../') for
files originating in .data/data/bin/ and .data/data/include/ on Windows.
Convert input/output paths using cygpath -w when running powershell in
wheel_record_rewriter_test.sh and ignore CRLF differences in diff.

Handle .bat extension on Windows venv bin scripts when validating
located files in importlib_metadata_test.py.
@aignas

aignas commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

A few open questions:

  • Should we actually do this path manipulation where we extract it differently from the wheel layout? We are doing this because this was the layout before when we used to extract with installer.
  • Should we rename the RECORD file to something else, like RECORD.orig? We can generate a RECORD file from just the globs in a build action and that would mean it is always deterministic.

@rickeylev

Copy link
Copy Markdown
Collaborator Author

AFAIK, we're extracting the wheel the same as what other installers do, sans the later "install" part where it shows up into a binary's venv.

e.g. given https://github.com/bazel-contrib/rules_python/blob/main/tests/repos/whl_with_data1/whl_with_data1-1.0.dist-info/RECORD

  • platlib/ and purelib/ goes into site-packages
  • scripts goes into scripts; venv later puts it into $venv/bin/
  • headers/ goes into headers; venv later puts it into $venv/headers/
  • data/ goes into data; venv later puts it into $venv/ directly (e.g. data/XXX/foo.h will show up as $venv/XXX/foo.h

- Use PyRewriteWheelRecord mnemonic and progress message in gen_wheel_record.bzl
- Use WINDOWS_CONSTRAINTS_ATTRS helper for _windows_constraints attribute
- Move _DATA_CATEGORIES mapping to top of whl_extract.bzl
- Document RECORD relative path conventions citing PEP 376 and PEP 427 in importlib_metadata_test.py
…rge_trees

- Define WINDOWS_CONSTRAINTS_ATTRS using attr.label_list so rules can use dicts.add directly
- Restore upstream merge_trees implementation in whl_extract.bzl
…n operator

- Factor out WINDOWS_CONSTRAINTS_PLAIN_ATTRS in attributes.bzl for rules not using builders
- Use | operator in gen_wheel_record.bzl instead of dicts.add
@rickeylev
rickeylev marked this pull request as ready for review August 11, 2026 06:38
@rickeylev
rickeylev requested a review from aignas as a code owner August 11, 2026 06:38
@rickeylev

Copy link
Copy Markdown
Collaborator Author

Ready, PTAL

@aignas aignas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to mention related code:

When patching the wheels we are rewriting a RECORD file. Since we are doing this here as well, we should stop doing it. Just patching the files is enough. That brings a question if we should rezip the patched wheel itself in a build action?

Comment thread python/private/pypi/BUILD.bazel
@aignas
aignas added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@aignas
aignas added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@rickeylev

Copy link
Copy Markdown
Collaborator Author

should rezip the patched wheel itself in a build action?

I would lean towards no, for two reasons: (1) keep our API smaller, and (2) does doing so benefit our code at all?

I don't entirely remember why re-zipping was required...did it stem from using piptools or something? Something that required a .whl to process?

TBH I'm not a fan of the :whl targets that let you reference the .whl file. They double the amount of disk used and I'm not sure they provide sufficient value.

@rickeylev
rickeylev added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 11, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 12, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 12, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 12, 2026
- PackagePath.locate() returns os.PathLike which lacks attribute definitions in type checkers
- Explicitly convert to pathlib.Path to satisfy pyrefly static analysis
@rickeylev
rickeylev enabled auto-merge August 12, 2026 02:14
@aignas

aignas commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

TBH I'm not a fan of the :whl targets that let you reference the .whl file. They double the amount of disk used and I'm not sure they provide sufficient value.

The only internal usage is gazelle_python.yaml generation, but we should be able to use the extracted_whl_srcs for this.

For external usage, there is definitely something, but it's hard to know. I think patching + whl reziping is such a niche use-case, that we could just not support forward compatibility in there.

@rickeylev
rickeylev disabled auto-merge August 12, 2026 07:35
@rickeylev
rickeylev merged commit 36c342a into bazel-contrib:main Aug 12, 2026
4 of 5 checks passed
@rickeylev
rickeylev deleted the verify_importlib_metadata_files branch August 12, 2026 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants