diff --git a/examples/bzlmod/MODULE.bazel.lock b/examples/bzlmod/MODULE.bazel.lock index 5a546c2f7a..20e8dd4797 100644 --- a/examples/bzlmod/MODULE.bazel.lock +++ b/examples/bzlmod/MODULE.bazel.lock @@ -1562,7 +1562,7 @@ }, "@@rules_python~//python/extensions:pip.bzl%pip": { "general": { - "bzlTransitiveDigest": "MwmpiMn2qoAVC+3E9MF3E98fB8v1utYBfMa0frXyi7g=", + "bzlTransitiveDigest": "3rjKs4RZR6Vxy6H3ehWUlJ+mWugczupHplKnVxZ03v8=", "usagesDigest": "VmrNvB/4EhzsYieLDka9584M+pYKPpjNLl3Wcb5rx/c=", "recordedFileInputs": { "@@//requirements_lock_3_10.txt": "5e7083982a7e60f34998579a0ae83b520d46ab8f2552cc51337217f024e6def5", @@ -7035,7 +7035,7 @@ }, "@@rules_python~//python/private/pypi:pip.bzl%pip_internal": { "general": { - "bzlTransitiveDigest": "Kx383BMHUpAHEjRiU5aWU4QTRQVg+Uu+Mgi7jVxuz0c=", + "bzlTransitiveDigest": "qjsx4HwW3BgZlPpGK9JmX+W4eUDZsfcTXEbkyz2Go1U=", "usagesDigest": "/lZXl/ZgP+u5PE8WkeWTyYBsvX9XQWFn1antj5qrBzQ=", "recordedFileInputs": { "@@rules_python~//tools/publish/requirements_linux.txt": "8175b4c8df50ae2f22d1706961884beeb54e7da27bd2447018314a175981997d", diff --git a/python/private/pypi/parse_requirements.bzl b/python/private/pypi/parse_requirements.bzl index a43217dbc2..cfc1d0b6f1 100644 --- a/python/private/pypi/parse_requirements.bzl +++ b/python/private/pypi/parse_requirements.bzl @@ -32,6 +32,15 @@ load(":index_sources.bzl", "index_sources") load(":parse_requirements_txt.bzl", "parse_requirements_txt") load(":whl_target_platforms.bzl", "select_whls") +def extract_version(entry): + """Extract the version part from the requirement string.""" + version_start = entry.find("==") + if version_start != -1: + # Extract everything after '==' until the next space or end of the string + version = entry[version_start + 2:].split(" ")[0] + return version + return None + def parse_requirements( ctx, *, @@ -92,7 +101,7 @@ def parse_requirements( # are returned as just the base package name. e.g., `foo[bar]` results # in an entry like `("foo", "foo[bar] == 1.0 ...")`. requirements_dict = { - normalize_name(entry[0]): entry + (entry[0], extract_version(entry[1])): entry for entry in sorted( parse_result.requirements, # Get the longest match and fallback to original WORKSPACE sorting, diff --git a/tests/pypi/parse_requirements/parse_requirements_tests.bzl b/tests/pypi/parse_requirements/parse_requirements_tests.bzl index a6e17bebec..854fb945a0 100644 --- a/tests/pypi/parse_requirements/parse_requirements_tests.bzl +++ b/tests/pypi/parse_requirements/parse_requirements_tests.bzl @@ -66,6 +66,10 @@ 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_different_package_version": """\ +foo==0.0.1+local --hash=sha256:deadbeef +foo==0.0.1 --hash=sha256:deadb00f """, } @@ -382,6 +386,49 @@ def _test_env_marker_resolution(env): _tests.append(_test_env_marker_resolution) +# +def _test_different_package_version(env): + got = parse_requirements( + ctx = _mock_ctx(), + requirements_by_platform = { + "requirements_different_package_version": ["linux_x86_64"], + }, + ) + env.expect.that_dict(got).contains_exactly({ + "foo": [ + struct( + distribution = "foo", + extra_pip_args = [], + requirement_line = "foo==0.0.1 --hash=sha256:deadb00f", + srcs = struct( + requirement = "foo==0.0.1", + shas = ["deadb00f"], + version = "0.0.1", + ), + target_platforms = ["linux_x86_64"], + whls = [], + sdist = None, + is_exposed = True, + ), + struct( + distribution = "foo", + extra_pip_args = [], + requirement_line = "foo==0.0.1+local --hash=sha256:deadbeef", + srcs = struct( + requirement = "foo==0.0.1+local", + shas = ["deadbeef"], + version = "0.0.1+local", + ), + target_platforms = ["linux_x86_64"], + whls = [], + sdist = None, + is_exposed = True, + ), + ], + }) + +_tests.append(_test_different_package_version) + def parse_requirements_test_suite(name): """Create the test suite.