Skip to content

Commit

Permalink
fix(whl_library): track sources in whl_library
Browse files Browse the repository at this point in the history
This is reusing a bit of code used in `evaluate_markers` and makes use
of the RECORD files in the `whl` files that we use to extract whls in
`whl_library`. This should be merged before bazelbuild#2514 to avoid any cache
invalidation issues downstream.

Fixes bazelbuild#2468
  • Loading branch information
aignas committed Dec 24, 2024
1 parent b5729b4 commit 7b0dc0d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ Unreleased changes template.
are now printing more details and include the currently active flag
values. Fixes [#2466](https://github.com/bazelbuild/rules_python/issues/2466).
* (py_proto_library) Fix import paths in Bazel 8.
* (whl_library) Now the changes to the dependencies are correctly tracked when
PyPI packages used in {bzl:obj}`whl_library` during the `repository_rule` phase
change. Fixes [#2468](https://github.com/bazelbuild/rules_python/issues/2468).

[pep-695]: https://peps.python.org/pep-0695/

Expand Down
7 changes: 7 additions & 0 deletions python/private/pypi/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ py_library(

# Collate all the repository names so they can be easily consumed
all_repo_names = [name for (name, _, _) in _RULE_DEPS]
record_files = {
name: Label("@{}//:{}.dist-info/RECORD".format(
name,
url.rpartition("/")[-1].partition("-py3-none")[0],
))
for (name, url, _) in _RULE_DEPS
}

def pypi_deps():
"""
Expand Down
3 changes: 2 additions & 1 deletion python/private/pypi/evaluate_markers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

"""A simple function that evaluates markers using a python interpreter."""

load(":deps.bzl", "record_files")
load(":pypi_repo_utils.bzl", "pypi_repo_utils")

# Used as a default value in a rule to ensure we fetch the dependencies.
SRCS = [
# When the version, or any of the files in `packaging` package changes,
# this file will change as well.
Label("@pypi__packaging//:packaging-24.0.dist-info/RECORD"),
record_files["pypi__packaging"],
Label("//python/private/pypi/requirements_parser:resolve_target_platforms.py"),
Label("//python/private/pypi/whl_installer:platform.py"),
]
Expand Down
18 changes: 15 additions & 3 deletions python/private/pypi/whl_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ load("//python/private:envsubst.bzl", "envsubst")
load("//python/private:is_standalone_interpreter.bzl", "is_standalone_interpreter")
load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
load(":attrs.bzl", "ATTRS", "use_isolated")
load(":deps.bzl", "all_repo_names")
load(":deps.bzl", "all_repo_names", "record_files")
load(":generate_whl_library_build_bazel.bzl", "generate_whl_library_build_bazel")
load(":parse_whl_name.bzl", "parse_whl_name")
load(":patch_whl.bzl", "patch_whl")
Expand Down Expand Up @@ -242,14 +242,15 @@ def _whl_library_impl(rctx):
else:
op_tmpl = "whl_library.ResolveRequirement({name}, {requirement})"

repo_utils.execute_checked(
pypi_repo_utils.execute_checked(
rctx,
# truncate the requirement value when logging it / reporting
# progress since it may contain several ' --hash=sha256:...
# --hash=sha256:...' substrings that fill up the console
op = op_tmpl.format(name = rctx.attr.name, requirement = rctx.attr.requirement.split(" ", 1)[0]),
arguments = args,
environment = environment,
srcs = rctx.attr._python_srcs,
quiet = rctx.attr.quiet,
timeout = rctx.attr.timeout,
logger = logger,
Expand Down Expand Up @@ -291,13 +292,14 @@ def _whl_library_impl(rctx):
)
]

repo_utils.execute_checked(
pypi_repo_utils.execute_checked(
rctx,
op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path),
arguments = args + [
"--whl-file",
whl_path,
] + ["--platform={}".format(p) for p in target_platforms],
srcs = rctx.attr._python_srcs,
environment = environment,
quiet = rctx.attr.quiet,
timeout = rctx.attr.timeout,
Expand Down Expand Up @@ -450,6 +452,16 @@ attr makes `extra_pip_args` and `download_only` ignored.""",
for repo in all_repo_names
],
),
"_python_srcs": attr.label_list(
# Used as a default value in a rule to ensure we fetch the dependencies.
default = [
Label("//python/private/pypi/whl_installer:platform.py"),
Label("//python/private/pypi/whl_installer:wheel.py"),
Label("//python/private/pypi/whl_installer:wheel_installer.py"),
Label("//python/private/pypi/whl_installer:arguments.py"),
Label("//python/private/pypi/whl_installer:namespace_pkgs.py"),
] + record_files.values(),
),
"_rule_name": attr.string(default = "whl_library"),
}, **ATTRS)
whl_library_attrs.update(AUTH_ATTRS)
Expand Down

0 comments on commit 7b0dc0d

Please sign in to comment.