Skip to content

Commit 81437b7

Browse files
committed
fix(uv): fix UV_BIN usage with current_toolchain
Before this PR the `uv` toolchain could not be used in a `genrule` it seems because the `//python/uv:toolchain` does not have the necessary providers for using the make variables and the re-exporting of the make variables from the `toolchain` in the `current_toolchain` rule did not seem to work. This PR removes the provider construction in the `toolchain` and does that only in the `current_toolchain`. This better mirrors how the Python toolchains are setup (grepping `PYTHON3` is sufficient to get examples). This also splits out work done in bazel-contrib#2059 to decrease its scope, so that this can be discussed separately. Work towards bazel-contrib#1975
1 parent bf70429 commit 81437b7

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, execute
66
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
7+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
8+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
99

1010
test --test_output=errors
1111

MODULE.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,17 @@ use_repo(
130130
"build_bazel_bazel_rolling",
131131
"build_bazel_bazel_self",
132132
)
133+
134+
# EXPERIMENTAL: This is experimental and may be removed without notice
135+
uv = use_extension(
136+
"//python/uv:extensions.bzl",
137+
"uv",
138+
dev_dependency = True,
139+
)
140+
uv.toolchain(uv_version = "0.2.23")
141+
use_repo(uv, "uv_toolchains")
142+
143+
register_toolchains(
144+
"@uv_toolchains//:all",
145+
dev_dependency = True,
146+
)

python/uv/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ current_toolchain(
4141
# even if no toolchain is registered.
4242
tags = ["manual"],
4343
# EXPERIMENTAL: Visibility is restricted to allow for changes.
44-
visibility = ["@rules_python//examples:__subpackages__"],
44+
visibility = [
45+
"//:__subpackages__",
46+
"@rules_python//examples:__subpackages__",
47+
],
4548
)
4649

4750
bzl_library(

python/uv/private/current_toolchain.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def _current_toolchain_impl(ctx):
3030
# Bazel requires executable rules to create the executable themselves,
3131
# so we create a symlink in this rule so that it appears this rule created its executable.
3232
original_uv_executable = toolchain_info.uv_toolchain_info.uv[DefaultInfo].files_to_run.executable
33-
symlink_uv_executable = ctx.actions.declare_file("uv_symlink_{}".format(original_uv_executable.basename))
33+
34+
# Use `uv` as the name of the binary to make the help message well formatted
35+
symlink_uv_executable = ctx.actions.declare_file("current_toolchain/uv".format(original_uv_executable.basename))
3436
ctx.actions.symlink(output = symlink_uv_executable, target_file = original_uv_executable)
3537

3638
new_default_info = DefaultInfo(
@@ -39,10 +41,14 @@ def _current_toolchain_impl(ctx):
3941
executable = symlink_uv_executable,
4042
)
4143

44+
template_variable_info = platform_common.TemplateVariableInfo({
45+
"UV_BIN": symlink_uv_executable.path,
46+
})
47+
4248
return [
4349
toolchain_info,
4450
new_default_info,
45-
toolchain_info.template_variable_info,
51+
template_variable_info,
4652
toolchain_info.uv_toolchain_info,
4753
]
4854

python/uv/toolchain.bzl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,16 @@ def _uv_toolchain_impl(ctx):
3131
uv = uv,
3232
version = ctx.attr.version,
3333
)
34-
template_variable_info = platform_common.TemplateVariableInfo({
35-
"UV_BIN": uv[DefaultInfo].files_to_run.executable.path,
36-
})
3734

3835
# Export all the providers inside our ToolchainInfo
3936
# so the current_toolchain rule can grab and re-export them.
4037
toolchain_info = platform_common.ToolchainInfo(
4138
default_info = default_info,
42-
template_variable_info = template_variable_info,
4339
uv_toolchain_info = uv_toolchain_info,
4440
)
4541
return [
4642
default_info,
4743
toolchain_info,
48-
template_variable_info,
4944
]
5045

5146
uv_toolchain = rule(

tests/uv/toolchain/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
2+
3+
genrule(
4+
name = "uv_help",
5+
outs = ["uv_help.txt"],
6+
cmd = "$(UV_BIN) --python-fetch manual --help >$@",
7+
toolchains = [
8+
"//python/uv:current_toolchain",
9+
"//python:current_py_toolchain",
10+
],
11+
)
12+
13+
build_test(
14+
name = "test_uv_toolchain_make_vars",
15+
targets = [":uv_help"],
16+
)

0 commit comments

Comments
 (0)