Skip to content
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

refactor(whl_library): split out a whl_archive library #2214

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
927 changes: 271 additions & 656 deletions examples/bzlmod/MODULE.bazel.lock

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions python/private/pypi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ bzl_library(
name = "deps_bzl",
srcs = ["deps.bzl"],
deps = [
":whl_archive_bzl",
"//python/private:bazel_tools_bzl",
],
)
Expand Down Expand Up @@ -214,7 +215,6 @@ bzl_library(
name = "pip_compile_bzl",
srcs = ["pip_compile.bzl"],
deps = [
":deps_bzl",
"//python:defs_bzl",
],
)
Expand Down Expand Up @@ -245,7 +245,6 @@ bzl_library(
srcs = ["pypi_repo_utils.bzl"],
deps = [
"//python/private:repo_utils_bzl",
"@bazel_skylib//lib:types",
],
)

Expand Down Expand Up @@ -282,6 +281,21 @@ bzl_library(
],
)

bzl_library(
name = "whl_archive_bzl",
srcs = ["whl_archive.bzl"],
deps = [
":attrs_bzl",
":generate_whl_library_build_bazel_bzl",
":parse_whl_name_bzl",
":patch_whl_bzl",
":pypi_repo_utils_bzl",
":whl_target_platforms_bzl",
"//python/private:auth_bzl",
"//python/private:repo_utils_bzl",
],
)

