Skip to content

feat(toolchain): use the uv.lock based coverage hub for the WORKSPACE toolchains - #4457

Draft
aignas wants to merge 22 commits into
bazel-contrib:aignas.feat.coverage-platform-whlsfrom
aignas:aignas.feat.coverage-hub-workspace
Draft

aignas wants to merge 22 commits into
bazel-contrib:aignas.feat.coverage-platform-whlsfrom
aignas:aignas.feat.coverage-hub-workspace

Conversation

@aignas

@aignas aignas commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

With WORKSPACE, python_register_toolchains(register_coverage_tool = True) now uses the same coverage wheels as bzlmod, which are locked
in python/private/coverage/uv.lock, without any changes to the users'
WORKSPACE files. This replaces the hand-maintained coverage_deps.bzl
and its update script.

The WORKSPACE macros cannot read the uv.lock file, so this adds a
vendor_uv_lock macro in //python/uv:vendor_uv_lock.bzl, which
vendors the needed subset of a uv.lock file as a JSON string constant
in a .bzl file. It takes the name of the lock target and its
.update target updates both files, so a single
bazel run //python/private/coverage:uv_lock_bzl.update is enough, and
its diff_test does not need the network.

python_register_toolchains selects the wheels from the vendored lock
with the same parse_requirements code as pip.parse and creates a
repository for each wheel and the @rules_python_coverage hub. The
WORKSPACE builds do not set the Python version flag by default, so the
executables cannot select the coverage package of the toolchain that
is used, like it is done with bzlmod. Instead, the hub has a coverage
tool for each Python version, e.g.
@rules_python_coverage//coverage:3.13, which only selects the wheel
based on the target platform and is set as the
py_runtime.coverage_tool of the toolchains. A py_library cannot be
used there, because it would be a dependency cycle, so the wheels are
extracted as plain files, as before. The repositories are fetched
lazily, so only the wheels that are used are downloaded.

The WORKSPACE of rules_python now enables the coverage tool, so that
the coverage collection tests also run with WORKSPACE.

DO NOT MERGE: awaiting #4415

Fixes #3902

…uv.lock

The WORKSPACE macros cannot read the coverage `uv.lock` file, so add a
dev-only repository rule, which uses the same wheel selection as the
`pip.parse` extension to write the wheels for each Python version and
platform to a checked-in `workspace_deps.bzl` file. The file is generated
with bzlmod only and a `diff_test` ensures that it is in sync with the
`uv.lock` file. Run `bazel run //dev/update_deps:update_coverage_workspace_deps`
to update it.

This replaces the `update_coverage_deps` script, which maintained the wheel
list by hand.
… toolchains

With WORKSPACE, `python_register_toolchains(register_coverage_tool = True)`
now creates the `@rules_python_coverage` hub from the checked-in
`workspace_deps.bzl` file, so the WORKSPACE toolchains use the same coverage
wheels as bzlmod without any changes to the `WORKSPACE` file. The WORKSPACE
builds do not set the Python version flag by default, so the hub has a
coverage tool for each Python version, which only selects the wheel based on
the target platform, e.g. `@rules_python_coverage//coverage:3.13`.

The `coverage_deps.bzl` file is removed and the coverage collection test now
also runs with WORKSPACE.
@aignas

aignas commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

This PR was drafted by an AI coding agent (Claude Code) working for @aignas as part of the plan in #3902. It stays a draft until @aignas has reviewed it. Review comments will be handled by @aignas, possibly with the help of the agent; any agent-written replies will be marked as such.

Agent-generated comment, posted automatically on behalf of the user.

…olchains

The `python/repositories.bzl` file is loaded before `py_repositories` defines
the `rules_python_internal` repository, which `whl_library` depends on, so
loading it created a cycle in the WORKSPACE file. Use `http_archive` with a
`py_library` BUILD file for the coverage wheels instead, which only depends
on `bazel_tools`.
WORKSPACE macros cannot read files, so add a `vendor_uv_lock` macro that
vendors a `uv.lock` file as a JSON string constant in a `.bzl` file. Only the
fields that are needed for selecting the distributions are kept and each
package and distribution is on its own line, so the diffs are small.

