Skip to content

fix(pypi): correctly aggregate the requirements files #2932

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions python/private/pypi/parse_requirements.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _package_srcs(
env_marker_target_platforms,
extract_url_srcs):
"""A function to return sources for a particular package."""
srcs = []
srcs = {}
for r in sorted(reqs.values(), key = lambda r: r.requirement_line):
whls, sdist = _add_dists(
requirement = r,
Expand All @@ -249,21 +249,31 @@ def _package_srcs(
)]
req_line = r.srcs.requirement_line

extra_pip_args = tuple(r.extra_pip_args)
for dist in all_dists:
srcs.append(
key = (
dist.filename,
req_line,
extra_pip_args,
)
entry = srcs.setdefault(
key,
struct(
distribution = name,
extra_pip_args = r.extra_pip_args,
requirement_line = req_line,
target_platforms = target_platforms,
target_platforms = [],
filename = dist.filename,
sha256 = dist.sha256,
url = dist.url,
yanked = dist.yanked,
),
)
for p in target_platforms:
if p not in entry.target_platforms:
entry.target_platforms.append(p)

return srcs
return srcs.values()

def select_requirement(requirements, *, platform):
"""A simple function to get a requirement for a particular platform.
Expand Down
64 changes: 63 additions & 1 deletion tests/pypi/parse_requirements/parse_requirements_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ foo==0.0.4 @ https://example.org/foo-0.0.4.whl
foo==0.0.5 @ https://example.org/foo-0.0.5.whl --hash=sha256:deadbeef
""",
"requirements_osx": """\
foo==0.0.3 --hash=sha256:deadbaaf
foo==0.0.3 --hash=sha256:deadbaaf --hash=sha256:deadb11f
""",
"requirements_osx_download_only": """\
--platform=macosx_10_9_arm64
Expand Down Expand Up @@ -515,6 +515,68 @@ def _test_git_sources(env):

_tests.append(_test_git_sources)

def _test_overlapping_shas_with_index_results(env):
got = parse_requirements(
ctx = _mock_ctx(),
requirements_by_platform = {
"requirements_linux": ["cp39_linux_x86_64"],
"requirements_osx": ["cp39_osx_x86_64"],
},
get_index_urls = lambda _, __: {
"foo": struct(
sdists = {
},
whls = {
"deadb11f": struct(
url = "super2",
sha256 = "deadb11f",
filename = "foo-0.0.1-py3-none-macosx_14_0_x86_64.whl",
yanked = False,
),
"deadbaaf": struct(
url = "super2",
sha256 = "deadbaaf",
filename = "foo-0.0.1-py3-none-any.whl",
yanked = False,
),
},
),
},
)

env.expect.that_collection(got).contains_exactly([
struct(
name = "foo",
is_exposed = True,
# TODO @aignas 2025-05-25: how do we rename this?
is_multiple_versions = True,
srcs = [
struct(
distribution = "foo",
extra_pip_args = [],
filename = "foo-0.0.1-py3-none-any.whl",
requirement_line = "foo==0.0.3",
sha256 = "deadbaaf",
target_platforms = ["cp39_linux_x86_64", "cp39_osx_x86_64"],
url = "super2",
yanked = False,
),
struct(
distribution = "foo",
extra_pip_args = [],
filename = "foo-0.0.1-py3-none-macosx_14_0_x86_64.whl",
requirement_line = "foo==0.0.3",
sha256 = "deadb11f",
target_platforms = ["cp39_osx_x86_64"],
url = "super2",
yanked = False,
),
],
),
])

_tests.append(_test_overlapping_shas_with_index_results)

def parse_requirements_test_suite(name):
"""Create the test suite.

Expand Down