Skip to content

Commit 60b910c

Browse files
committed
cleanup: rename version file, add filegroup, fix var names, make public
1 parent 5bc1416 commit 60b910c

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

python/private/local_runtime_repo.bzl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@ _OnFailure = enum(
2525
FAIL = "fail",
2626
)
2727

28-
_TOOLCHAIN_IMPL_TEMPLATE = """\
28+
_BUILD_BAZEL_TEMPLATE = """\
2929
# Generated by python/private/local_runtime_repo.bzl
3030
31-
load("@rules_python//python/private:local_runtime_repo_setup.bzl", "define_local_runtime_toolchain_impl")
31+
load(
32+
"@rules_python//python/private:local_runtime_repo_setup.bzl",
33+
"define_local_runtime_toolchain_impl",
34+
"define_local_runtime_targets",
35+
)
36+
37+
package(
38+
default_visibility = ["//visibility:public"]
39+
)
3240
3341
define_local_runtime_toolchain_impl(
3442
name = "local_runtime",
@@ -41,6 +49,8 @@ define_local_runtime_toolchain_impl(
4149
implementation_name = "{implementation_name}",
4250
os = "{os}",
4351
)
52+
53+
define_local_runtime_targets()
4454
"""
4555

4656
RUNTIME_INFO_BZL_TEMPLATE = """
@@ -175,7 +185,7 @@ def _local_runtime_repo_impl(rctx):
175185
else:
176186
logger.warn("No external python libraries found.")
177187

178-
build_bazel = _TOOLCHAIN_IMPL_TEMPLATE.format(
188+
build_bazel = _BUILD_BAZEL_TEMPLATE.format(
179189
major = info["major"],
180190
minor = info["minor"],
181191
micro = info["micro"],
@@ -201,7 +211,9 @@ def _local_runtime_repo_impl(rctx):
201211
))
202212

203213
# Text format for `python.toolchain.python_version_file`
204-
rctx.file("version.txt", "{major}.{minor}".format(**info))
214+
# The name `python-version` is used to match pyenv and uv naming that looks
215+
# for a `.python-version` file.
216+
rctx.file("python-version", "{major}.{minor}".format(**info))
205217

206218
local_runtime_repo = repository_rule(
207219
implementation = _local_runtime_repo_impl,
@@ -282,7 +294,7 @@ How to handle errors when trying to automatically determine settings.
282294
)
283295

284296
def _expand_incompatible_template():
285-
return _TOOLCHAIN_IMPL_TEMPLATE.format(
297+
return _BUILD_BAZEL_TEMPLATE.format(
286298
interpreter_path = "/incompatible",
287299
implementation_name = "incompatible",
288300
interface_library = "None",

python/private/local_runtime_repo_setup.bzl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Setup code called by the code generated by `local_runtime_repo`."""
1616

17+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1718
load("@bazel_skylib//lib:selects.bzl", "selects")
1819
load("@rules_cc//cc:cc_import.bzl", "cc_import")
1920
load("@rules_cc//cc:cc_library.bzl", "cc_library")
@@ -168,6 +169,23 @@ def define_local_runtime_toolchain_impl(
168169
visibility = ["//visibility:public"],
169170
)
170171

172+
def define_local_runtime_targets():
173+
native.filegroup(
174+
name = "python-version",
175+
srcs = ["python-version"],
176+
)
177+
native.filegroup(
178+
name = "runtime_info",
179+
srcs = ["runtime_info.json"],
180+
)
181+
bzl_library(
182+
name = "runtime_info_bzl",
183+
srcs = ["runtime_info.bzl"],
184+
deps = [
185+
Label("//python/private:local_runtime_repo_setup.bzl"),
186+
],
187+
)
188+
171189
def create_info_struct(info):
172190
self = struct(
173191
_info = info,

tests/integration/local_toolchains/MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ register_toolchains("@local_toolchains//:all")
9999

100100
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
101101
python.defaults(
102-
python_version_file = "@local_python3//:version.txt",
102+
python_version_file = "@local_python3//:python-version",
103103
)
104104

105105
# todo: loosen toolchain checking. These lines are only necessary to satisfy

0 commit comments

Comments
 (0)