Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ python_register_multi_toolchains(
python_versions = PYTHON_VERSIONS,
)

load("//python/uv:repositories.bzl", "uv_toolchains_manifest")

uv_toolchains_manifest(name = "uv_manifest")

load("@uv_manifest//:dist_manifest.bzl", "uv_register_toolchains")

uv_register_toolchains(name = "uv")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Used for Bazel CI
Expand Down
1 change: 1 addition & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ sphinx_stardocs(
"//python/private/zipapp:py_zipapp_rule",
"//python/uv",
"//python/uv:lock",
"//python/uv:repositories",
"//python/uv:uv_toolchain",
"//python/uv:uv_toolchain_info",
"//python/zipapp:py_zipapp_binary",
Expand Down
111 changes: 110 additions & 1 deletion docs/pypi/lock.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,121 @@ compile_pip_requirements(
)
```

### uv pip compile (bzlmod only)
### uv pip compile

We also have a setup for the `uv pip compile` way of generating lock files.
This is well tested with the public PyPI index, but you may hit some rough
edges with private mirrors.

#### Setup

A `uv` toolchain has to be registered, as well as a Python toolchain, which
{obj}`lock` uses to run `uv` with a hermetic interpreter.

##### Bzlmod

`rules_python` registers the `uv` toolchains for you; `MODULE.bazel` only has
to configure the version to download with the {obj}`uv` module extension:

```starlark
uv = use_extension(
"@rules_python//python/uv:uv.bzl",
"uv",
dev_dependency = True,
)
uv.configure(version = "0.13.5")
```

Without `version`, the default version specified by `rules_python` is used.
See {obj}`uv.configure` for using a mirror, explicit URLs or extra platforms.

(uv-workspace-setup)=
##### WORKSPACE

The setup has two steps: {obj}`uv_toolchains_manifest` fetches the release
manifests of the `uv` versions you want, and the {obj}`uv_register_toolchains`
it exposes creates the toolchains for the platforms those releases have
archives for and registers them:

```starlark
load("@rules_python//python/uv:repositories.bzl", "uv_toolchains_manifest")

uv_toolchains_manifest(
name = "uv_manifest",
versions = ["0.13.5"],
)

load("@uv_manifest//:dist_manifest.bzl", "uv_register_toolchains")

uv_register_toolchains(name = "uv")
```

Any released `uv` version can be fetched this way; the download URLs and
checksums are read from the release manifests. The `configure` argument of
{obj}`uv_register_toolchains` takes the same entries as {obj}`uv.configure`.

Bazel prefers toolchains in registration order, so to replace one of the
registered toolchains with your own, register yours before calling
`uv_register_toolchains()` and remove ours; to add one without configuring it
here, register yours after:

```starlark
uv_toolchains_manifest(
name = "uv_manifest",
versions = ["0.13.5", "0.6.5"],
)

load("@uv_manifest//:dist_manifest.bzl", "uv_register_toolchains")

# Preferred over the toolchains registered below.
register_toolchains("//tools/uv:patched_linux_toolchain")

uv_register_toolchains(
name = "uv",
configure = [
# Replaced by the toolchain registered above.
{"platform": "x86_64-unknown-linux-gnu"},
# An additional platform for 0.13.5.
{
"platform": "aarch64-unknown-linux-musl",
"compatible_with": [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
"target_settings": [Label("//tools/uv:is_musl")],
},
# The older version with the default platforms. Its toolchains
# come after those of 0.13.5 in `@uv//:all`, so they are only
# used where no earlier toolchain matches.
{"version": "0.6.5"},
],
)

# Used only when none of the toolchains above match.
register_toolchains("//tools/uv:freebsd_toolchain")
```

Using a manifest is optional. Without {obj}`uv_toolchains_manifest`, load
{obj}`uv_register_toolchains` directly from
`@rules_python//python/uv:repositories.bzl` and specify `urls` for every
platform:

```starlark
load("@rules_python//python/uv:repositories.bzl", "uv_register_toolchains")

uv_register_toolchains(
name = "uv",
version = "0.13.5",
configure = [
{
"platform": "x86_64-unknown-linux-gnu",
"urls": ["https://mirror.example.com/uv/uv-0.13.5-x86_64-unknown-linux-gnu.tar.gz"],
"sha256": "...",
},
],
)
```

#### Example usage

```starlark
Expand Down
4 changes: 4 additions & 0 deletions news/1975.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(uv) The {obj}`lock` macro now works in WORKSPACE builds; set up the `uv`
toolchains with the new {obj}`uv_toolchains_manifest` repository rule and
{obj}`uv_register_toolchains` macro.
([#1975](https://github.com/bazel-contrib/rules_python/issues/1975))
9 changes: 9 additions & 0 deletions python/uv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ bzl_library(
deps = ["//python/uv/private:lock"],
)

bzl_library(
name = "repositories",
srcs = ["repositories.bzl"],
deps = [
"//python/uv/private:uv_register_toolchains",
"//python/uv/private:uv_toolchains_manifest",
],
)

bzl_library(
name = "uv",
srcs = ["uv.bzl"],
Expand Down
52 changes: 46 additions & 6 deletions python/uv/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")

package(default_visibility = ["//:__subpackages__"])
Expand All @@ -28,7 +27,6 @@ distribution_filegroup(
filegroup(
name = "lock_copier_template",
srcs = ["template/lock_copier.py"],
target_compatible_with = [] if BZLMOD_ENABLED else ["@platforms//:incompatible"],
visibility = _NOT_REALLY_PUBLIC,
)

Expand All @@ -38,7 +36,6 @@ filegroup(
"@platforms//os:windows": ["template/uv_lock.bat"],
"//conditions:default": ["template/uv_lock.sh"],
}),
target_compatible_with = [] if BZLMOD_ENABLED else ["@platforms//:incompatible"],
visibility = _NOT_REALLY_PUBLIC,
)

Expand All @@ -48,7 +45,6 @@ filegroup(
"@platforms//os:windows": ["template/uv_pip_compile.bat"],
"//conditions:default": ["template/uv_pip_compile.sh"],
}),
target_compatible_with = [] if BZLMOD_ENABLED else ["@platforms//:incompatible"],
visibility = _NOT_REALLY_PUBLIC,
)

Expand All @@ -59,6 +55,13 @@ bzl_library(
deps = [":toolchain_types"],
)

bzl_library(
name = "dist_manifest",
srcs = ["dist_manifest.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = ["//python/private:auth"],
)

# keep
bzl_library(
name = "lock",
Expand All @@ -69,7 +72,6 @@ bzl_library(
deps = [
":toolchain_types",
"//python:py_binary",
"//python/private:bzlmod_enabled",
"//python/private:common_labels",
"//python/private:toolchain_types",
"@bazel_skylib//lib:paths",
Expand All @@ -89,11 +91,37 @@ bzl_library(
srcs = ["uv.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = [
":dist_manifest",
":toolchain_types",
":uv_repository",
":uv_toolchain_config",
":uv_toolchains_repo",
"//python/private:auth",
"//python/private:common_labels",
],
)

bzl_library(
name = "uv_register_toolchains",
srcs = ["uv_register_toolchains.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = [
":toolchain_types",
":uv_defaults",
":uv_repository",
":uv_toolchain_config",
":uv_toolchains_repo",
"@bazel_skylib//lib:types",
],
)

bzl_library(
name = "uv_toolchains_manifest",
srcs = ["uv_toolchains_manifest.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = [
":dist_manifest",
":uv_defaults",
"//python/private:auth",
],
)

Expand Down Expand Up @@ -128,11 +156,23 @@ bzl_library(
],
)

bzl_library(
name = "uv_toolchain_config",
srcs = ["uv_toolchain_config.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = ["//python/private:common_labels"],
)

bzl_library(
name = "toolchain_types",
srcs = ["toolchain_types.bzl"],
)

bzl_library(
name = "uv_defaults",
srcs = ["uv_defaults.bzl"],
)

bzl_library(
name = "uv_toolchain_info",
srcs = ["uv_toolchain_info.bzl"],
Expand Down
Loading
Loading