Skip to content

Commit 5631f05

Browse files
authored
fix(whl_library): only add group machinery when it is needed (#1822)
This removes the `alias` additions when the package groups are not used fixing alias that attempt to traverse the entire py_library dependency tree. I noticed that for some code that I want to write for `whl_library` having it explicit whether we are using groups or not in the `pip.parse` and or `whl_library` makes the code easier to understand and write. Fixes #1760
1 parent e8039db commit 5631f05

File tree

4 files changed

+73
-106
lines changed

4 files changed

+73
-106
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ A brief description of the categories of changes:
2929
* (gazelle) In `project` or `package` generation modes, do not generate `py_test`
3030
rules when there are no test files and do not set `main = "__test__.py"` when
3131
that file doesn't exist.
32+
* (whl_library) The group redirection is only added when the package is part of
33+
the group potentially fixing aspects that want to traverse a `py_library` graph.
34+
Fixes [#1760](https://github.com/bazelbuild/rules_python/issues/1760).
3235

3336
### Added
3437

python/pip_install/private/generate_whl_library_build_bazel.bzl

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ filegroup(
6363
)
6464
6565
filegroup(
66-
name = "{whl_file_impl_label}",
66+
name = "{whl_file_label}",
6767
srcs = ["{whl_name}"],
6868
data = {whl_file_deps},
6969
visibility = {impl_vis},
7070
)
7171
7272
py_library(
73-
name = "{py_library_impl_label}",
73+
name = "{py_library_label}",
7474
srcs = glob(
7575
["site-packages/**/*.py"],
7676
exclude={srcs_exclude},
@@ -89,16 +89,6 @@ py_library(
8989
tags = {tags},
9090
visibility = {impl_vis},
9191
)
92-
93-
alias(
94-
name = "{py_library_public_label}",
95-
actual = "{py_library_actual_label}",
96-
)
97-
98-
alias(
99-
name = "{whl_file_public_label}",
100-
actual = "{whl_file_actual_label}",
101-
)
10292
"""
10393

10494
def _plat_label(plat):
@@ -353,36 +343,45 @@ def generate_whl_library_build_bazel(
353343
# implementation.
354344
if group_name:
355345
group_repo = repo_prefix + "_groups"
356-
library_impl_label = "@%s//:%s_%s" % (group_repo, normalize_name(group_name), PY_LIBRARY_PUBLIC_LABEL)
357-
whl_impl_label = "@%s//:%s_%s" % (group_repo, normalize_name(group_name), WHEEL_FILE_PUBLIC_LABEL)
358-
impl_vis = "@%s//:__pkg__" % (group_repo,)
346+
label_tmpl = "\"@{}//:{}_{{}}\"".format(group_repo, normalize_name(group_name))
347+
impl_vis = ["@{}//:__pkg__".format(group_repo)]
348+
additional_content.extend([
349+
"",
350+
render.alias(
351+
name = PY_LIBRARY_PUBLIC_LABEL,
352+
actual = label_tmpl.format(PY_LIBRARY_PUBLIC_LABEL),
353+
),
354+
"",
355+
render.alias(
356+
name = WHEEL_FILE_PUBLIC_LABEL,
357+
actual = label_tmpl.format(WHEEL_FILE_PUBLIC_LABEL),
358+
),
359+
])
360+
py_library_label = PY_LIBRARY_IMPL_LABEL
361+
whl_file_label = WHEEL_FILE_IMPL_LABEL
359362

360363
else:
361-
library_impl_label = PY_LIBRARY_IMPL_LABEL
362-
whl_impl_label = WHEEL_FILE_IMPL_LABEL
363-
impl_vis = "//visibility:private"
364+
py_library_label = PY_LIBRARY_PUBLIC_LABEL
365+
whl_file_label = WHEEL_FILE_PUBLIC_LABEL
366+
impl_vis = ["//visibility:public"]
364367

365368
contents = "\n".join(
366369
[
367370
_BUILD_TEMPLATE.format(
368371
loads = "\n".join(loads),
369-
py_library_public_label = PY_LIBRARY_PUBLIC_LABEL,
370-
py_library_impl_label = PY_LIBRARY_IMPL_LABEL,
371-
py_library_actual_label = library_impl_label,
372+
py_library_label = py_library_label,
372373
dependencies = render.indent(lib_dependencies, " " * 4).lstrip(),
373374
whl_file_deps = render.indent(whl_file_deps, " " * 4).lstrip(),
374375
data_exclude = repr(_data_exclude),
375376
whl_name = whl_name,
376-
whl_file_public_label = WHEEL_FILE_PUBLIC_LABEL,
377-
whl_file_impl_label = WHEEL_FILE_IMPL_LABEL,
378-
whl_file_actual_label = whl_impl_label,
377+
whl_file_label = whl_file_label,
379378
tags = repr(tags),
380379
data_label = DATA_LABEL,
381380
dist_info_label = DIST_INFO_LABEL,
382381
entry_point_prefix = WHEEL_ENTRY_POINT_PREFIX,
383382
srcs_exclude = repr(srcs_exclude),
384383
data = repr(data),
385-
impl_vis = repr([impl_vis]),
384+
impl_vis = repr(impl_vis),
386385
),
387386
] + additional_content,
388387
)

python/private/bzlmod/pip.bzl

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,27 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides):
152152
for mod, whl_name in pip_attr.whl_modifications.items():
153153
whl_modifications[whl_name] = mod
154154

155-
requirement_cycles = {
156-
name: [normalize_name(whl_name) for whl_name in whls]
157-
for name, whls in pip_attr.experimental_requirement_cycles.items()
158-
}
155+
if pip_attr.experimental_requirement_cycles:
156+
requirement_cycles = {
157+
name: [normalize_name(whl_name) for whl_name in whls]
158+
for name, whls in pip_attr.experimental_requirement_cycles.items()
159+
}
159160

160-
whl_group_mapping = {
161-
whl_name: group_name
162-
for group_name, group_whls in requirement_cycles.items()
163-
for whl_name in group_whls
164-
}
161+
whl_group_mapping = {
162+
whl_name: group_name
163+
for group_name, group_whls in requirement_cycles.items()
164+
for whl_name in group_whls
165+
}
165166

166-
group_repo = "%s__groups" % (pip_name,)
167-
group_library(
168-
name = group_repo,
169-
repo_prefix = pip_name + "_",
170-
groups = pip_attr.experimental_requirement_cycles,
171-
)
167+
group_repo = "%s__groups" % (pip_name,)
168+
group_library(
169+
name = group_repo,
170+
repo_prefix = pip_name + "_",
171+
groups = pip_attr.experimental_requirement_cycles,
172+
)
173+
else:
174+
whl_group_mapping = {}
175+
requirement_cycles = {}
172176

173177
# Create a new wheel library for each of the different whls
174178
for whl_name, requirement_line in requirements:
@@ -177,6 +181,7 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides):
177181
# to.
178182
annotation = whl_modifications.get(whl_name)
179183
whl_name = normalize_name(whl_name)
184+
180185
group_name = whl_group_mapping.get(whl_name)
181186
group_deps = requirement_cycles.get(group_name, [])
182187

tests/pip_install/whl_library/generate_build_bazel_tests.bzl

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ filegroup(
3737
)
3838
3939
filegroup(
40-
name = "_whl",
40+
name = "whl",
4141
srcs = ["foo.whl"],
4242
data = [
4343
"@pypi_bar_baz//:whl",
4444
"@pypi_foo//:whl",
4545
],
46-
visibility = ["//visibility:private"],
46+
visibility = ["//visibility:public"],
4747
)
4848
4949
py_library(
50-
name = "_pkg",
50+
name = "pkg",
5151
srcs = glob(
5252
["site-packages/**/*.py"],
5353
exclude=[],
@@ -67,17 +67,7 @@ py_library(
6767
"@pypi_foo//:pkg",
6868
],
6969
tags = ["tag1", "tag2"],
70-
visibility = ["//visibility:private"],
71-
)
72-
73-
alias(
74-
name = "pkg",
75-
actual = "_pkg",
76-
)
77-
78-
alias(
79-
name = "whl",
80-
actual = "_whl",
70+
visibility = ["//visibility:public"],
8171
)
8272
"""
8373
actual = generate_whl_library_build_bazel(
@@ -113,7 +103,7 @@ filegroup(
113103
)
114104
115105
filegroup(
116-
name = "_whl",
106+
name = "whl",
117107
srcs = ["foo.whl"],
118108
data = [
119109
"@pypi_bar_baz//:whl",
@@ -130,11 +120,11 @@ filegroup(
130120
"//conditions:default": [],
131121
},
132122
),
133-
visibility = ["//visibility:private"],
123+
visibility = ["//visibility:public"],
134124
)
135125
136126
py_library(
137-
name = "_pkg",
127+
name = "pkg",
138128
srcs = glob(
139129
["site-packages/**/*.py"],
140130
exclude=[],
@@ -165,17 +155,7 @@ py_library(
165155
},
166156
),
167157
tags = ["tag1", "tag2"],
168-
visibility = ["//visibility:private"],
169-
)
170-
171-
alias(
172-
name = "pkg",
173-
actual = "_pkg",
174-
)
175-
176-
alias(
177-
name = "whl",
178-
actual = "_whl",
158+
visibility = ["//visibility:public"],
179159
)
180160
181161
config_setting(
@@ -275,17 +255,17 @@ filegroup(
275255
)
276256
277257
filegroup(
278-
name = "_whl",
258+
name = "whl",
279259
srcs = ["foo.whl"],
280260
data = [
281261
"@pypi_bar_baz//:whl",
282262
"@pypi_foo//:whl",
283263
],
284-
visibility = ["//visibility:private"],
264+
visibility = ["//visibility:public"],
285265
)
286266
287267
py_library(
288-
name = "_pkg",
268+
name = "pkg",
289269
srcs = glob(
290270
["site-packages/**/*.py"],
291271
exclude=["srcs_exclude_all"],
@@ -305,17 +285,7 @@ py_library(
305285
"@pypi_foo//:pkg",
306286
],
307287
tags = ["tag1", "tag2"],
308-
visibility = ["//visibility:private"],
309-
)
310-
311-
alias(
312-
name = "pkg",
313-
actual = "_pkg",
314-
)
315-
316-
alias(
317-
name = "whl",
318-
actual = "_whl",
288+
visibility = ["//visibility:public"],
319289
)
320290
321291
copy_file(
@@ -373,17 +343,17 @@ filegroup(
373343
)
374344
375345
filegroup(
376-
name = "_whl",
346+
name = "whl",
377347
srcs = ["foo.whl"],
378348
data = [
379349
"@pypi_bar_baz//:whl",
380350
"@pypi_foo//:whl",
381351
],
382-
visibility = ["//visibility:private"],
352+
visibility = ["//visibility:public"],
383353
)
384354
385355
py_library(
386-
name = "_pkg",
356+
name = "pkg",
387357
srcs = glob(
388358
["site-packages/**/*.py"],
389359
exclude=[],
@@ -403,17 +373,7 @@ py_library(
403373
"@pypi_foo//:pkg",
404374
],
405375
tags = ["tag1", "tag2"],
406-
visibility = ["//visibility:private"],
407-
)
408-
409-
alias(
410-
name = "pkg",
411-
actual = "_pkg",
412-
)
413-
414-
alias(
415-
name = "whl",
416-
actual = "_whl",
376+
visibility = ["//visibility:public"],
417377
)
418378
419379
py_binary(
@@ -502,16 +462,6 @@ py_library(
502462
visibility = ["@pypi__groups//:__pkg__"],
503463
)
504464
505-
alias(
506-
name = "pkg",
507-
actual = "@pypi__groups//:qux_pkg",
508-
)
509-
510-
alias(
511-
name = "whl",
512-
actual = "@pypi__groups//:qux_whl",
513-
)
514-
515465
config_setting(
516466
name = "is_linux_x86_64",
517467
constraint_values = [
@@ -520,6 +470,16 @@ config_setting(
520470
],
521471
visibility = ["//visibility:private"],
522472
)
473+
474+
alias(
475+
name = "pkg",
476+
actual = "@pypi__groups//:qux_pkg",
477+
)
478+
479+
alias(
480+
name = "whl",
481+
actual = "@pypi__groups//:qux_whl",
482+
)
523483
"""
524484
actual = generate_whl_library_build_bazel(
525485
repo_prefix = "pypi_",

0 commit comments

Comments
 (0)