-
-
Notifications
You must be signed in to change notification settings - Fork 98
refactor: move unpack.py into a configurable toolchain #1432
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
Open
jbedard
wants to merge
2
commits into
main
Choose a base branch
from
toolchains-unpack
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| load("@aspect_rules_py//py:defs.bzl", "py_test") | ||
| load("@bazel_lib//lib:transitions.bzl", "platform_transition_test") | ||
| load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") | ||
| load("@rules_cc//cc:cc_binary.bzl", "cc_binary") | ||
| load(":toolchain.bzl", "custom_unpack_toolchain") | ||
|
|
||
| # End-to-end check for the exec-tools toolchain's swappable `unpack_tool` | ||
| # (docs/interpreter.md, "Custom wheel-unpack tool"): a self-contained C binary | ||
| # replaces the default unpack.py for the WhlInstall action installing a real | ||
| # uv-locked wheel (iniconfig, reused from uv-whl-install-output-group's hub). | ||
| # | ||
| # The toolchain is registered module-wide (see MODULE.bazel) but gated by | ||
| # `target_settings` on the flag below, flipped only inside | ||
| # platform_transition_test — sibling cases keep the default tool. Each tool | ||
| # writes a distinctive dist-info INSTALLER (the C tool its own marker, the | ||
| # reference unpack.py `aspect_rules_py`); the tests read it to prove which | ||
| # tool installed the wheel in each configuration. | ||
|
|
||
| # POSIX-only C tool; mirror the unpack_test guard. | ||
| _NOT_WINDOWS = select({ | ||
| "@platforms//os:windows": ["@platforms//:incompatible"], | ||
| "//conditions:default": [], | ||
| }) | ||
|
|
||
| bool_flag( | ||
| name = "use_custom_unpack", | ||
| build_setting_default = False, | ||
| ) | ||
|
|
||
| config_setting( | ||
| name = "custom_unpack_enabled", | ||
| flag_values = {":use_custom_unpack": "true"}, | ||
| ) | ||
|
|
||
| cc_binary( | ||
| name = "unpack_tool", | ||
| srcs = ["unpack_tool.c"], | ||
| target_compatible_with = _NOT_WINDOWS, | ||
| deps = ["@zlib"], | ||
| ) | ||
|
|
||
| custom_unpack_toolchain( | ||
| name = "c_unpack", | ||
| unpack_tool = ":unpack_tool", | ||
| ) | ||
|
|
||
| toolchain( | ||
| name = "c_unpack_toolchain", | ||
| target_settings = [":custom_unpack_enabled"], | ||
| toolchain = ":c_unpack", | ||
| toolchain_type = "@aspect_rules_py//py/private/toolchain:exec_tools_toolchain_type", | ||
| ) | ||
|
|
||
| platform( | ||
| name = "custom_unpack_platform", | ||
| flags = ["--//custom-unpack-tool:use_custom_unpack=true"], | ||
| parents = ["@platforms//host"], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "custom_test_bin", | ||
| srcs = ["custom_test.py"], | ||
| dep_group = "uv-whl-install-output-group", | ||
| main = "custom_test.py", | ||
| tags = ["manual"], | ||
| target_compatible_with = _NOT_WINDOWS, | ||
| deps = ["@pypi_uv_whl_install_output_group//iniconfig"], | ||
| ) | ||
|
|
||
| platform_transition_test( | ||
| name = "custom_test", | ||
| binary = ":custom_test_bin", | ||
| target_compatible_with = _NOT_WINDOWS, | ||
| target_platform = ":custom_unpack_platform", | ||
| ) | ||
|
|
||
| # Control: without the flag the gated toolchain must not match and the | ||
| # default unpack.py installs the wheel. | ||
| py_test( | ||
| name = "default_test", | ||
| srcs = ["default_test.py"], | ||
| dep_group = "uv-whl-install-output-group", | ||
| main = "default_test.py", | ||
| target_compatible_with = _NOT_WINDOWS, | ||
| deps = ["@pypi_uv_whl_install_output_group//iniconfig"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| """Runs with --//custom-unpack-tool:use_custom_unpack=true: the wheel must | ||
| have been installed by the C unpack tool, not the default unpack.py.""" | ||
|
|
||
| import pathlib | ||
|
|
||
| import iniconfig | ||
|
|
||
| site_packages = pathlib.Path(iniconfig.__file__).resolve().parent.parent | ||
| dist_infos = sorted(site_packages.glob("iniconfig-*.dist-info")) | ||
| assert len(dist_infos) == 1, "expected one iniconfig dist-info, found %s" % dist_infos | ||
|
|
||
| installer = dist_infos[0] / "INSTALLER" | ||
| assert installer.is_file(), ( | ||
| "INSTALLER missing: the custom C unpack tool did not run (%s)" % installer | ||
| ) | ||
| content = installer.read_text(encoding="utf-8") | ||
| assert content == "rules_py-e2e-c-unpack-tool\n", ( | ||
| "unexpected INSTALLER content %r: wheel was not installed by the C tool" % content | ||
| ) | ||
| assert (dist_infos[0] / "REQUESTED").is_file() | ||
|
|
||
| # whl_install passes --compile-pyc by default; the C tool must have run | ||
| # compileall under the exec interpreter. | ||
| pycs = list((site_packages / "iniconfig" / "__pycache__").glob("__init__.*.pyc")) | ||
| assert pycs, "no compiled bytecode: the C tool skipped --compile-pyc" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| """Runs without the flag: the default unpack.py must have installed the wheel. | ||
|
|
||
| The reference tool stamps dist-info INSTALLER with `aspect_rules_py`; the C | ||
| tool's marker here would mean the flag-gated custom toolchain leaked into the | ||
| default configuration.""" | ||
|
|
||
| import pathlib | ||
|
|
||
| import iniconfig | ||
|
|
||
| site_packages = pathlib.Path(iniconfig.__file__).resolve().parent.parent | ||
| dist_infos = sorted(site_packages.glob("iniconfig-*.dist-info")) | ||
| assert len(dist_infos) == 1, "expected one iniconfig dist-info, found %s" % dist_infos | ||
|
|
||
| installer = dist_infos[0] / "INSTALLER" | ||
| assert installer.is_file(), "INSTALLER missing from %s" % dist_infos[0] | ||
| content = installer.read_text(encoding="utf-8") | ||
| assert content == "aspect_rules_py", ( | ||
| "unexpected INSTALLER content %r: custom unpack toolchain matched without its flag" | ||
| % content | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| """Test-only exec-tools toolchain wrapping a self-contained unpack binary. | ||
|
|
||
| Mirrors what a user registering a custom `unpack_tool` writes today: the | ||
| toolchain resolves the standard Python toolchain type for its runtime payloads | ||
| (safe — this target is registered only under the exec-tools type, so that | ||
| resolution cannot cycle back into it) and exposes the binary as the opaque | ||
| `unpack_tool` struct consumed by PyUnpackedWheel/WhlInstall actions. | ||
| """ | ||
|
|
||
| PY_TOOLCHAIN = "@bazel_tools//tools/python:toolchain_type" | ||
|
|
||
| def _custom_unpack_toolchain_impl(ctx): | ||
| return [platform_common.ToolchainInfo( | ||
| exec_runtime = ctx.toolchains[PY_TOOLCHAIN].py3_runtime, | ||
| unpack_tool = struct( | ||
| executable = ctx.attr.unpack_tool[DefaultInfo].files_to_run, | ||
| arguments = [], | ||
| inputs = depset(), | ||
| ), | ||
| )] | ||
|
|
||
| custom_unpack_toolchain = rule( | ||
| implementation = _custom_unpack_toolchain_impl, | ||
| attrs = { | ||
| "unpack_tool": attr.label( | ||
| doc = "Self-contained executable implementing the unpack CLI contract.", | ||
| executable = True, | ||
| cfg = "target", | ||
| mandatory = True, | ||
| ), | ||
| }, | ||
| toolchains = [PY_TOOLCHAIN], | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Avoid importing
iniconfigbefore the bytecode check.The import on Line 6 can create the matching
.pycfile. The assertion then does not prove thatunpack_toolprocessed--compile-pyc. Resolve the module path withimportlib.util.find_spec()instead.🤖 Prompt for AI Agents