Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(uv): fix UV_BIN usage with current_toolchain #2074

Merged
merged 3 commits into from
Jul 19, 2024
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
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
# To update these lines, execute
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
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
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
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
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

test --test_output=errors

Expand Down
14 changes: 14 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,17 @@ use_repo(
"build_bazel_bazel_rolling",
"build_bazel_bazel_self",
)

# EXPERIMENTAL: This is experimental and may be removed without notice
uv = use_extension(
"//python/uv:extensions.bzl",
"uv",
dev_dependency = True,
)
uv.toolchain(uv_version = "0.2.23")
use_repo(uv, "uv_toolchains")

register_toolchains(
"@uv_toolchains//:all",
dev_dependency = True,
)
5 changes: 4 additions & 1 deletion python/uv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ current_toolchain(
# even if no toolchain is registered.
tags = ["manual"],
# EXPERIMENTAL: Visibility is restricted to allow for changes.
visibility = ["@rules_python//examples:__subpackages__"],
visibility = [
"//:__subpackages__",
"@rules_python//examples:__subpackages__",
],
)

bzl_library(
Expand Down
10 changes: 8 additions & 2 deletions python/uv/private/current_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def _current_toolchain_impl(ctx):
# Bazel requires executable rules to create the executable themselves,
# so we create a symlink in this rule so that it appears this rule created its executable.
original_uv_executable = toolchain_info.uv_toolchain_info.uv[DefaultInfo].files_to_run.executable
symlink_uv_executable = ctx.actions.declare_file("uv_symlink_{}".format(original_uv_executable.basename))

# Use `uv` as the name of the binary to make the help message well formatted
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The only reason I went with a weird name, was because I was worried about Windows. I might be wrong, but I thought Windows requires(ed) that executables have the .exe suffix?

symlink_uv_executable = ctx.actions.declare_file("current_toolchain/uv".format(original_uv_executable.basename))
ctx.actions.symlink(output = symlink_uv_executable, target_file = original_uv_executable)

new_default_info = DefaultInfo(
Expand All @@ -39,10 +41,14 @@ def _current_toolchain_impl(ctx):
executable = symlink_uv_executable,
)

template_variable_info = platform_common.TemplateVariableInfo({
"UV_BIN": symlink_uv_executable.path,
})

return [
toolchain_info,
new_default_info,
toolchain_info.template_variable_info,
template_variable_info,
toolchain_info.uv_toolchain_info,
]

Expand Down
5 changes: 0 additions & 5 deletions python/uv/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,16 @@ def _uv_toolchain_impl(ctx):
uv = uv,
version = ctx.attr.version,
)
template_variable_info = platform_common.TemplateVariableInfo({
"UV_BIN": uv[DefaultInfo].files_to_run.executable.path,
})

# Export all the providers inside our ToolchainInfo
# so the current_toolchain rule can grab and re-export them.
toolchain_info = platform_common.ToolchainInfo(
default_info = default_info,
template_variable_info = template_variable_info,
uv_toolchain_info = uv_toolchain_info,
)
return [
default_info,
toolchain_info,
template_variable_info,
]

uv_toolchain = rule(
Expand Down
22 changes: 22 additions & 0 deletions tests/uv/toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("//python:py_test.bzl", "py_test")
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility

# We only test this feature when `bzlmod` is enabled.
_TARGET_COMPATIBLE_WITH = [] if BZLMOD_ENABLED else ["@platforms//:incompatible"]

genrule(
name = "uv_help",
outs = ["uv_help.txt"],
cmd = "$(UV_BIN) --python-fetch manual --help >$@",
target_compatible_with = _TARGET_COMPATIBLE_WITH,
toolchains = ["//python/uv:current_toolchain"],
)

py_test(
name = "uv_help_test",
srcs = ["uv_help_test.py"],
data = [":uv_help"],
env = {"DATA": "$(rlocationpath :uv_help)"},
target_compatible_with = _TARGET_COMPATIBLE_WITH,
deps = ["//python/runfiles"],
)
27 changes: 27 additions & 0 deletions tests/uv/toolchain/uv_help_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python

import os
import unittest
from pathlib import Path

from python.runfiles import runfiles


class TestUV(unittest.TestCase):
def test_uv_help(self):
rfiles = runfiles.Create()
assert rfiles is not None, "rfiles creation failed"

data_rpath = os.environ["DATA"]
uv_help_path = rfiles.Rlocation(data_rpath)
assert (
uv_help_path is not None
), f"the rlocation path was not found: {data_rpath}"

uv_help = Path(uv_help_path).read_text()

self.assertIn("Usage: uv [OPTIONS] <COMMAND>", uv_help)


if __name__ == "__main__":
unittest.main()