Skip to content

Commit 1383bd4

Browse files
authored
fix: Using canonical name in requirements.bzl (bazel-contrib#1176)
When `incompatible_generate_aliases = False`, `pip.parse` doesn't work in bzlmod, because requirements.bzl has: ``` all_requirements = ["@rules_python~0.21.0~pip~pip_yapf//:pkg"] all_whl_requirements = ["@rules_python~0.21.0~pip~pip_yapf//:whl"] ``` Starting Bazel 6, canonical names should be referred with double "@". The reason why `incompatible_generate_aliases = True` worked is because it uses apparent name by parsing the canonical label with `repo_name = rctx.attr.name.split("~")[-1]`. This is dangerous because Bazel may change its canonical name pattern in future versions. This PR adds a new attribute "repo_name" to `pip_repository_bzlmod`, so we have access to the human-readable name in the implementation.
1 parent 28bc03c commit 1383bd4

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

docs/pip_repository.md

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/bzlmod/BUILD.bazel

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
load("@pip//:requirements.bzl", "requirement")
1+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
2+
load("@pip//:requirements.bzl", "all_requirements", "all_whl_requirements", "requirement")
23
load("@python3_9//:defs.bzl", py_test_with_transition = "py_test")
34
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
45
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
@@ -43,3 +44,13 @@ py_test_with_transition(
4344
main = "test.py",
4445
deps = [":lib"],
4546
)
47+
48+
build_test(
49+
name = "all_wheels",
50+
targets = all_whl_requirements,
51+
)
52+
53+
build_test(
54+
name = "all_requirements",
55+
targets = all_requirements,
56+
)

examples/bzlmod/MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module(
44
compatibility_level = 1,
55
)
66

7+
bazel_dep(name = "bazel_skylib", version = "1.4.1")
78
bazel_dep(name = "rules_python", version = "0.0.0")
89
local_path_override(
910
module_name = "rules_python",
@@ -26,6 +27,8 @@ register_toolchains(
2627
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
2728
pip.parse(
2829
name = "pip",
30+
# Intentionally set it false because the "true" case is already covered by examples/bzlmod_build_file_generation
31+
incompatible_generate_aliases = False,
2932
requirements_lock = "//:requirements_lock.txt",
3033
requirements_windows = "//:requirements_windows.txt",
3134
)

python/extensions/pip.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def _pip_impl(module_ctx):
3434
# this does not create the install_deps() macro.
3535
pip_repository_bzlmod(
3636
name = attr.name,
37+
repo_name = attr.name,
3738
requirements_lock = attr.requirements_lock,
3839
incompatible_generate_aliases = attr.incompatible_generate_aliases,
3940
)

python/pip_install/pip_repository.bzl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def _pip_repository_bzlmod_impl(rctx):
358358

359359
bzl_packages = sorted([name for name, _ in packages])
360360

361-
repo_name = rctx.attr.name.split("~")[-1]
361+
repo_name = rctx.attr.repo_name
362362

363363
build_contents = _BUILD_FILE_CONTENTS
364364

@@ -379,11 +379,11 @@ def _pip_repository_bzlmod_impl(rctx):
379379
rctx.file("BUILD.bazel", build_contents)
380380
rctx.template("requirements.bzl", rctx.attr._template, substitutions = {
381381
"%%ALL_REQUIREMENTS%%": _format_repr_list([
382-
"@{}//{}".format(repo_name, p) if rctx.attr.incompatible_generate_aliases else "@{}_{}//:pkg".format(rctx.attr.name, p)
382+
macro_tmpl.format(p, p) if rctx.attr.incompatible_generate_aliases else macro_tmpl.format(p, "pkg")
383383
for p in bzl_packages
384384
]),
385385
"%%ALL_WHL_REQUIREMENTS%%": _format_repr_list([
386-
"@{}//{}:whl".format(repo_name, p) if rctx.attr.incompatible_generate_aliases else "@{}_{}//:whl".format(rctx.attr.name, p)
386+
macro_tmpl.format(p, "whl")
387387
for p in bzl_packages
388388
]),
389389
"%%MACRO_TMPL%%": macro_tmpl,
@@ -395,6 +395,10 @@ pip_repository_bzlmod_attrs = {
395395
default = False,
396396
doc = "Allow generating aliases in '@pip//:<pkg>' -> '@pip_<pkg>//:pkg'. This replaces the aliases generated by the `bzlmod` tooling.",
397397
),
398+
"repo_name": attr.string(
399+
mandatory = True,
400+
doc = "The apparent name of the repo. This is needed because in bzlmod, the name attribute becomes the canonical name",
401+
),
398402
"requirements_darwin": attr.label(
399403
allow_single_file = True,
400404
doc = "Override the requirements_lock attribute when the host platform is Mac OS",

0 commit comments

Comments
 (0)