Skip to content

Commit 5b8fa22

Browse files
authored
fix(toolchain): restrict coverage tool visibility under bzlmod (#1252)
Before this PR the `coverage_tool` automatically registered by `rules_python` was visible outside the toolchain repository. This fixes it to be consistent with `non-bzlmod` setups and ensures that the default `coverage_tool` is not visible outside the toolchain repos. This means that the `MODULE.bazel` file can be cleaned-up at the expense of relaxing the `coverage_tool` attribute for the `python_repository` to be a simple string as the label would be evaluated within the context of `rules_python` which may not necessarily resolve correctly without the `use_repo` statement in our `MODULE.bazel`.
1 parent fe2c325 commit 5b8fa22

File tree

7 files changed

+18
-76
lines changed

7 files changed

+18
-76
lines changed

MODULE.bazel

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,6 @@ use_repo(
2929
"pypi__tomli",
3030
"pypi__wheel",
3131
"pypi__zipp",
32-
# coverage_deps managed by running ./tools/update_coverage_deps.py <version>
33-
"pypi__coverage_cp310_aarch64-apple-darwin",
34-
"pypi__coverage_cp310_aarch64-unknown-linux-gnu",
35-
"pypi__coverage_cp310_x86_64-apple-darwin",
36-
"pypi__coverage_cp310_x86_64-unknown-linux-gnu",
37-
"pypi__coverage_cp311_aarch64-apple-darwin",
38-
"pypi__coverage_cp311_aarch64-unknown-linux-gnu",
39-
"pypi__coverage_cp311_x86_64-apple-darwin",
40-
"pypi__coverage_cp311_x86_64-unknown-linux-gnu",
41-
"pypi__coverage_cp38_aarch64-apple-darwin",
42-
"pypi__coverage_cp38_aarch64-unknown-linux-gnu",
43-
"pypi__coverage_cp38_x86_64-apple-darwin",
44-
"pypi__coverage_cp38_x86_64-unknown-linux-gnu",
45-
"pypi__coverage_cp39_aarch64-apple-darwin",
46-
"pypi__coverage_cp39_aarch64-unknown-linux-gnu",
47-
"pypi__coverage_cp39_x86_64-apple-darwin",
48-
"pypi__coverage_cp39_x86_64-unknown-linux-gnu",
4932
)
5033

5134
# We need to do another use_extension call to expose the "pythons_hub"

examples/bzlmod/description.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Before this PR the `coverage_tool` automatically registered by `rules_python`
2+
was visible outside the toolchain repository. This fixes it to be consistent
3+
with `non-bzlmod` setups and ensures that the default `coverage_tool` is not
4+
visible outside the toolchain repos.
5+
6+
This means that the `MODULE.bazel` file can be cleaned-up at the expense of
7+
relaxing the `coverage_tool` attribute for the `python_repository` to be a
8+
simple string as the label would be evaluated within the context of
9+
`rules_python` which may not necessarily resolve correctly without the
10+
`use_repo` statement in our `MODULE.bazel`.

examples/bzlmod/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_coverage_sys_path(self):
6666
if os.environ.get("COVERAGE_MANIFEST"):
6767
# we are running under the 'bazel coverage :test'
6868
self.assertTrue(
69-
"pypi__coverage_cp" in last_item,
69+
"_coverage" in last_item,
7070
f"Expected {last_item} to be related to coverage",
7171
)
7272
self.assertEqual(pathlib.Path(last_item).name, "coverage")

python/extensions/private/internal_deps.bzl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
"Python toolchain module extension for internal rule use"
1010

1111
load("@rules_python//python/pip_install:repositories.bzl", "pip_install_dependencies")
12-
load("@rules_python//python/private:coverage_deps.bzl", "install_coverage_deps")
1312

1413
# buildifier: disable=unused-variable
1514
def _internal_deps_impl(module_ctx):
1615
pip_install_dependencies()
17-
install_coverage_deps()
1816

1917
internal_deps = module_extension(
2018
doc = "This extension to register internal rules_python dependecies.",

python/private/coverage_deps.bzl

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717

1818
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1919
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
20-
load(
21-
"//python:versions.bzl",
22-
"MINOR_MAPPING",
23-
"PLATFORMS",
24-
)
2520

2621
# Update with './tools/update_coverage_deps.py <version>'
2722
#START: managed by update_coverage_deps.py script
@@ -103,16 +98,14 @@ _coverage_deps = {
10398

10499
_coverage_patch = Label("//python/private:coverage.patch")
105100

106-
def coverage_dep(name, python_version, platform, visibility, install = True):
101+
def coverage_dep(name, python_version, platform, visibility):
107102
"""Register a singe coverage dependency based on the python version and platform.
108103
109104
Args:
110105
name: The name of the registered repository.
111106
python_version: The full python version.
112107
platform: The platform, which can be found in //python:versions.bzl PLATFORMS dict.
113108
visibility: The visibility of the coverage tool.
114-
install: should we install the dependency with a given name or generate the label
115-
of the bzlmod dependency fallback, which is hard-coded in MODULE.bazel?
116109
117110
Returns:
118111
The label of the coverage tool if the platform is supported, otherwise - None.
@@ -131,17 +124,6 @@ def coverage_dep(name, python_version, platform, visibility, install = True):
131124
# Some wheels are not present for some builds, so let's silently ignore those.
132125
return None
133126

134-
if not install:
135-
# FIXME @aignas 2023-01-19: right now we use globally installed coverage
136-
# which has visibility set to public, but is hidden due to repo remapping.
137-
#
138-
# The name of the toolchain is not known when registering the coverage tooling,
139-
# so we use this as a workaround for now.
140-
return Label("@pypi__coverage_{abi}_{platform}//:coverage".format(
141-
abi = abi,
142-
platform = platform,
143-
))
144-
145127
maybe(
146128
http_archive,
147129
name = name,
@@ -162,26 +144,4 @@ filegroup(
162144
urls = [url],
163145
)
164146

165-
return Label("@@{name}//:coverage".format(name = name))
166-
167-
def install_coverage_deps():
168-
"""Register the dependency for the coverage dep.
169-
170-
This is only used under bzlmod.
171-
"""
172-
173-
for python_version in MINOR_MAPPING.values():
174-
for platform in PLATFORMS.keys():
175-
if "windows" in platform:
176-
continue
177-
178-
coverage_dep(
179-
name = "pypi__coverage_cp{version_no_dot}_{platform}".format(
180-
version_no_dot = python_version.rpartition(".")[0].replace(".", ""),
181-
platform = platform,
182-
),
183-
python_version = python_version,
184-
platform = platform,
185-
visibility = ["//visibility:public"],
186-
install = True,
187-
)
147+
return "@{name}//:coverage".format(name = name)

python/repositories.bzl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,9 @@ python_repository = repository_rule(
374374
_python_repository_impl,
375375
doc = "Fetches the external tools needed for the Python toolchain.",
376376
attrs = {
377-
"coverage_tool": attr.label(
377+
"coverage_tool": attr.string(
378378
# Mirrors the definition at
379379
# https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/python/py_runtime_rule.bzl
380-
allow_files = False,
381380
doc = """
382381
This is a target to use for collecting code coverage information from `py_binary`
383382
and `py_test` targets.
@@ -392,6 +391,9 @@ The entry point for the tool must be loadable by a Python interpreter (e.g. a
392391
of coverage.py (https://coverage.readthedocs.io), at least including
393392
the `run` and `lcov` subcommands.
394393
394+
The target is accepted as a string by the python_repository and evaluated within
395+
the context of the toolchain repository.
396+
395397
For more information see the official bazel docs
396398
(https://bazel.build/reference/be/python#py_runtime.coverage_tool).
397399
""",
@@ -535,11 +537,10 @@ def python_register_toolchains(
535537
),
536538
python_version = python_version,
537539
platform = platform,
538-
visibility = ["@@{name}_{platform}//:__subpackages__".format(
540+
visibility = ["@{name}_{platform}//:__subpackages__".format(
539541
name = name,
540542
platform = platform,
541543
)],
542-
install = not bzlmod,
543544
)
544545

545546
python_repository(

tools/update_coverage_deps.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,6 @@ def main():
241241
dry_run=args.dry_run,
242242
)
243243

244-
# Update the MODULE.bazel, which needs to expose the dependencies to the toolchain
245-
# repositories
246-
_update_file(
247-
path=rules_python / "MODULE.bazel",
248-
snippet="".join(sorted([f' "{u.repo_name}",\n' for u in urls])),
249-
start_marker=" # coverage_deps managed by running",
250-
end_marker=")",
251-
dry_run=args.dry_run,
252-
)
253-
254244
return
255245

256246

0 commit comments

Comments
 (0)