diff options
author | Ignas Anikevicius <anikevicius@gmail.com> | 2023-05-02 02:34:57 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-01 10:34:57 -0700 |
commit | 1d1efe906b4c5bf215f807844ba9b53869fa53e6 (patch) | |
tree | b1a0811f0242a8e7edfd0cc0618350ca8afc4987 | |
parent | ce749147a9a4f8fc8ef6141bf34ab538ed7f7476 (diff) | |
download | bazelbuild-rules_python-1d1efe906b4c5bf215f807844ba9b53869fa53e6.tar.gz |
fix(bzlmod): correctly template repository macros for requirements, etc (#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 #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.
-rw-r--r-- | python/pip_install/pip_repository.bzl | 11 | ||||
-rw-r--r-- | python/pip_install/pip_repository_requirements_bzlmod.bzl.tmpl | 8 |
2 files changed, 14 insertions, 5 deletions
diff --git a/python/pip_install/pip_repository.bzl b/python/pip_install/pip_repository.bzl index fce0dcd..1ea7bca 100644 --- a/python/pip_install/pip_repository.bzl +++ b/python/pip_install/pip_repository.bzl @@ -367,6 +367,15 @@ def _pip_repository_bzlmod_impl(rctx): else: build_contents += _bzlmod_pkg_aliases(repo_name, bzl_packages) + # NOTE: we are using the canonical name with the double '@' in order to + # always uniquely identify a repository, as the labels are being passed as + # a string and the resolution of the label happens at the call-site of the + # `requirement`, et al. macros. + if rctx.attr.incompatible_generate_aliases: + macro_tmpl = "@@{name}//{{}}:{{}}".format(name = rctx.attr.name) + else: + macro_tmpl = "@@{name}//:{{}}_{{}}".format(name = rctx.attr.name) + rctx.file("BUILD.bazel", build_contents) rctx.template("requirements.bzl", rctx.attr._template, substitutions = { "%%ALL_REQUIREMENTS%%": _format_repr_list([ @@ -377,7 +386,7 @@ def _pip_repository_bzlmod_impl(rctx): "@{}//{}:whl".format(repo_name, p) if rctx.attr.incompatible_generate_aliases else "@{}_{}//:whl".format(rctx.attr.name, p) for p in bzl_packages ]), - "%%NAME%%": rctx.attr.name, + "%%MACRO_TMPL%%": macro_tmpl, "%%REQUIREMENTS_LOCK%%": str(requirements_txt), }) diff --git a/python/pip_install/pip_repository_requirements_bzlmod.bzl.tmpl b/python/pip_install/pip_repository_requirements_bzlmod.bzl.tmpl index 462829d..1b2e217 100644 --- a/python/pip_install/pip_repository_requirements_bzlmod.bzl.tmpl +++ b/python/pip_install/pip_repository_requirements_bzlmod.bzl.tmpl @@ -12,13 +12,13 @@ def _clean_name(name): return name.replace("-", "_").replace(".", "_").lower() def requirement(name): - return "@@%%NAME%%//:" + _clean_name(name) + "_pkg" + return "%%MACRO_TMPL%%".format(_clean_name(name), "pkg") def whl_requirement(name): - return "@@%%NAME%%//:" + _clean_name(name) + "_whl" + return "%%MACRO_TMPL%%".format(_clean_name(name), "whl") def data_requirement(name): - return "@@%%NAME%%//:" + _clean_name(name) + "_data" + return "%%MACRO_TMPL%%".format(_clean_name(name), "data") def dist_info_requirement(name): - return "@@%%NAME%%//:" + _clean_name(name) + "_dist_info" + return "%%MACRO_TMPL%%".format(_clean_name(name), "dist_info") |