feat(uv): support WORKSPACE builds in the lock() macro (#4360)

This makes the uv `lock()` rule work in workspace mode. Since we'll
be supporting Bazel 8 for another year, and I'd really like to get
rid of the remaining requirements.txt and compile_pip_requirements
calls, I figured we should just implement workspace support for it.

This is a fairly low-effort conversion of the bzlmod APIs to
workspace mode. Fundamentally, the bzlmod API works by obtaining
one or more manifests, then using the manifest information to
define repos with the uv binaries. Then the names of all those
repos are fed into the toolchain hub where the `toolchain()`
registrations are.

The workspace API more-or-less exposes that same API.

The `uv_toolchain_manifest()` function is the equivalent of the
first half of the bzlmod phase. All it does is download the
manifests so they can be read by subsequent workspace macros
and repos. Like the bzlmod API, a manifest per uv version can
be registered.

The `uv_register_toolchains()` function is the equivalent of the
second half of the bzlmod phase. It takes what's in the manifest
and defines repos pointing to a particular URL. Like the bzlmod
API, it accepts args to customize processing. This function also
keeps track of all the created toolchain implementations and
creates the toolchain hub with all the `toolchain()` registrations.

The args to the functions mirror the bzlmod API, which is basically
a list of key-values. Hence the `uv_register_toolchains.configure`
arg accepts a list of dicts, where the keys are the same as the
attributes of the `uv.configure` tag class. This makes for a
somewhat awkward API, but it matches the bzlmod API and makes it
easier to integrate into the underlying code.

Tested on Bazel 7.x and 8.x in WORKSPACE mode, and on Bazel 7.x, 8.x
and 9.x in bzlmod mode (Bazel 9 no longer evaluates WORKSPACE).

Work towards #1975
30 files changed
tree: 5e04ca29ec5662d846ae232d3a841f213a2318a0
  1. .agents/
  2. .bazelci/
  3. .bcr/
  4. .ci/
  5. .github/
  6. command_line_option/
  7. crates/
  8. dev/
  9. docs/
  10. examples/
  11. gazelle/
  12. news/
  13. python/
  14. sphinxdocs/
  15. tests/
  16. tools/
  17. .bazelignore
  18. .bazelrc
  19. .bazelrc.deleted_packages
  20. .bazelversion
  21. .editorconfig
  22. .git-blame-ignore-revs
  23. .gitattributes
  24. .gitignore
  25. .pre-commit-config.yaml
  26. .python-version
  27. .readthedocs.yml
  28. addlicense.sh
  29. AGENTS.md
  30. AUTHORS
  31. BUILD.bazel
  32. buildbuddy.bazelrc
  33. BZLMOD_SUPPORT.md
  34. CHANGELOG.md
  35. CLAUDE.md
  36. CONTRIBUTING.md
  37. CONTRIBUTORS
  38. downloader_config.cfg
  39. GEMINI.md
  40. internal_dev_deps.bzl
  41. internal_dev_setup.bzl
  42. LICENSE
  43. MODULE.bazel
  44. README.md
  45. RELEASING.md
  46. replicate_ci
  47. ruff.toml
  48. specialized_configs.bazelrc
  49. version.bzl
  50. WORKSPACE
  51. WORKSPACE.bzlmod
  52. workspace_bazel9.bzl
README.md

Python Rules for Bazel

Build status GitHub CI status

Overview

This repository is the home of the core Python rules -- py_library, py_binary, py_test, and related symbols that provide the basis for Python support in Bazel. It also contains package installation rules for integrating with PyPI and other indices.

Documentation for rules_python is at https://rules-python.readthedocs.io and in the Bazel Build Encyclopedia.

Examples live in the examples directory.

The core rules are stable. Their implementation is subject to Bazel's backward compatibility policy. This repository aims to follow semantic versioning.

The Bazel community maintains this repository. Neither Google nor the Bazel team provides support for the code. However, this repository is part of the test suite used to vet new Bazel releases. See How to contribute page for information on our development workflow.

Design

  • Supported OSes - as per our supported platform policy, we strive for support on all of the platforms that we have CI for. Some platforms do not have the same backwards compatibility guarantees, but we hope the community can step in where needed to make the support more robust.
  • requirements.txt is how users have been defining dependencies for a long time. We support this to support legacy usecases or package managers that we don't support directly. Any additional information that we need will be retrieved from the SimpleAPI during the bzlmod extension evaluation phase. Then it will be written to the MODULE.bazel.lock file for future reuse. We have plans to support uv.lock file directly. uv is recommended for generating a fully locked requirements.txt file and we do provide a rule for it.
  • The py_binary, py_test rules should scale to large monorepos and we work hard to minimize the work done during analysis and build phase. What is more, the space requirements for should be minimal, so we strive to use symlinks rather than extracting wheels at build time. This means that for different configurations of the same build, we are not extracting the wheel multiple times thus scaling better over the time. From 2.0 onwards we are creating a virtual env for each target by creating an actual minimal virtual environment using symlinks. We plan on creating the traditional site-packages layout in the future by default.
  • Support for standards - we strive to first implement any standards needed within rules_python and this has resulted in a few PEPs supported within pure starlark - PEP440, PEP509.

Common misconceptions:

  • rules_python has to keep backwards compatibility with google3. Whilst this might have been true in the past, rules_python is an open source project and any compatibility needs should come from the community - we have no requirement to keep this compatibility and are allowed to make our decisions. However, we do want to keep backwards compatibility as long as possible to not upset users with never ending migrations.
  • rules_python is not caching pip downloads. With 2.0, we use Bazel's downloader by default and rely on bazel to provide the repository caching mechanisms. This means that for simpler setups this should result in transparent and scalable caching with the most recent bazel versions unless there are issues in the bazel itself.

Documentation

For detailed documentation, see https://rules-python.readthedocs.io

Bzlmod support

See Bzlmod support for more details.