From 214993c518212d89f67185427a1805c25e55baea Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Thu, 22 Oct 2026 09:10:23 -0700 Subject: [PATCH] refactor(examples): replace compile_pip_requirements with uv lock rule `compile_pip_requirements` relies on `pip-compile`, whereas the `//python/uv:lock.bzl%lock` rule uses `uv` for faster and more modern lockfile generation. Replace `compile_pip_requirements` targets across the examples with `//python/uv:lock.bzl%lock`. Where examples previously generated a separate Windows lockfile via `requirements_windows`, define a second `lock` target passing `--python-platform=windows`. --- examples/build_file_generation/BUILD.bazel | 16 +++++++--- examples/bzlmod/BUILD.bazel | 27 +++++++++++----- examples/bzlmod/other_module/BUILD.bazel | 8 ++--- .../requirements/BUILD.bazel | 32 +++++++++---------- examples/pip_parse/BUILD.bazel | 20 +++++++++--- examples/pip_parse_vendored/BUILD.bazel | 7 ++-- examples/wheel/BUILD.bazel | 8 ++--- .../bzlmod_build_file_generation/BUILD.bazel | 18 +++++++---- 8 files changed, 85 insertions(+), 51 deletions(-) diff --git a/examples/build_file_generation/BUILD.bazel b/examples/build_file_generation/BUILD.bazel index bfff6f3273..23e90736c4 100644 --- a/examples/build_file_generation/BUILD.bazel +++ b/examples/build_file_generation/BUILD.bazel @@ -4,18 +4,24 @@ # ruleset. When the symbol is loaded you can use the rule. load("@bazel_gazelle//:def.bzl", "gazelle") load("@pip//:requirements.bzl", "all_whl_requirements") -load("@rules_python//python:pip.bzl", "compile_pip_requirements") load("@rules_python//python:py_binary.bzl", "py_binary") load("@rules_python//python:py_library.bzl", "py_library") load("@rules_python//python:py_test.bzl", "py_test") +load("@rules_python//python/uv:lock.bzl", "lock") load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest") load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping") -compile_pip_requirements( +lock( name = "requirements", - src = "pyproject.toml", - requirements_txt = "requirements_lock.txt", - requirements_windows = "requirements_windows.txt", + srcs = ["pyproject.toml"], + out = "requirements_lock.txt", +) + +lock( + name = "requirements_windows", + srcs = ["pyproject.toml"], + out = "requirements_windows.txt", + args = ["--python-platform=windows"], ) # This repository rule fetches the metadata for python packages we diff --git a/examples/bzlmod/BUILD.bazel b/examples/bzlmod/BUILD.bazel index 7877105a84..95d8a9e83a 100644 --- a/examples/bzlmod/BUILD.bazel +++ b/examples/bzlmod/BUILD.bazel @@ -8,23 +8,34 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@pip//:requirements.bzl", "all_data_requirements", "all_requirements", "all_whl_requirements", "requirement") load("@python_3_13//:defs.bzl", py_test_with_transition = "py_test") -load("@python_versions//3.10:defs.bzl", compile_pip_requirements_3_10 = "compile_pip_requirements") load("@rules_python//python:py_binary.bzl", "py_binary") load("@rules_python//python:py_library.bzl", "py_library") load("@rules_python//python:py_test.bzl", "py_test") +load("@rules_python//python/uv:lock.bzl", "lock") # This stanza calls a rule that generates targets for managing pip dependencies -# with pip-compile for a particular python version. -compile_pip_requirements_3_10( +# with uv for a particular python version. +lock( name = "requirements_3_10", - timeout = "moderate", - src = "pyproject.toml", - extra_args = [ + srcs = ["pyproject.toml"], + out = "requirements_lock_3_10.txt", + args = [ "--extra-index-url", "https://pypi.org/simple/", ], - requirements_txt = "requirements_lock_3_10.txt", - requirements_windows = "requirements_windows_3_10.txt", + python_version = "3.10", +) + +lock( + name = "requirements_windows_3_10", + srcs = ["pyproject.toml"], + out = "requirements_windows_3_10.txt", + args = [ + "--extra-index-url", + "https://pypi.org/simple/", + "--python-platform=windows", + ], + python_version = "3.10", ) # The rules below are language specific rules defined in diff --git a/examples/bzlmod/other_module/BUILD.bazel b/examples/bzlmod/other_module/BUILD.bazel index 3998665a84..c8585f1062 100644 --- a/examples/bzlmod/other_module/BUILD.bazel +++ b/examples/bzlmod/other_module/BUILD.bazel @@ -1,10 +1,10 @@ -load("@rules_python//python:pip.bzl", "compile_pip_requirements") +load("@rules_python//python/uv:lock.bzl", "lock") # NOTE: To update the requirements, you need to uncomment the rules_python # override in the MODULE.bazel. -compile_pip_requirements( +lock( name = "requirements", - src = "pyproject.toml", + srcs = ["pyproject.toml"], + out = "requirements_lock_3_11.txt", python_version = "3.11", - requirements_txt = "requirements_lock_3_11.txt", ) diff --git a/examples/multi_python_versions/requirements/BUILD.bazel b/examples/multi_python_versions/requirements/BUILD.bazel index bf3f129330..836d41f43a 100644 --- a/examples/multi_python_versions/requirements/BUILD.bazel +++ b/examples/multi_python_versions/requirements/BUILD.bazel @@ -1,36 +1,36 @@ -load("@rules_python//python:pip.bzl", "compile_pip_requirements") +load("@rules_python//python/uv:lock.bzl", "lock") -compile_pip_requirements( +lock( name = "requirements_3_10", - src = "pyproject.toml", + srcs = ["pyproject.toml"], + out = "requirements_lock_3_10.txt", python_version = "3.10", - requirements_txt = "requirements_lock_3_10.txt", ) -compile_pip_requirements( +lock( name = "requirements_3_11", - src = "pyproject.toml", + srcs = ["pyproject.toml"], + out = "requirements_lock_3_11.txt", python_version = "3.11", - requirements_txt = "requirements_lock_3_11.txt", ) -compile_pip_requirements( +lock( name = "requirements_3_12", - src = "pyproject.toml", + srcs = ["pyproject.toml"], + out = "requirements_lock_3_12.txt", python_version = "3.12", - requirements_txt = "requirements_lock_3_12.txt", ) -compile_pip_requirements( +lock( name = "requirements_3_13", - src = "pyproject.toml", + srcs = ["pyproject.toml"], + out = "requirements_lock_3_13.txt", python_version = "3.13", - requirements_txt = "requirements_lock_3_13.txt", ) -compile_pip_requirements( +lock( name = "requirements_3_14", - src = "pyproject.toml", + srcs = ["pyproject.toml"], + out = "requirements_lock_3_14.txt", python_version = "3.14", - requirements_txt = "requirements_lock_3_14.txt", ) diff --git a/examples/pip_parse/BUILD.bazel b/examples/pip_parse/BUILD.bazel index 014fcf25f4..294aa5f513 100644 --- a/examples/pip_parse/BUILD.bazel +++ b/examples/pip_parse/BUILD.bazel @@ -1,7 +1,7 @@ -load("@rules_python//python:pip.bzl", "compile_pip_requirements") load("@rules_python//python:py_binary.bzl", "py_binary") load("@rules_python//python:py_test.bzl", "py_test") load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary") +load("@rules_python//python/uv:lock.bzl", "lock") # Toolchain setup, this is optional. # Demonstrate that we can use the same python interpreter for the toolchain and executing pip in pip install (see WORKSPACE). @@ -51,15 +51,25 @@ py_console_script_binary( ) # This rule adds a convenient way to update the requirements file. -compile_pip_requirements( +lock( name = "requirements", - src = "pyproject.toml", + srcs = ["pyproject.toml"], + out = "requirements_lock.txt", + constraints = [ + "constraints_certifi.txt", + "constraints_urllib3.txt", + ], +) + +lock( + name = "requirements_windows", + srcs = ["pyproject.toml"], + out = "requirements_windows.txt", + args = ["--python-platform=windows"], constraints = [ "constraints_certifi.txt", "constraints_urllib3.txt", ], - requirements_txt = "requirements_lock.txt", - requirements_windows = "requirements_windows.txt", ) # Test the use of all pip_parse utilities in a single py_test diff --git a/examples/pip_parse_vendored/BUILD.bazel b/examples/pip_parse_vendored/BUILD.bazel index 08a3bdbb28..b6e94b8a9a 100644 --- a/examples/pip_parse_vendored/BUILD.bazel +++ b/examples/pip_parse_vendored/BUILD.bazel @@ -1,16 +1,17 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@bazel_skylib//rules:diff_test.bzl", "diff_test") load("@bazel_skylib//rules:write_file.bzl", "write_file") -load("@rules_python//python:pip.bzl", "compile_pip_requirements") load("@rules_python//python:py_test.bzl", "py_test") +load("@rules_python//python/uv:lock.bzl", "lock") load("@rules_shell//shell:sh_binary.bzl", "sh_binary") load("//:requirements.bzl", "all_data_requirements", "all_requirements", "all_whl_requirements", "requirement") # This rule adds a convenient way to update the requirements.txt # lockfile based on the pyproject.toml. -compile_pip_requirements( +lock( name = "requirements", - src = "pyproject.toml", + srcs = ["pyproject.toml"], + out = "requirements.txt", ) # The requirements.bzl file is using the hub repo to access packages via the diff --git a/examples/wheel/BUILD.bazel b/examples/wheel/BUILD.bazel index cdc7953adb..e84e130e32 100644 --- a/examples/wheel/BUILD.bazel +++ b/examples/wheel/BUILD.bazel @@ -16,12 +16,12 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@bazel_skylib//rules:write_file.bzl", "write_file") load("//examples/wheel/private:wheel_utils.bzl", "directory_writer", "make_variable_tags") load("//python:packaging.bzl", "py_package", "py_wheel") -load("//python:pip.bzl", "compile_pip_requirements") load("//python:py_library.bzl", "py_library") load("//python:py_test.bzl", "py_test") load("//python:versions.bzl", "gen_python_config_settings") load("//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary") load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility +load("//python/uv:lock.bzl", "lock") package(default_visibility = ["//visibility:public"]) @@ -473,10 +473,10 @@ py_test( # Test wheel publishing -compile_pip_requirements( +lock( name = "requirements_server", - src = "pyproject.toml", - requirements_txt = "requirements_server.txt", + srcs = ["pyproject.toml"], + out = "requirements_server.txt", ) py_test( diff --git a/gazelle/examples/bzlmod_build_file_generation/BUILD.bazel b/gazelle/examples/bzlmod_build_file_generation/BUILD.bazel index 081bd79138..97f0f519fd 100644 --- a/gazelle/examples/bzlmod_build_file_generation/BUILD.bazel +++ b/gazelle/examples/bzlmod_build_file_generation/BUILD.bazel @@ -7,20 +7,26 @@ # requirements. load("@bazel_gazelle//:def.bzl", "gazelle") load("@pip//:requirements.bzl", "all_whl_requirements") -load("@rules_python//python:pip.bzl", "compile_pip_requirements") load("@rules_python//python:py_binary.bzl", "py_binary") load("@rules_python//python:py_library.bzl", "py_library") load("@rules_python//python:py_test.bzl", "py_test") +load("@rules_python//python/uv:lock.bzl", "lock") load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest") load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping") # This stanza calls a rule that generates targets for managing pip dependencies -# with pip-compile. -compile_pip_requirements( +# with uv. +lock( name = "requirements", - src = "pyproject.toml", - requirements_txt = "requirements_lock.txt", - requirements_windows = "requirements_windows.txt", + srcs = ["pyproject.toml"], + out = "requirements_lock.txt", +) + +lock( + name = "requirements_windows", + srcs = ["pyproject.toml"], + out = "requirements_windows.txt", + args = ["--python-platform=windows"], ) # This repository rule fetches the metadata for python packages we