Skip to content

Commit 1d1efe9

Browse files
authored
fix(bzlmod): correctly template repository macros for requirements, etc (bazel-contrib#1190)
It seems that the macros for specifying the requirements break when the user starts using `incompatible_generate_aliases=True`. This PR fixes this. Testing done: 1. Modify the example: ``` $ git diff diff --git a/examples/bzlmod/MODULE.bazel b/examples/bzlmod/MODULE.bazel index ce91228..1750210 100644 --- a/examples/bzlmod/MODULE.bazel +++ b/examples/bzlmod/MODULE.bazel @@ -26,6 +26,7 @@ register_toolchains( pip = use_extension("@rules_python//python:extensions.bzl", "pip") pip.parse( name = "pip", + incompatible_generate_aliases=True, requirements_lock = "//:requirements_lock.txt", requirements_windows = "//:requirements_windows.txt", ) ``` 2. Run `bazel build ...` and check that it is still working. I noticed this when working on bazel-contrib#1189 and creating a separate PR for easier cherry-picking if we wanted to make a patch release which includes this. I am not sure how I could make an automated test for this other than creating a separate example.
1 parent ce74914 commit 1d1efe9

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

python/pip_install/pip_repository.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,15 @@ def _pip_repository_bzlmod_impl(rctx):
367367
else:
368368
build_contents += _bzlmod_pkg_aliases(repo_name, bzl_packages)
369369

370+
# NOTE: we are using the canonical name with the double '@' in order to
371+
# always uniquely identify a repository, as the labels are being passed as
372+
# a string and the resolution of the label happens at the call-site of the
373+
# `requirement`, et al. macros.
374+
if rctx.attr.incompatible_generate_aliases:
375+
macro_tmpl = "@@{name}//{{}}:{{}}".format(name = rctx.attr.name)
376+
else:
377+
macro_tmpl = "@@{name}//:{{}}_{{}}".format(name = rctx.attr.name)
378+
370379
rctx.file("BUILD.bazel", build_contents)
371380
rctx.template("requirements.bzl", rctx.attr._template, substitutions = {
372381
"%%ALL_REQUIREMENTS%%": _format_repr_list([
@@ -377,7 +386,7 @@ def _pip_repository_bzlmod_impl(rctx):
377386
"@{}//{}:whl".format(repo_name, p) if rctx.attr.incompatible_generate_aliases else "@{}_{}//:whl".format(rctx.attr.name, p)
378387
for p in bzl_packages
379388
]),
380-
"%%NAME%%": rctx.attr.name,
389+
"%%MACRO_TMPL%%": macro_tmpl,
381390
"%%REQUIREMENTS_LOCK%%": str(requirements_txt),
382391
})
383392

python/pip_install/pip_repository_requirements_bzlmod.bzl.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ def _clean_name(name):
1212
return name.replace("-", "_").replace(".", "_").lower()
1313

1414
def requirement(name):
15-
return "@@%%NAME%%//:" + _clean_name(name) + "_pkg"
15+
return "%%MACRO_TMPL%%".format(_clean_name(name), "pkg")
1616

1717
def whl_requirement(name):
18-
return "@@%%NAME%%//:" + _clean_name(name) + "_whl"
18+
return "%%MACRO_TMPL%%".format(_clean_name(name), "whl")
1919

2020
def data_requirement(name):
21-
return "@@%%NAME%%//:" + _clean_name(name) + "_data"
21+
return "%%MACRO_TMPL%%".format(_clean_name(name), "data")
2222

2323
def dist_info_requirement(name):
24-
return "@@%%NAME%%//:" + _clean_name(name) + "_dist_info"
24+
return "%%MACRO_TMPL%%".format(_clean_name(name), "dist_info")

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