Skip to content

Commit 537fc4b

Browse files
authored
fix(pypi): correctly fallback to pip for git direct URLs (#2732)
Whilst integrating #2695 I introduced a regression and here I add a test for that and fix it. The code that was getting the filename from the URL was too eager and would break if there was a git ref as noted in the test. Before this commit and #2695 the code was not handling all of the cases that are tested now either, so I think now we are in a good place. I am not sure how we should handle the `git_repository` URLs. Maybe having `http_archive` and `git_repository` usage would be nice, but I am not sure how we can introduce it at the moment. Work towards #2363
1 parent f65b2ac commit 537fc4b

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

python/private/pypi/parse_requirements.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ def _add_dists(*, requirement, index_urls, logger = None):
297297
if requirement.srcs.url:
298298
url = requirement.srcs.url
299299
_, _, filename = url.rpartition("/")
300+
if "." not in filename:
301+
# detected filename has no extension, it might be an sdist ref
302+
# TODO @aignas 2025-04-03: should be handled if the following is fixed:
303+
# https://github.com/bazel-contrib/rules_python/issues/2363
304+
return [], None
305+
300306
direct_url_dist = struct(
301307
url = url,
302308
filename = filename,

tests/pypi/extension/extension_tests.bzl

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ some_pkg==0.0.1 @ example-direct.org/some_pkg-0.0.1-py3-none-any.whl \
662662
direct_without_sha==0.0.1 @ example-direct.org/direct_without_sha-0.0.1-py3-none-any.whl
663663
some_other_pkg==0.0.1
664664
pip_fallback==0.0.1
665+
direct_sdist_without_sha @ some-archive/any-name.tar.gz
666+
git_dep @ git+https://git.server/repo/project@deadbeefdeadbeef
665667
""",
666668
}[x],
667669
),
@@ -672,10 +674,28 @@ pip_fallback==0.0.1
672674
)
673675

674676
pypi.is_reproducible().equals(False)
675-
pypi.exposed_packages().contains_exactly({"pypi": ["direct_without_sha", "pip_fallback", "simple", "some_other_pkg", "some_pkg"]})
677+
pypi.exposed_packages().contains_exactly({"pypi": [
678+
"direct_sdist_without_sha",
679+
"direct_without_sha",
680+
"git_dep",
681+
"pip_fallback",
682+
"simple",
683+
"some_other_pkg",
684+
"some_pkg",
685+
]})
676686
pypi.hub_group_map().contains_exactly({"pypi": {}})
677687
pypi.hub_whl_map().contains_exactly({
678688
"pypi": {
689+
"direct_sdist_without_sha": {
690+
"pypi_315_any_name": [
691+
struct(
692+
config_setting = None,
693+
filename = "any-name.tar.gz",
694+
target_platforms = None,
695+
version = "3.15",
696+
),
697+
],
698+
},
679699
"direct_without_sha": {
680700
"pypi_315_direct_without_sha_0_0_1_py3_none_any": [
681701
struct(
@@ -686,6 +706,16 @@ pip_fallback==0.0.1
686706
),
687707
],
688708
},
709+
"git_dep": {
710+
"pypi_315_git_dep": [
711+
struct(
712+
config_setting = None,
713+
filename = None,
714+
target_platforms = None,
715+
version = "3.15",
716+
),
717+
],
718+
},
689719
"pip_fallback": {
690720
"pypi_315_pip_fallback": [
691721
struct(
@@ -737,6 +767,17 @@ pip_fallback==0.0.1
737767
},
738768
})
739769
pypi.whl_libraries().contains_exactly({
770+
"pypi_315_any_name": {
771+
"dep_template": "@pypi//{name}:{target}",
772+
"experimental_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"],
773+
"extra_pip_args": ["--extra-args-for-sdist-building"],
774+
"filename": "any-name.tar.gz",
775+
"python_interpreter_target": "unit_test_interpreter_target",
776+
"repo": "pypi_315",
777+
"requirement": "direct_sdist_without_sha @ some-archive/any-name.tar.gz",
778+
"sha256": "",
779+
"urls": ["some-archive/any-name.tar.gz"],
780+
},
740781
"pypi_315_direct_without_sha_0_0_1_py3_none_any": {
741782
"dep_template": "@pypi//{name}:{target}",
742783
"experimental_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"],
@@ -747,6 +788,13 @@ pip_fallback==0.0.1
747788
"sha256": "",
748789
"urls": ["example-direct.org/direct_without_sha-0.0.1-py3-none-any.whl"],
749790
},
791+
"pypi_315_git_dep": {
792+
"dep_template": "@pypi//{name}:{target}",
793+
"extra_pip_args": ["--extra-args-for-sdist-building"],
794+
"python_interpreter_target": "unit_test_interpreter_target",
795+
"repo": "pypi_315",
796+
"requirement": "git_dep @ git+https://git.server/repo/project@deadbeefdeadbeef",
797+
},
750798
"pypi_315_pip_fallback": {
751799
"dep_template": "@pypi//{name}:{target}",
752800
"extra_pip_args": ["--extra-args-for-sdist-building"],

0 commit comments

Comments
 (0)