Skip to content

fix(packaging): build the .publish twine binary for the exec platform#3909

Open
rdelfin wants to merge 1 commit into
bazel-contrib:mainfrom
rdelfin:fix/py-wheel-publish-exec-platform
Open

fix(packaging): build the .publish twine binary for the exec platform#3909
rdelfin wants to merge 1 commit into
bazel-contrib:mainfrom
rdelfin:fix/py-wheel-publish-exec-platform

Conversation

@rdelfin

@rdelfin rdelfin commented Jul 6, 2026

Copy link
Copy Markdown

The .publish target generated by py_wheel wraps twine in a native_binary whose src attribute uses cfg = "target". When the build uses a cross-compilation platform (e.g. --platforms=linux_aarch64), this causes bazel to resolve twine for the target platform rather than the host, which fails because rules_python_publish_deps only provides wheels for the host platform.

Twine is a developer tool that uploads wheels to PyPI; it only ever runs on the developer's workstation and should always be built for the exec platform regardless of the target.

Replace the native_binary call with a minimal inline rule, _py_wheel_publish_binary, that is identical except its src attribute uses cfg = "exec".

The .publish target generated by py_wheel wraps twine in a native_binary
whose src attribute uses cfg = "target". When the build uses a
cross-compilation platform (e.g. --platforms=linux_aarch64), this causes
bazel to resolve twine for the target platform rather than the host,
which fails because rules_python_publish_deps only provides wheels for
the host platform.

Twine is a developer tool that uploads wheels to PyPI; it only ever runs
on the developer's workstation and should always be built for the exec
platform regardless of the target.

Replace the native_binary call with a minimal inline rule,
_py_wheel_publish_binary, that is identical except its src attribute uses
cfg = "exec".
Copilot AI review requested due to automatic review settings July 6, 2026 17:22
@rdelfin rdelfin requested review from aignas and rickeylev as code owners July 6, 2026 17:22

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request replaces the usage of bazel_skylib's native_binary with a custom _py_wheel_publish_binary rule in python/packaging.bzl to ensure the twine binary is built for the execution platform. The review feedback suggests adding allow_files = True to the src attribute of the new rule to maintain compatibility with native_binary and prevent analysis-phase errors when passing file targets.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread python/packaging.bzl
Comment on lines +111 to +115
"src": attr.label(
executable = True,
mandatory = True,
cfg = "exec",
),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To ensure full compatibility with bazel_skylib's native_binary and allow passing pre-built binary files directly as src, the src attribute should explicitly set allow_files = True. Without this, passing a file target to src will result in an analysis-phase error.

Suggested change
"src": attr.label(
executable = True,
mandatory = True,
cfg = "exec",
),
"src": attr.label(
executable = True,
allow_files = True,
mandatory = True,
cfg = "exec",
),

Copilot AI 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.

Pull request overview

This PR aims to make the .publish target generated by py_wheel reliably runnable during cross-compilation by ensuring the twine executable is built for the exec (host) platform, not the target platform.

Changes:

  • Remove the @bazel_skylib native_binary dependency from python/packaging.bzl.
  • Add an inline _py_wheel_publish_binary rule intended to mirror native_binary, but with src built using cfg = "exec".
  • Switch the .publish target generation in py_wheel() to use _py_wheel_publish_binary.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread python/packaging.bzl
Comment on lines +107 to +118
_py_wheel_publish_binary = rule(
implementation = _py_wheel_publish_binary_impl,
executable = True,
attrs = {
"src": attr.label(
executable = True,
mandatory = True,
cfg = "exec",
),
"data": attr.label_list(allow_files = True),
"out": attr.string(),
},

@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.

+1 on the direction here and thank you for the fix.

We have lot's of analysis tests in the tests folder and it would be very useful to add a regression test for this. With AI help it should be very doable.

That and please add a news/3909.fixed.md file summarising the fixes here.

Comment thread python/packaging.bzl
**copy_propagating_kwargs(kwargs)
)
elif twine:
py_binary(

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.

Don't we need to update this as well?

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.

3 participants