Skip to content

Commit a55128c

Browse files
committed
Conditionally remove builtin PyInfo and other providers
Use bazel_features to detect if PyInfo is still present in Bazel and stop using it, if it's not. Bazel throws an error while compiling bzl files if there is a reference to a top-level symbol that doesn't exist anymore. bazel_features provide a mechanism that detects presence of top-level symbols without throwing an exception. It either returns the symbol or None. bazel_features support not only Bazel 8, but also older versions of Bazel that have --incompatible_autoload_externally cherry-picked. If the flag is enabled with "@rules_python" or "-@rules_python" the provider is removed from Bazel. Remove also PyRuntimeInfo and PyCcLinkParamsProvider.
1 parent 9c3d303 commit a55128c

File tree

14 files changed

+46
-29
lines changed

14 files changed

+46
-29
lines changed

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module(
44
compatibility_level = 1,
55
)
66

7-
bazel_dep(name = "bazel_features", version = "1.9.1")
7+
bazel_dep(name = "bazel_features", version = "1.18.0")
88
bazel_dep(name = "bazel_skylib", version = "1.6.1")
99
bazel_dep(name = "rules_cc", version = "0.0.9")
1010
bazel_dep(name = "platforms", version = "0.0.4")

internal_deps.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ def rules_python_internal_deps():
212212

213213
http_archive(
214214
name = "bazel_features",
215-
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
216-
strip_prefix = "bazel_features-1.9.1",
217-
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
215+
sha256 = "b4b145c19e08fd48337f53c383db46398d0a810002907ff0c590762d926e05be",
216+
strip_prefix = "bazel_features-1.18.0",
217+
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.18.0/bazel_features-v1.18.0.tar.gz",
218218
)
219219

220220
http_archive(

python/config_settings/transition.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ def _transition_py_impl(ctx):
101101
]
102102
if PyInfo in target:
103103
providers.append(target[PyInfo])
104-
if BuiltinPyInfo in target and PyInfo != BuiltinPyInfo:
104+
if BuiltinPyInfo != None and BuiltinPyInfo in target and PyInfo != BuiltinPyInfo:
105105
providers.append(target[BuiltinPyInfo])
106106

107107
if PyRuntimeInfo in target:
108108
providers.append(target[PyRuntimeInfo])
109-
if BuiltinPyRuntimeInfo in target and PyRuntimeInfo != BuiltinPyRuntimeInfo:
109+
if BuiltinPyRuntimeInfo != None and BuiltinPyRuntimeInfo in target and PyRuntimeInfo != BuiltinPyRuntimeInfo:
110110
providers.append(target[BuiltinPyRuntimeInfo])
111111
return providers
112112

python/private/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,10 @@ bzl_library(
341341
visibility = [
342342
"//:__subpackages__",
343343
],
344-
deps = [":bazel_tools_bzl"],
344+
deps = [
345+
":bazel_tools_bzl",
346+
"@bazel_features//:features",
347+
],
345348
)
346349

347350
bzl_library(

python/private/common/attributes.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,16 @@ COMMON_ATTRS = union_attrs(
253253
allow_none = True,
254254
)
255255

256+
_MaybeBuiltinPyInfo = [[BuiltinPyInfo]] if BuiltinPyInfo != None else []
257+
256258
# Attributes common to rules accepting Python sources and deps.
257259
PY_SRCS_ATTRS = union_attrs(
258260
{
259261
"deps": attr.label_list(
260262
providers = [
261263
[PyInfo],
262264
[CcInfo],
263-
[BuiltinPyInfo],
264-
],
265+
] + _MaybeBuiltinPyInfo,
265266
# TODO(b/228692666): Google-specific; remove these allowances once
266267
# the depot is cleaned up.
267268
allow_rules = DEPS_ATTR_ALLOW_RULES,

python/private/common/common.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def collect_imports(ctx, semantics):
280280
dep[BuiltinPyInfo].imports
281281
for dep in ctx.attr.deps
282282
if BuiltinPyInfo in dep
283-
])
283+
] if BuiltinPyInfo != None else [])
284284

