Skip to content

Commit 1551326

Browse files
committed
move extra transition label generation into rules_python_internal
1 parent 5db852b commit 1551326

File tree

9 files changed

+79
-128
lines changed

9 files changed

+79
-128
lines changed

MODULE.bazel

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ bazel_dep(name = "platforms", version = "0.0.11")
1313
# Use py_proto_library directly from protobuf repository
1414
bazel_dep(name = "protobuf", version = "29.0-rc2", repo_name = "com_google_protobuf")
1515

16-
internal_deps = use_extension("//python/private:internal_deps.bzl", "internal_deps")
16+
rules_python_config = use_extension("//python/extensions:config.bzl", "config")
1717
use_repo(
18-
internal_deps,
18+
rules_python_config,
1919
"pypi__build",
2020
"pypi__click",
2121
"pypi__colorama",
@@ -34,12 +34,6 @@ use_repo(
3434
"rules_python_internal",
3535
)
3636

37-
rules_python_config = use_extension(
38-
"//python/extensions:config.bzl",
39-
"rules_python_config",
40-
)
41-
use_repo(rules_python_config, "rules_python_config")
42-
4337
# We need to do another use_extension call to expose the "pythons_hub"
4438
# repo.
4539
python = use_extension("//python/extensions:python.bzl", "python")
@@ -226,7 +220,7 @@ use_repo(
226220

227221
dev_rules_python_config = use_extension(
228222
"//python/extensions:config.bzl",
229-
"rules_python_config",
223+
"config",
230224
dev_dependency = True,
231225
)
232226
dev_rules_python_config.add_transition_setting(

python/extensions/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ bzl_library(
4545
srcs = ["config.bzl"],
4646
visibility = ["//:__subpackages__"],
4747
deps = [
48-
"//python/private:rules_python_config_repo_bzl",
48+
"//python/private:internal_config_repo_bzl",
4949
],
5050
)

python/extensions/config.bzl

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
"""Extension for configuring global settings of rules_python."""
22

3-
load("//python/private:rules_python_config_repo.bzl", "rules_python_config_repo")
4-
5-
def _rules_python_config_impl(mctx):
6-
transition_setting_generators = {}
7-
for mod in mctx.modules:
8-
for tag in mod.tags.add_transition_setting:
9-
setting = str(tag.setting)
10-
if setting not in transition_setting_generators:
11-
transition_setting_generators[setting] = []
12-
transition_setting_generators[setting].append(mod.name)
13-
14-
rules_python_config_repo(
15-
name = "rules_python_config",
16-
transition_setting_generators = transition_setting_generators,
17-
transition_settings = transition_setting_generators.keys(),
18-
)
3+
load("@bazel_skylib//lib:modules.bzl", "modules")
4+
load("//python/private:internal_config_repo.bzl", "internal_config_repo")
5+
load("//python/private/pypi:deps.bzl", "pypi_deps")
196

207
_add_transition_setting = tag_class(
218
doc = """
@@ -35,13 +22,32 @@ to repositories that are expensive to create or invalidate frequently.
3522
},
3623
)
3724

38-
rules_python_config = module_extension(
25+
def _config_impl(mctx):
26+
transition_setting_generators = {}
27+
transition_settings = []
28+
for mod in mctx.modules:
29+
for tag in mod.tags.add_transition_setting:
30+
setting = str(tag.setting)
31+
if setting not in transition_setting_generators:
32+
transition_setting_generators[setting] = []
33+
transition_settings.append(setting)
34+
transition_setting_generators[setting].append(mod.name)
35+
36+
internal_config_repo(
37+
name = "rules_python_internal",
38+
transition_setting_generators = transition_setting_generators,
39+
transition_settings = transition_settings,
40+
)
41+
42+
pypi_deps()
43+
44+
config = module_extension(
3945
doc = """Global settings for rules_python.
4046
4147
:::{versionadded} VERSION_NEXT_FEATURE
4248
:::
4349
""",
44-
implementation = _rules_python_config_impl,
50+
implementation = _config_impl,
4551
tag_classes = {
4652
"add_transition_setting": _add_transition_setting,
4753
},

python/private/BUILD.bazel

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,6 @@ bzl_library(
506506
":bazel_tools_bzl",
507507
":internal_config_repo_bzl",
508508
":pythons_hub_bzl",
509-
":rules_python_config_repo_bzl",
510509
"//python:versions_bzl",
511510
"//python/private/pypi:deps_bzl",
512511
],
@@ -627,14 +626,6 @@ bzl_library(
627626
],
628627
)
629628

630-
bzl_library(
631-
name = "rules_python_config_repo_bzl",
632-
srcs = ["rules_python_config_repo.bzl"],
633-
deps = [
634-
":repo_utils_bzl",
635-
],
636-
)
637-
638629
bzl_library(
639630
name = "sentinel_bzl",
640631
srcs = ["sentinel.bzl"],
@@ -672,7 +663,7 @@ bzl_library(
672663
deps = [
673664
"common_labels_bzl",
674665
"@bazel_skylib//lib:collections",
675-
"@rules_python_config//:extra_transition_settings_bzl",
666+
"@rules_python_internal//:extra_transition_settings_bzl",
676667
],
677668
)
678669

python/private/internal_config_repo.bzl

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ settings for rules to later use.
1919
"""
2020

2121
load(":repo_utils.bzl", "repo_utils")
22+
load("//python/private:text_util.bzl", "render")
2223

2324
_ENABLE_PIPSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PIPSTAR"
2425
_ENABLE_PIPSTAR_DEFAULT = "0"
@@ -27,7 +28,7 @@ _ENABLE_PYSTAR_DEFAULT = "1"
2728
_ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME = "RULES_PYTHON_DEPRECATION_WARNINGS"
2829
_ENABLE_DEPRECATION_WARNINGS_DEFAULT = "0"
2930

30-
_CONFIG_TEMPLATE = """\
31+
_CONFIG_TEMPLATE = """
3132
config = struct(
3233
enable_pystar = {enable_pystar},
3334
enable_pipstar = {enable_pipstar},
@@ -40,12 +41,12 @@ config = struct(
4041

4142
# The py_internal symbol is only accessible from within @rules_python, so we have to
4243
# load it from there and re-export it so that rules_python can later load it.
43-
_PY_INTERNAL_SHIM = """\
44+
_PY_INTERNAL_SHIM = """
4445
load("@rules_python//tools/build_defs/python/private:py_internal_renamed.bzl", "py_internal_renamed")
4546
py_internal_impl = py_internal_renamed
4647
"""
4748

48-
ROOT_BUILD_TEMPLATE = """\
49+
ROOT_BUILD_TEMPLATE = """
4950
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
5051
5152
package(
@@ -64,6 +65,26 @@ bzl_library(
6465
srcs = ["py_internal.bzl"],
6566
deps = [{py_internal_dep}],
6667
)
68+
69+
bzl_library(
70+
name = "extra_transition_settings_bzl",
71+
srcs = ["extra_transition_settings.bzl"],
72+
)
73+
"""
74+
75+
_EXTRA_TRANSITIONS_TEMPLATE = """
76+
# Generated by @rules_python//python/private:internal_config_repo.bzl
77+
#
78+
# For a list of what modules added what labels, see
79+
# transition_settings_debug.txt
80+
81+
EXTRA_TRANSITION_SETTINGS = {labels}
82+
"""
83+
84+
_TRANSITION_SETTINGS_DEBUG_TEMPLATE = """
85+
# Generated by @rules_python//python/private:internal_config_repo.bzl
86+
87+
{lines}
6788
"""
6889

6990
def _internal_config_repo_impl(rctx):
@@ -113,13 +134,33 @@ def _internal_config_repo_impl(rctx):
113134
visibility = visibility,
114135
))
115136
rctx.file("py_internal.bzl", shim_content)
137+
138+
rctx.file(
139+
"extra_transition_settings.bzl",
140+
_EXTRA_TRANSITIONS_TEMPLATE.format(
141+
labels = render.list(rctx.attr.transition_settings),
142+
),
143+
)
144+
debug_lines = [
145+
"{} added by modules: {}".format(setting, ", ".join(sorted(requesters)))
146+
for setting, requesters in rctx.attr.transition_setting_generators.items()
147+
]
148+
rctx.file(
149+
"transition_settings_debug.txt",
150+
_TRANSITION_SETTINGS_DEBUG_TEMPLATE.format(lines = "\n".join(debug_lines)),
151+
)
152+
116153
return None
117154

118155
internal_config_repo = repository_rule(
119156
implementation = _internal_config_repo_impl,
120157
configure = True,
121158
environ = [_ENABLE_PYSTAR_ENVVAR_NAME],
159+
attrs = {
160+
"transition_setting_generators": attr.string_list_dict(),
161+
"transition_settings": attr.string_list(),
162+
},
122163
)
123164

124165
def _bool_from_environ(rctx, key, default):
125-
return bool(int(repo_utils.getenv(rctx, key, default)))
166+
return bool(int(repo_utils.getenv(rctx, key, default)))

python/private/internal_deps.bzl

Lines changed: 0 additions & 22 deletions
This file was deleted.

python/private/py_repositories.bzl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ load("//python:versions.bzl", "MINOR_MAPPING", "TOOL_VERSIONS")
2020
load("//python/private/pypi:deps.bzl", "pypi_deps")
2121
load(":internal_config_repo.bzl", "internal_config_repo")
2222
load(":pythons_hub.bzl", "hub_repo")
23-
load(":rules_python_config_repo.bzl", "rules_python_config_repo")
2423

2524
def http_archive(**kwargs):
2625
maybe(_http_archive, **kwargs)
@@ -35,15 +34,14 @@ def py_repositories(transition_settings = []):
3534
transition_settings: A list of labels that terminal rules transition on
3635
by default.
3736
"""
37+
38+
# NOTE: The @rules_python_internal repo is special cased by Bazel: it
39+
# has autoloading disabled. This allows the rules to load from it
40+
# without triggering recursion.
3841
maybe(
3942
internal_config_repo,
4043
name = "rules_python_internal",
4144
)
42-
maybe(
43-
rules_python_config_repo,
44-
name = "rules_python_config",
45-
transition_settings = transition_settings,
46-
)
4745
maybe(
4846
hub_repo,
4947
name = "pythons_hub",

python/private/rules_python_config_repo.bzl

Lines changed: 0 additions & 57 deletions
This file was deleted.

python/private/transition_labels.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Terminal rules are e.g. py_binary, py_test, or packaging rules.
44
"""
55

66
load("@bazel_skylib//lib:collections.bzl", "collections")
7-
load("@rules_python_config//:extra_transition_settings.bzl", "EXTRA_TRANSITION_SETTINGS")
7+
load("@rules_python_internal//:extra_transition_settings.bzl", "EXTRA_TRANSITION_SETTINGS")
88
load(":common_labels.bzl", "labels")
99

1010
_BASE_TRANSITION_LABELS = [

0 commit comments

Comments
 (0)