Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set _PYTHON_HOST_PLATFORM when building wheels in CI #21942

Merged
merged 4 commits into from
Feb 11, 2025

Conversation

huonw
Copy link
Contributor

@huonw huonw commented Feb 10, 2025

This follows the lead of cibuildwheel to explicitly correctly tag our wheels on macOS, via the _PYTHON_HOST_PLATFORM environment variable. This change ensures that the pip understands which platform each wheel is built for and compatible with, and thus external plugins using pants_requirements(...) work again.

It appears the file name (including platform) tags of a wheel are derived from sysconfig.get_platform(), i.e. based on the build configuration of the current Python interpreter. Pants builds single platform wheels, so the wheels need to be tagged with the right CPU architecture.

In #21655, we changed from tagging correctly to tagging incorrectly:

  • Before that change, our self-hosted runners had 'simple' Python interpreters, built for the current platform via Pyenv. python -c 'import sysconfig; print(sysconfig.get_platform())' reports macosx-10.15-x86_64andmacosx-11.6-arm64`, and indeed the wheels were tagged like that.
  • After that change, we started using Python interpreters provided by GitHub-hosted runners. The interpreters report macosx-10.9-universal2 on both the ARM64 and x86-64 runners. That is, they're built as universal binaries that can run on either architecture. This lead to the wheels being tagged as universal2 as well.

The _PYTHON_HOST_PLATFORM environment variable overrides the default sysconfig.get_platform() value, and appears to be designed for this sort of purpose (and is used by cibuildwheel for such):

Fixes #21938

# macosx-10.9-universal2. Thus, without this env var, we tag our single-platform wheels
# as universal2... this is a lie and understandably leads to installing the wrong wheel
# and thus things do not work (see #21938 for an example).
ret["_PYTHON_HOST_PLATFORM"] = "macosx-13.0-x86_64"
Copy link
Contributor

@tdyas tdyas Feb 10, 2025

Choose a reason for hiding this comment

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

Should we also have logic in the GHA workflow to check that the wheel's filename is as expected? I.e. that no universal2 wheels were generated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, yeah, I'll add some extra checks to release.py

@huonw
Copy link
Contributor Author

huonw commented Feb 11, 2025

Woohoo. This looks better. From the CI logs:

Compared to a problematic build (e.g. the 2.26.0.dev0 release):

@huonw huonw marked this pull request as ready for review February 11, 2025 01:59
@huonw huonw requested review from tdyas and benjyw February 11, 2025 02:00
Copy link
Contributor

@benjyw benjyw left a comment

Choose a reason for hiding this comment

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

Yay! Thanks for fixing

@huonw huonw merged commit 2147e17 into main Feb 11, 2025
24 checks passed
@huonw huonw deleted the huonw/21938-python-host-platform branch February 11, 2025 05:27
@WorkerPants
Copy link
Member

I tried to automatically cherry-pick this change back to each relevant milestone, so that it is available in those older releases of Pants.

✔️ 2.25.x

Successfully opened #21944.


Thanks again for your contributions!

🤖 Beep Boop here's my run link

@huonw huonw mentioned this pull request Feb 11, 2025
huonw added a commit that referenced this pull request Feb 11, 2025
…21942) (#21944)

This follows the lead of `cibuildwheel` to explicitly correctly tag our
wheels on macOS, via the `_PYTHON_HOST_PLATFORM` environment variable.
This change ensures that the `pip` understands which platform each wheel
is built for and compatible with, and thus external plugins using
`pants_requirements(...)` work again.

It appears the file name (including platform) tags of a wheel are
derived from `sysconfig.get_platform()`, i.e. based on the build
configuration of the current Python interpreter. Pants builds single
platform wheels, so the wheels need to be tagged with the right CPU
architecture.

In #21655, we changed from tagging correctly to tagging incorrectly:

- Before that change, our self-hosted runners had 'simple' Python
interpreters, built for the current platform via Pyenv. `python -c
'import sysconfig; print(sysconfig.get_platform())' reports
`macosx-10.15-x86_64` and `macosx-11.6-arm64`, and indeed the wheels
were tagged like that.
- After that change, we started using Python interpreters provided by
GitHub-hosted runners. The interpreters report `macosx-10.9-universal2`
on both the ARM64 and x86-64 runners. That is, they're built as
universal binaries that can run on either architecture. This lead to the
wheels being tagged as `universal2` as well.

The `_PYTHON_HOST_PLATFORM` environment variable overrides the default
`sysconfig.get_platform()` value, and appears to be designed for this
sort of purpose (and is used by `cibuildwheel` for such):

-
https://github.com/python/cpython/blob/5505b91a684b0fc7ffcb3a5b325302671d74fb15/Lib/sysconfig.py#L652-L654
-
https://github.com/pypa/cibuildwheel/blob/3805787fe7a0476391541d834fa548a721f0ab2e/cibuildwheel/macos.py#L318-L331

Fixes #21938

Co-authored-by: Huon Wilson <huon@exoflare.io>
@tdyas tdyas mentioned this pull request Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes:not-required PR doesn't require mention in release notes
Projects
None yet
4 participants