Skip to content

Commit 9ad0674

Browse files
committed
Only set py_runtime.coverage_tool for Bazel 6 and higher. (bazel-contrib#1061)
Only set `py_runtime.coverage_tool` for Bazel 6 and higher. Avoid setting it in earlier version by checking `native.bazel_version` in the repository rule and disabling it if less than Bazel 6 is detected. A warning is also printed if coverage was requested, but the Bazel version check is ignoring it. Fixes bazel-contrib#1056
1 parent 6c8ae76 commit 9ad0674

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

python/repositories.bzl

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,21 @@ def _python_repository_impl(rctx):
243243
"share/**",
244244
]
245245

246+
if rctx.attr.coverage_tool:
247+
if "windows" in rctx.os.name:
248+
coverage_tool = None
249+
else:
250+
coverage_tool = '"{}"'.format(rctx.attr.coverage_tool)
251+
252+
coverage_attr_text = """\
253+
coverage_tool = select({{
254+
":coverage_enabled": {coverage_tool},
255+
"//conditions:default": None
256+
}}),
257+
""".format(coverage_tool = coverage_tool)
258+
else:
259+
coverage_attr_text = " # coverage_tool attribute not supported by this Bazel version"
260+
246261
build_content = """\
247262
# Generated by python/repositories.bzl
248263
@@ -308,10 +323,7 @@ config_setting(
308323
py_runtime(
309324
name = "py3_runtime",
310325
files = [":files"],
311-
coverage_tool = select({{
312-
":coverage_enabled": {coverage_tool},
313-
"//conditions:default": None,
314-
}}),
326+
{coverage_attr}
315327
interpreter = "{python_path}",
316328
python_version = "PY3",
317329
)
@@ -327,7 +339,7 @@ py_runtime_pair(
327339
python_path = python_bin,
328340
python_version = python_short_version,
329341
python_version_nodot = python_short_version.replace(".", ""),
330-
coverage_tool = rctx.attr.coverage_tool if rctx.attr.coverage_tool == None or "windows" in rctx.os.name else "\"{}\"".format(rctx.attr.coverage_tool),
342+
coverage_attr = coverage_attr_text,
331343
)
332344
rctx.delete("python")
333345
rctx.symlink(python_bin, "python")
@@ -459,6 +471,8 @@ def python_register_toolchains(
459471
distutils_content: see the distutils_content attribute in the python_repository repository rule.
460472
register_toolchains: Whether or not to register the downloaded toolchains.
461473
register_coverage_tool: Whether or not to register the downloaded coverage tool to the toolchains.
474+
NOTE: Coverage support using the toolchain is only supported in Bazel 6 and higher.
475+
462476
set_python_version_constraint: When set to true, target_compatible_with for the toolchains will include a version constraint.
463477
tool_versions: a dict containing a mapping of version with SHASUM and platform info. If not supplied, the defaults
464478
in python/versions.bzl will be used.
@@ -472,6 +486,19 @@ def python_register_toolchains(
472486

473487
toolchain_repo_name = "{name}_toolchains".format(name = name)
474488

489+
bazel_major = int(native.bazel_version.split(".")[0])
490+
if bazel_major < 6:
491+
if register_coverage_tool:
492+
# buildifier: disable=print
493+
print((
494+
"WARNING: ignoring register_coverage_tool=True when " +
495+
"registering @{name}: Bazel 6+ required, got {version}"
496+
).format(
497+
name = name,
498+
version = native.bazel_version,
499+
))
500+
register_coverage_tool = False
501+
475502
for platform in PLATFORMS.keys():
476503
sha256 = tool_versions[python_version]["sha256"].get(platform, None)
477504
if not sha256:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy