Skip to content

python_wheel: Write Metadata-Version in METADATA - #1668

Closed
phr4nky wants to merge 2 commits into
meta:mainfrom
phr4nky:fix-python-wheel-metadata-version
Closed

phr4nky wants to merge 2 commits into
meta:mainfrom
phr4nky:fix-python-wheel-metadata-version

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Summary

The METADATA file that python_wheel writes (prelude/python/tools/wheel.py) starts with Name and Version and has no Metadata-Version, which the core metadata specification requires. Strict validators reject such a wheel. pip and uv install it anyway, so the problem only shows once the wheel meets a strict check.

This PR writes Metadata-Version: 2.1 as the first field. 2.1 is the oldest version that covers every field the rule writes: Description-Content-Type was added in 2.1, while Requires-Dist and Requires-Python date from 1.2. A Metadata-Version passed through extra_metadata takes its place, for wheels that use later fields, so the header never appears twice.

Affected version

origin/main 00c2ee4. Neither wheel.py nor python_wheel.bzl has ever written the field.

Reproduction

python3 prelude/python/tools/wheel.py --output my_pkg-1.0.0-py3-none-any.whl --name my-pkg --version 1.0.0 \
  --metadata Requires-Python '==3.14.*' --src-path mypkg/core.py src/mypkg/core.py
unzip -p my_pkg-1.0.0-py3-none-any.whl my_pkg-1.0.0.dist-info/METADATA > METADATA
python3 -c 'import packaging.metadata as pm; pm.Metadata.from_email(open("METADATA").read(), validate=True)'

Expected

The metadata validates.

Actual

Name: my-pkg
Version: 1.0.0
Requires-Python: ==3.14.*

packaging 26.4 raises ExceptionGroup: ['None is not a valid metadata version']. The same file with Metadata-Version: 2.1 prepended validates.

Test plan

  • prelude/python/tools/tests/wheel_test.py: test_metadata_starts_with_metadata_version fails without the change and passes with it. test_metadata_version_given_by_the_caller_is_kept checks that a caller's Metadata-Version is the only one in the file.
  • With the change, the METADATA of the wheel above validates with packaging 26.4, and pip still installs the wheel and imports its module.

The core metadata of a wheel must start with Metadata-Version, and the
METADATA that `python_wheel` writes had none. Strict validators reject such
a wheel: `packaging.metadata.Metadata.from_email(validate=True)` fails with
"None is not a valid metadata version". pip and uv install it anyway.

Write `Metadata-Version: 2.1` first, the oldest version with every field the
rule writes (`Description-Content-Type` is 2.1). A Metadata-Version passed
through `extra_metadata` replaces it, for wheels that use later fields.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 27, 2026
@meta-codesync

meta-codesync Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D134830465. (Because this pull request was imported automatically, there will not be any future comments.)

@dtolnay dtolnay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks!

@meta-codesync meta-codesync Bot closed this in 4268187 Nov 28, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 28, 2026
@meta-codesync

meta-codesync Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 4268187.

meta-codesync Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 28, 2026
Summary:
The `METADATA` file that `python_wheel` writes (`prelude/python/tools/wheel.py`) starts with `Name` and `Version` and has no `Metadata-Version`, which the core metadata specification requires. Strict validators reject such a wheel. pip and uv install it anyway, so the problem only shows once the wheel meets a strict check.

This PR writes `Metadata-Version: 2.1` as the first field. 2.1 is the oldest version that covers every field the rule writes: `Description-Content-Type` was added in 2.1, while `Requires-Dist` and `Requires-Python` date from 1.2. A `Metadata-Version` passed through `extra_metadata` takes its place, for wheels that use later fields, so the header never appears twice.

## Affected version

origin/main 00c2ee4f74. Neither `wheel.py` nor `python_wheel.bzl` has ever written the field.

## Reproduction

```
python3 prelude/python/tools/wheel.py --output my_pkg-1.0.0-py3-none-any.whl --name my-pkg --version 1.0.0 \
  --metadata Requires-Python '==3.14.*' --src-path mypkg/core.py src/mypkg/core.py
unzip -p my_pkg-1.0.0-py3-none-any.whl my_pkg-1.0.0.dist-info/METADATA > METADATA
python3 -c 'import packaging.metadata as pm; pm.Metadata.from_email(open("METADATA").read(), validate=True)'
```

## Expected

The metadata validates.

## Actual

```
Name: my-pkg
Version: 1.0.0
Requires-Python: ==3.14.*
```

`packaging` 26.4 raises `ExceptionGroup: ['None is not a valid metadata version']`. The same file with `Metadata-Version: 2.1` prepended validates.

X-link: meta/buck2#1668

Reviewed By: dtolnay

Differential Revision: D134830465

fbshipit-source-id: 6c638c459b4bfb082942c0a6bde01916f03038c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants