Skip to content

fix: Using canonical name in requirements.bzl #1176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 17, 2023
6 changes: 4 additions & 2 deletions docs/pip_repository.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion examples/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@pip//:requirements.bzl", "requirement")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@pip//:requirements.bzl", "all_requirements", "all_whl_requirements", "requirement")
load("@python3_9//:defs.bzl", py_test_with_transition = "py_test")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
Expand Down Expand Up @@ -43,3 +44,13 @@ py_test_with_transition(
main = "test.py",
deps = [":lib"],
)

build_test(
name = "all_wheels",
targets = all_whl_requirements,
)

build_test(
name = "all_requirements",
targets = all_requirements,
)
3 changes: 3 additions & 0 deletions examples/bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_skylib", version = "1.4.1")
bazel_dep(name = "rules_python", version = "0.0.0")
local_path_override(
module_name = "rules_python",
Expand All @@ -26,6 +27,8 @@ register_toolchains(
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
name = "pip",
# Intentionally set it false because the "true" case is already covered by examples/bzlmod_build_file_generation
incompatible_generate_aliases = False,
requirements_lock = "//:requirements_lock.txt",
requirements_windows = "//:requirements_windows.txt",
)
Expand Down
1 change: 1 addition & 0 deletions python/extensions/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def _pip_impl(module_ctx):
# this does not create the install_deps() macro.
pip_repository_bzlmod(
name = attr.name,
repo_name = attr.name,
requirements_lock = attr.requirements_lock,
incompatible_generate_aliases = attr.incompatible_generate_aliases,
)
Expand Down
10 changes: 7 additions & 3 deletions python/pip_install/pip_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def _pip_repository_bzlmod_impl(rctx):

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

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

build_contents = _BUILD_FILE_CONTENTS

Expand All @@ -379,11 +379,11 @@ def _pip_repository_bzlmod_impl(rctx):
rctx.file("BUILD.bazel", build_contents)
rctx.template("requirements.bzl", rctx.attr._template, substitutions = {
"%%ALL_REQUIREMENTS%%": _format_repr_list([
"@{}//{}".format(repo_name, p) if rctx.attr.incompatible_generate_aliases else "@{}_{}//:pkg".format(rctx.attr.name, p)
macro_tmpl.format(p, p) if rctx.attr.incompatible_generate_aliases else macro_tmpl.format(p, "pkg")
for p in bzl_packages
]),
"%%ALL_WHL_REQUIREMENTS%%": _format_repr_list([
"@{}//{}:whl".format(repo_name, p) if rctx.attr.incompatible_generate_aliases else "@{}_{}//:whl".format(rctx.attr.name, p)
macro_tmpl.format(p, "whl")
for p in bzl_packages
]),
"%%MACRO_TMPL%%": macro_tmpl,
Expand All @@ -395,6 +395,10 @@ pip_repository_bzlmod_attrs = {
default = False,
doc = "Allow generating aliases in '@pip//:<pkg>' -> '@pip_<pkg>//:pkg'. This replaces the aliases generated by the `bzlmod` tooling.",
),
"repo_name": attr.string(
mandatory = True,
doc = "The apparent name of the repo. This is needed because in bzlmod, the name attribute becomes the canonical name",
),
"requirements_darwin": attr.label(
allow_single_file = True,
doc = "Override the requirements_lock attribute when the host platform is Mac OS",
Expand Down