Skip to content

Commit e5d2ff4

Browse files
committed
add a flag to use all requirements to make the extension lock file entries host independent
1 parent 7582d5b commit e5d2ff4

File tree

4 files changed

+68
-18
lines changed

4 files changed

+68
-18
lines changed

examples/bzlmod/MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ pip.parse(
218218
"host",
219219
],
220220
hub_name = "pip",
221+
# Parse all requirements files for the same lock file on all OSes, this will
222+
# become the default with 1.0 release
223+
parse_all_requirements_files = True,
221224
python_version = "3.10",
222225
# The requirements files for each platform that we want to support.
223226
requirements_by_platform = {

examples/bzlmod/MODULE.bazel.lock

Lines changed: 15 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/pypi/extension.bzl

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ load("//python/private:version_label.bzl", "version_label")
2424
load(":attrs.bzl", "use_isolated")
2525
load(":evaluate_markers.bzl", "evaluate_markers", EVALUATE_MARKERS_SRCS = "SRCS")
2626
load(":hub_repository.bzl", "hub_repository")
27-
load(":parse_requirements.bzl", "parse_requirements")
27+
load(":parse_requirements.bzl", "host_platform", "parse_requirements", "select_requirement")
2828
load(":parse_whl_name.bzl", "parse_whl_name")
2929
load(":pip_repository_attrs.bzl", "ATTRS")
3030
load(":render_pkg_aliases.bzl", "whl_alias")
@@ -212,6 +212,7 @@ def _create_whl_repos(
212212
logger = logger,
213213
)
214214

215+
repository_platform = host_platform(module_ctx)
215216
for whl_name, requirements in requirements_by_platform.items():
216217
# We are not using the "sanitized name" because the user
217218
# would need to guess what name we modified the whl name
@@ -318,6 +319,38 @@ def _create_whl_repos(
318319
exposed_packages[whl_name] = None
319320
continue
320321

322+
if not pip_attr.parse_all_requirements_files:
323+
requirement = select_requirement(
324+
requirements,
325+
platform = None if pip_attr.download_only else repository_platform,
326+
)
327+
if not requirement:
328+
# Sometimes the package is not present for host platform if there
329+
# are whls specified only in particular requirements files, in that
330+
# case just continue, however, if the download_only flag is set up,
331+
# then the user can also specify the target platform of the wheel
332+
# packages they want to download, in that case there will be always
333+
# a requirement here, so we will not be in this code branch.
334+
continue
335+
elif get_index_urls:
336+
logger.warn(lambda: "falling back to pip for installing the right file for {}".format(requirement.requirement_line))
337+
338+
whl_library_args["requirement"] = requirement.requirement_line
339+
if requirement.extra_pip_args:
340+
whl_library_args["extra_pip_args"] = requirement.extra_pip_args
341+
342+
# We sort so that the lock-file remains the same no matter the order of how the
343+
# args are manipulated in the code going before.
344+
repo_name = "{}_{}".format(pip_name, whl_name)
345+
whl_libraries[repo_name] = dict(whl_library_args.items())
346+
whl_map.setdefault(whl_name, []).append(
347+
whl_alias(
348+
repo = repo_name,
349+
version = major_minor,
350+
),
351+
)
352+
continue
353+
321354
for requirement in requirements:
322355
is_exposed = is_exposed or requirement.is_exposed
323356
if get_index_urls:
@@ -730,6 +763,20 @@ find in case extra indexes are specified.
730763
""",
731764
default = True,
732765
),
766+
"parse_all_requirements_files": attr.bool(
767+
default = False,
768+
doc = """\
769+
A temporary flag to enable users to make `pip` extension result always
770+
the same independent of the whether transitive dependencies use {bzl:attr}`experimental_index_url` or not.
771+
772+
This enables users to migrate to a solution that fixes
773+
[#2268](https://github.com/bazelbuild/rules_python/issues/2268).
774+
775+
::::{deprecated} 0.38.0
776+
This is a transition flag and will be removed in a subsequent release.
777+
::::
778+
""",
779+
),
733780
"python_version": attr.string(
734781
mandatory = True,
735782
doc = """

tests/pypi/extension/extension_tests.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def _parse(
8484
extra_pip_args = [],
8585
isolated = True,
8686
netrc = None,
87+
parse_all_requirements_files = True,
8788
pip_data_exclude = None,
8889
python_interpreter = None,
8990
python_interpreter_target = None,
@@ -110,6 +111,7 @@ def _parse(
110111
hub_name = hub_name,
111112
isolated = isolated,
112113
netrc = netrc,
114+
parse_all_requirements_files = parse_all_requirements_files,
113115
pip_data_exclude = pip_data_exclude,
114116
python_interpreter = python_interpreter,
115117
python_interpreter_target = python_interpreter_target,

0 commit comments

Comments
 (0)