Skip to content

Respect metadata_directory in build_wheel per PEP 517#3086

Open
messense wants to merge 1 commit intoPyO3:mainfrom
messense:fix/pep517-metadata-directory
Open

Respect metadata_directory in build_wheel per PEP 517#3086
messense wants to merge 1 commit intoPyO3:mainfrom
messense:fix/pep517-metadata-directory

Conversation

@messense
Copy link
Member

When a PEP 517 frontend passes metadata_directory to build_wheel, the built wheel should use the pre-generated metadata from that directory. This enables wrapping build backends that modify metadata in prepare_metadata_for_build_wheel.

Python side: when metadata_directory is not None, set MATURIN_PEP517_METADATA_DIR in the subprocess environment.

Rust side: write_dist_info() checks the env var and copies files from the pre-existing .dist-info directory instead of regenerating them. The WHEEL file is always regenerated to ensure correct tags, and RECORD is skipped since it is generated by WheelWriter::finish(). If the env var is set but the expected directory does not exist, an error is raised instead of silently falling back.

Fixes #1973

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Implements PEP 517 build_wheel(..., metadata_directory=...) support by reusing pre-generated .dist-info metadata created by prepare_metadata_for_build_wheel, enabling wrapper backends to adjust metadata without repacking wheels.

Changes:

  • Python PEP 517 wrapper passes metadata_directory to the Rust subprocess via MATURIN_PEP517_METADATA_DIR.
  • Rust wheel metadata writer conditionally copies .dist-info contents from MATURIN_PEP517_METADATA_DIR, while always regenerating WHEEL and skipping RECORD.
  • Adds a unit test ensuring copied metadata is used and WHEEL/RECORD behaviors are correct.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/module_writer/mod.rs Adds env-var-driven branch to copy pre-generated .dist-info content into the wheel.
src/module_writer/mock_writer.rs Adds unit test for env-var metadata directory behavior (copy vs regenerate/skip).
maturin/__init__.py Propagates metadata_directory into the maturin subprocess environment.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Implements PEP 517’s build_wheel(..., metadata_directory=...) behavior by allowing the wheel build step to reuse pre-generated .dist-info metadata (typically produced by prepare_metadata_for_build_wheel), enabling wrappers that adjust metadata without repacking the wheel.

Changes:

  • Python: when metadata_directory is provided to build_wheel, pass it to the Rust subprocess via MATURIN_PEP517_METADATA_DIR.
  • Rust: write_dist_info() can copy existing .dist-info contents from disk (skipping RECORD, regenerating WHEEL) instead of regenerating all metadata.
  • Tests: add a unit test asserting that copied metadata is used and that WHEEL/RECORD handling is correct.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/module_writer/mod.rs Adds env-var-controlled path to reuse/copy pre-generated .dist-info metadata during wheel creation.
src/module_writer/mock_writer.rs Adds a test covering the “reuse pre-generated metadata” path and validates WHEEL regeneration / RECORD omission.
maturin/__init__.py Sets MATURIN_PEP517_METADATA_DIR for the Rust subprocess when metadata_directory is provided to build_wheel.

You can also share your feedback on Copilot code review. Take the survey.

When a PEP 517 frontend passes metadata_directory to build_wheel,
the built wheel should use the pre-generated metadata from that directory.
This enables wrapping build backends that modify metadata in
prepare_metadata_for_build_wheel.

Python side: when metadata_directory is not None, set
MATURIN_PEP517_METADATA_DIR in the subprocess environment.

Rust side: write_dist_info() checks the env var and copies files from
the pre-existing .dist-info directory instead of regenerating them.
The WHEEL file is always regenerated to ensure correct tags, and
RECORD is skipped since it is generated by WheelWriter::finish().
If the env var is set but the expected directory does not exist,
an error is raised instead of silently falling back.

Fixes PyO3#1973
@messense messense force-pushed the fix/pep517-metadata-directory branch from 5c7c6cf to a0dca02 Compare March 14, 2026 11:35
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.

build meta wheel does not respect metadata_directory per PEP-517

2 participants