Skip to content

fix: sdist resulting in duplicate repo name when requirements line does not match #2658

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 15 commits into
base: main
Choose a base branch
from
31 changes: 30 additions & 1 deletion python/private/pypi/parse_requirements.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def parse_requirements(

# Return normalized names
ret_requirements = ret.setdefault(normalize_name(whl_name), [])
sdists_by_sha = {}

for r in sorted(reqs.values(), key = lambda r: r.requirement_line):
whls, sdist = _add_dists(
Expand All @@ -215,14 +216,42 @@ def parse_requirements(
)

Copy link
Collaborator

Choose a reason for hiding this comment

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

The idea of adding a common sdist feels like a hack. Maybe having a different algorithm/grouping would be better. Maybe having per sha grouping could be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Took a stab at changing it to use the sha instead.

target_platforms = env_marker_target_platforms.get(r.requirement_line, r.target_platforms)

if sdist:
sha = sdist.sha256
sdist_info = sdists_by_sha.setdefault(sha, struct(
distribution = r.distribution,
srcs = r.srcs,
extra_pip_args = r.extra_pip_args,
sdist = sdist,
platforms = [],
))
sdist_info.platforms.extend(target_platforms)

if len(whls) == 0:
continue

ret_requirements.append(
struct(
distribution = r.distribution,
srcs = r.srcs,
target_platforms = sorted(target_platforms),
extra_pip_args = r.extra_pip_args,
whls = whls,
sdist = sdist,
sdist = None,
is_exposed = is_exposed,
),
)

for sdist_info in sdists_by_sha.values():
ret_requirements.append(
struct(
distribution = sdist_info.distribution,
srcs = sdist_info.srcs,
target_platforms = sorted(sdist_info.platforms),
extra_pip_args = sdist_info.extra_pip_args,
whls = [],
sdist = sdist_info.sdist,
is_exposed = is_exposed,
),
)
Expand Down
4 changes: 2 additions & 2 deletions tests/pypi/extension/extension_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -701,15 +701,15 @@ pip_fallback==0.0.1
struct(
config_setting = None,
filename = "simple-0.0.1-py3-none-any.whl",
target_platforms = None,
target_platforms = ("cp315_linux_aarch64", "cp315_linux_arm", "cp315_linux_ppc", "cp315_linux_s390x", "cp315_linux_x86_64", "cp315_osx_aarch64", "cp315_osx_x86_64", "cp315_windows_x86_64"),
Copy link
Contributor Author

@chrisirhc chrisirhc Mar 11, 2025

Choose a reason for hiding this comment

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

I'm not sure why my changes introduced this change. My guess is that extension.bzl may have some handling for any.whl

version = "3.15",
),
],
"pypi_315_simple_sdist_deadbeef": [
struct(
config_setting = None,
filename = "simple-0.0.1.tar.gz",
target_platforms = None,
target_platforms = ("cp315_linux_aarch64", "cp315_linux_arm", "cp315_linux_ppc", "cp315_linux_s390x", "cp315_linux_x86_64", "cp315_osx_aarch64", "cp315_osx_x86_64", "cp315_windows_x86_64"),
version = "3.15",
),
],
Expand Down
119 changes: 119 additions & 0 deletions tests/pypi/parse_requirements/parse_requirements_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ foo==0.0.3 --hash=sha256:deadbaaf
"requirements_windows": """\
foo[extra]==0.0.2 --hash=sha256:deadbeef
bar==0.0.1 --hash=sha256:deadb00f
""",
"requirements_sdist_different_hashes": """\
foo==0.0.1 --hash=sha256:deadbeef --hash=sha256:cafebabe
""",
"requirements_sdist_different_hashes_linux": """\
foo==0.0.1 --hash=sha256:deadbaaf --hash=sha256:cafebabe
""",
}

Expand Down Expand Up @@ -623,6 +629,119 @@ def _test_optional_hash(env):

_tests.append(_test_optional_hash)

def _test_sdist_different_hashes(env):
"""Test that sdists with same hash but wheels with different hashes across platforms are handled correctly."""

def _mock_get_index_urls(_, distributions):
return {
"foo": struct(
whls = {
"deadbeef": struct(
filename = "foo-0.0.1-py3-none-win_amd64.whl",
url = "https://pypi.org/foo-0.0.1-win.whl",
sha256 = "deadbeef",
yanked = False,
),
"deadbaaf": struct(
filename = "foo-0.0.1-py3-none-manylinux_2_17_x86_64.whl",
url = "https://pypi.org/foo-0.0.1-linux.whl",
sha256 = "deadbaaf",
yanked = False,
),
},
sdists = {
"cafebabe": struct(
filename = "foo-0.0.1.tar.gz",
url = "https://pypi.org/foo-0.0.1.tar.gz",
sha256 = "cafebabe",
yanked = False,
),
},
),
}

got = parse_requirements(
ctx = _mock_ctx(),
requirements_by_platform = {
"requirements_sdist_different_hashes": ["cp315_windows_x86_64"],
"requirements_sdist_different_hashes_linux": ["cp315_linux_x86_64"],
},
get_index_urls = _mock_get_index_urls,
)
env.expect.that_dict(got).contains_exactly({
"foo": [
struct(
distribution = "foo",
extra_pip_args = [],
is_exposed = True,
sdist = None,
srcs = struct(
marker = "",
requirement = "foo==0.0.1",
requirement_line = "foo==0.0.1 --hash=sha256:deadbaaf --hash=sha256:cafebabe",
shas = ["cafebabe", "deadbaaf"],
url = "",
version = "0.0.1",
),
target_platforms = ["cp315_linux_x86_64"],
whls = [
struct(
filename = "foo-0.0.1-py3-none-manylinux_2_17_x86_64.whl",
url = "https://pypi.org/foo-0.0.1-linux.whl",
sha256 = "deadbaaf",
yanked = False,
),
],
),
struct(
distribution = "foo",
extra_pip_args = [],
is_exposed = True,
sdist = None,
srcs = struct(
marker = "",
requirement = "foo==0.0.1",
requirement_line = "foo==0.0.1 --hash=sha256:deadbeef --hash=sha256:cafebabe",
shas = ["cafebabe", "deadbeef"],
url = "",
version = "0.0.1",
),
target_platforms = ["cp315_windows_x86_64"],
whls = [
struct(
filename = "foo-0.0.1-py3-none-win_amd64.whl",
url = "https://pypi.org/foo-0.0.1-win.whl",
sha256 = "deadbeef",
yanked = False,
),
],
),
struct(
distribution = "foo",
extra_pip_args = [],
is_exposed = True,
sdist = struct(
filename = "foo-0.0.1.tar.gz",
url = "https://pypi.org/foo-0.0.1.tar.gz",
sha256 = "cafebabe",
yanked = False,
),
srcs = struct(
marker = "",
requirement = "foo==0.0.1",
requirement_line = "foo==0.0.1 --hash=sha256:deadbaaf --hash=sha256:cafebabe",
shas = ["cafebabe", "deadbaaf"],
url = "",
version = "0.0.1",
),
target_platforms = ["cp315_linux_x86_64", "cp315_windows_x86_64"],
whls = [],
),
],
})

_tests.append(_test_sdist_different_hashes)

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

Expand Down