bzl_library(
name = "whl_library_alias_bzl",
srcs = ["whl_library_alias.bzl"],
Expand All @@ -297,14 +311,11 @@ bzl_library(
deps = [
":attrs_bzl",
":deps_bzl",
":generate_whl_library_build_bazel_bzl",
":parse_whl_name_bzl",
":patch_whl_bzl",
":pypi_repo_utils_bzl",
":whl_target_platforms_bzl",
"//python:repositories_bzl",
":whl_archive_bzl",
"//python/private:auth_bzl",
"//python/private:envsubst_bzl",
"//python/private:python_repositories_bzl",
"//python/private:repo_utils_bzl",
],
)
Expand Down
13 changes: 13 additions & 0 deletions python/private/pypi/dependency_resolver/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
load("//python:py_library.bzl", "py_library")

exports_files(["dependency_resolver.py"])

filegroup(
name = "distribution",
srcs = glob(["**"]),
visibility = ["//python/private/pypi:__subpackages__"],
)

py_library(
name = "dependency_resolver",
srcs = ["dependency_resolver.py"],
visibility = ["//visibility:public"],
deps = [
"//python/runfiles",
"@pypi__click//:pkg",
"@pypi__pip_tools//:pkg",
],
)
40 changes: 31 additions & 9 deletions python/private/pypi/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load(":whl_archive.bzl", "whl_archive")

_RULE_DEPS = [
# START: maintained by 'bazel run //tools/private/update_deps:update_pip_deps'
Expand Down Expand Up @@ -103,7 +104,7 @@ package(default_visibility = ["//visibility:public"])
load("@rules_python//python:defs.bzl", "py_library")

py_library(
name = "lib",
name = "pkg",
srcs = glob(["**/*.py"]),
data = glob(["**/*"], exclude=[
# These entries include those put into user-installed dependencies by
Expand All @@ -130,11 +131,32 @@ def pypi_deps():
Fetch dependencies these rules depend on. Workspaces that use the pip_parse rule can call this.
"""
for (name, url, sha256) in _RULE_DEPS:
maybe(
http_archive,
name,
url = url,
sha256 = sha256,
type = "zip",
build_file_content = _GENERIC_WHEEL,
)
if name in ["pypi__installer", "pypi__packaging"]:
maybe(
http_archive,
name,
url = url,
sha256 = sha256,
type = "zip",
build_file_content = _GENERIC_WHEEL,
)
else:
maybe(
whl_archive,
name,
urls = [url],
sha256 = sha256,
requirement = name.split("_")[-1],
repo = "pypi_",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be nice to dogfood the library, but we need to have a reference to the default (or some other) python interpreter in order to do this.

In bzlmod it is possible to do this, but in WORKSPACE it is not, so we may need to leave the deps.bzl as is.

experimental_target_platforms = [
"linux_x86_64",
"linux_aarch64",
"linux_i386",
"linux_s390x",
"linux_ppc",
"osx_x86_64",
"osx_aarch64",
"windows_x86_64",
],
dep_template = "@pypi__{name}//:{target}",
)
8 changes: 7 additions & 1 deletion python/private/pypi/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ load(":pip_repository_attrs.bzl", "ATTRS")
load(":render_pkg_aliases.bzl", "whl_alias")
load(":requirements_files_by_platform.bzl", "requirements_files_by_platform")
load(":simpleapi_download.bzl", "simpleapi_download")
load(":whl_archive.bzl", "whl_archive")
load(":whl_library.bzl", "whl_library")
load(":whl_repo_name.bzl", "whl_repo_name")

Expand Down Expand Up @@ -307,7 +308,12 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides, group_map, s
if len(requirements) > 1:
target_platforms = requirement.target_platforms

whl_library(name = repo_name, **dict(sorted(whl_library_args.items())))
if distribution.filename.endswith(".whl"):
# This is not used by the rule
whl_library_args.pop("envsubst", None)
whl_archive(name = repo_name, **dict(sorted(whl_library_args.items())))
else:
whl_library(name = repo_name, **dict(sorted(whl_library_args.items())))

whl_map[hub_name].setdefault(whl_name, []).append(
whl_alias(
Expand Down
22 changes: 5 additions & 17 deletions python/private/pypi/pip_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def pip_compile(

# Use the Label constructor so this is expanded in the context of the file
# where it appears, which is to say, in @rules_python
pip_compile = Label("//python/private/pypi/dependency_resolver:dependency_resolver.py")
pip_compile_lib = Label("//python/private/pypi/dependency_resolver")
pip_compile_main = Label("//python/private/pypi/dependency_resolver:dependency_resolver.py")

loc = "$(rlocationpath {})"

Expand All @@ -124,20 +125,7 @@ def pip_compile(
args.extend(extra_args)

deps = [
Label("@pypi__build//:lib"),
Label("@pypi__click//:lib"),
Label("@pypi__colorama//:lib"),
Label("@pypi__importlib_metadata//:lib"),
Label("@pypi__more_itertools//:lib"),
Label("@pypi__packaging//:lib"),
Label("@pypi__pep517//:lib"),
Label("@pypi__pip//:lib"),
Label("@pypi__pip_tools//:lib"),
Label("@pypi__pyproject_hooks//:lib"),
Label("@pypi__setuptools//:lib"),
Label("@pypi__tomli//:lib"),
Label("@pypi__zipp//:lib"),
Label("//python/runfiles:runfiles"),
pip_compile_lib,
] + extra_deps

tags = tags or []
Expand All @@ -148,8 +136,8 @@ def pip_compile(
"args": args,
"data": data,
"deps": deps,
"main": pip_compile,
"srcs": [pip_compile],
"main": pip_compile_main,
"srcs": [pip_compile_main],
"tags": tags,
"visibility": visibility,
}
Expand Down
6 changes: 4 additions & 2 deletions python/private/pypi/pypi_repo_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

""

load("@bazel_skylib//lib:types.bzl", "types")
load("//python/private:repo_utils.bzl", "repo_utils")

def _get_python_interpreter_attr(mrctx, *, python_interpreter = None):
Expand Down Expand Up @@ -127,7 +126,10 @@ def _execute_checked(mrctx, *, srcs, **kwargs):

env = kwargs.pop("environment", {})
pythonpath = env.get("PYTHONPATH", "")
if pythonpath and not types.is_string(pythonpath):

# NOTE @aignas 2024-09-11: Not using `bazel_skylib` `types` module to avoid
# a circular dependency in WORKSPACE
if pythonpath and not type(pythonpath) == type(""):
env["PYTHONPATH"] = _construct_pypath(mrctx, entries = pythonpath)

return repo_utils.execute_checked(
Expand Down
Loading