285285
def collect_runfiles(ctx, files = depset()):
286286
"""Collects the necessary files from the rule's context.
@@ -374,7 +374,7 @@ def create_py_info(ctx, *, direct_sources, direct_pyc_files, imports):
374374

375375
for target in ctx.attr.deps:
376376
# PyInfo may not be present e.g. cc_library rules.
377-
if PyInfo in target or BuiltinPyInfo in target:
377+
if PyInfo in target or (BuiltinPyInfo != None and BuiltinPyInfo in target):
378378
py_info.merge(_get_py_info(target))
379379
else:
380380
# TODO(b/228692666): Remove this once non-PyInfo targets are no
@@ -395,7 +395,7 @@ def create_py_info(ctx, *, direct_sources, direct_pyc_files, imports):
395395
for target in ctx.attr.data:
396396
# TODO(b/234730058): Remove checking for PyInfo in data once depot
397397
# cleaned up.
398-
if PyInfo in target or BuiltinPyInfo in target:
398+
if PyInfo in target or (BuiltinPyInfo != None and BuiltinPyInfo in target):
399399
info = _get_py_info(target)
400400
py_info.merge_uses_shared_libraries(info.uses_shared_libraries)
401401
else:
@@ -410,7 +410,7 @@ def create_py_info(ctx, *, direct_sources, direct_pyc_files, imports):
410410
return py_info.build(), deps_transitive_sources, py_info.build_builtin_py_info()
411411

412412
def _get_py_info(target):
413-
return target[PyInfo] if PyInfo in target else target[BuiltinPyInfo]
413+
return target[PyInfo] if PyInfo in target or BuiltinPyInfo == None else target[BuiltinPyInfo]
414414

415415
def create_instrumented_files_info(ctx):
416416
return _coverage_common.instrumented_files_info(

python/private/common/py_executable.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ def _create_providers(
855855
# builtin py_runtime rule or defined their own. We can't directly detect
856856
# the type of the provider object, but the rules_python PyRuntimeInfo
857857
# object has an extra attribute that the builtin one doesn't.
858-
if hasattr(py_runtime_info, "interpreter_version_info"):
858+
if hasattr(py_runtime_info, "interpreter_version_info") and BuiltinPyRuntimeInfo != None:
859859
providers.append(BuiltinPyRuntimeInfo(
860860
interpreter_path = py_runtime_info.interpreter_path,
861861
interpreter = py_runtime_info.interpreter,

python/private/common/py_runtime_rule.bzl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,20 @@ def _py_runtime_impl(ctx):
125125
if not IS_BAZEL_7_OR_HIGHER:
126126
builtin_py_runtime_info_kwargs.pop("bootstrap_template")
127127

128-
return [
128+
providers = [
129129
PyRuntimeInfo(**py_runtime_info_kwargs),
130-
# Return the builtin provider for better compatibility.
131-
# 1. There is a legacy code path in py_binary that
132-
# checks for the provider when toolchains aren't used
133-
# 2. It makes it easier to transition from builtins to rules_python
134-
BuiltinPyRuntimeInfo(**builtin_py_runtime_info_kwargs),
135130
DefaultInfo(
136131
files = runtime_files,
137132
runfiles = runfiles,
138133
),
139134
]
135+
if BuiltinPyRuntimeInfo != None:
136+
# Return the builtin provider for better compatibility.
137+
# 1. There is a legacy code path in py_binary that
138+
# checks for the provider when toolchains aren't used
139+
# 2. It makes it easier to transition from builtins to rules_python
140+
providers.append(BuiltinPyRuntimeInfo(**builtin_py_runtime_info_kwargs))
141+
return providers
140142

141143
# Bind to the name "py_runtime" to preserve the kind/rule_class it shows up
142144
# as elsewhere.

python/private/py_info.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ This field is currently unused in Bazel and may go away in the future.
118118
)
119119

120120
# The "effective" PyInfo is what the canonical //python:py_info.bzl%PyInfo symbol refers to
121-
_EffectivePyInfo = PyInfo if config.enable_pystar else BuiltinPyInfo
121+
_EffectivePyInfo = PyInfo if config.enable_pystar or BuiltinPyInfo == None else BuiltinPyInfo
122122

123123
def PyInfoBuilder():
124124
# buildifier: disable=uninitialized
@@ -201,7 +201,7 @@ def _PyInfoBuilder_merge_all(self, py_infos):
201201
def _PyInfoBuilder_merge_target(self, target):
202202
if PyInfo in target:
203203
self.merge(target[PyInfo])
204-
elif BuiltinPyInfo in target:
204+
elif BuiltinPyInfo != None and BuiltinPyInfo in target:
205205
self.merge(target[BuiltinPyInfo])
206206
return self
207207

python/private/py_repositories.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ def py_repositories():
4646
sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
4747
strip_prefix = "rules_cc-0.0.9",
4848
)
49+
http_archive(
50+
name = "bazel_features",
51+
sha256 = "b4b145c19e08fd48337f53c383db46398d0a810002907ff0c590762d926e05be",
52+
strip_prefix = "bazel_features-1.18.0",
53+
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.18.0/bazel_features-v1.18.0.tar.gz",
54+
)
4955
pypi_deps()

0 commit comments

Comments
 (0)