The macro takes the name of the `lock` target that updates the `uv.lock` file
and creates `.update` and `.run` targets, which run the same targets of the
`lock` first and then vendor the updated `uv.lock`, so a single command
updates both files. The `diff_test` only depends on the `uv.lock` file and
does not need the network.
…ndored uv.lock

Replace the dev-only repository rule, which generated the wheel table, with
the `vendor_uv_lock` macro. The WORKSPACE toolchains now decode the vendored
`uv.lock` and select the wheels with the same code as `pip.parse` in a pure
`select_wheels` function, which has unit tests. The `coverage_repositories`
macro gets its params from the vendored JSON constant and only selects the
wheels once, as it is called for each toolchain platform.

`bazel run //python/private/coverage:uv_lock_bzl.update` now updates both the
`uv.lock` and the vendored file, and it is part of
`bazel run //dev:all_requirements.update`.
…hon_internal

The `patch_whl.bzl` and `whl_extract.bzl` files loaded the
`@rules_python_internal` repository only to get the `supports_whl_extraction`
and `extract_needs_chmod` values, which only depend on the Bazel version.
Compute them from `native.bazel_version` in a new `extract_features` function,
which `internal_config_repo` also uses.

This allows loading `whl_library` from the files that are loaded before
`py_repositories` defines `@rules_python_internal` in WORKSPACE, e.g.
`python/repositories.bzl`.
The `pypi_repo_utils.bzl` and `util.bzl` files only used `types.is_string` and
`types.is_tuple` from `@bazel_skylib`, which are simple `type()` checks.
Without them, `whl_library` can be loaded from the files that are loaded
before the `WORKSPACE` defines `@bazel_skylib`, e.g. `python/repositories.bzl`.
Replace the private `extract_features` function with a public
`bazel_features(bazel_version)` function in `//python:features.bzl`, which
returns the features of a Bazel version, e.g.
`bazel_features(native.bazel_version).supports_whl_extraction`. The Bazel
version is only available when a repository rule or a module extension runs,
so this is a function and not fields of the `features` struct.

The values are no longer written to the `@rules_python_internal` config, as
nothing reads them from there anymore.
Now that `whl_library` can be loaded from `python/repositories.bzl` (bazel-contrib#4458),
use it for the coverage wheels instead of a hand-crafted `http_archive`, so
the wheels are extracted the same way as for the pip hubs.
rickeylev pushed a commit to rickeylev/rules_python that referenced this pull request Nov 13, 2026
…CE (bazel-contrib#4458)

In WORKSPACE, `python/repositories.bzl` is loaded before
`py_repositories()`
defines `@rules_python_internal` and `@bazel_skylib`, so nothing loaded
from it
can depend on those repositories. The WORKSPACE toolchains in bazel-contrib#4457 need
to
use `whl_library` for the coverage wheels, which means the `whl_library`
load
chain must only load `@bazel_tools`.

The `whl_extract.bzl` and `patch_whl.bzl` files loaded
`@rules_python_internal`
only to read `supports_whl_extraction` and `extract_needs_chmod`, which
depend
on nothing but the Bazel version. They now compute these from
`native.bazel_version` themselves, and the values are no longer written
to the
`@rules_python_internal` config. The `pypi_repo_utils.bzl` and
`util.bzl` files
loaded `@bazel_skylib` only for `types.is_string` and `types.is_tuple`,
so
those are replaced with plain `type()` checks.

Work towards bazel-contrib#3902
@aignas
aignas changed the base branch from main to aignas.feat.coverage-platform-whls November 22, 2026 12:45

This branch has not been deployed

No deployments
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.

feat(toolchain): allow users easily override coverage

1 participant