Skip to content

Commit b4458e9

Browse files
committed
add an escape hatch and documentation if users encounter errors
1 parent 82fc4d7 commit b4458e9

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ Unreleased changes template.
8484
This means that `rules_python` will attempt to fetch metadata for all
8585
packages through SimpleAPI unless they are pulled through direct URL
8686
references. Fixes [#2023](https://github.com/bazel-contrib/rules_python/issues/2023).
87+
In case you see issues with `rules_python` being too eager to fetch the SimpleAPI
88+
metadata, you can use the newly added {attr}`pip.parse.experimental_skip_sources`
89+
to skip metadata fetching for those packages.
8790
* (uv) A {obj}`lock` rule that is the replacement for the
8891
{obj}`compile_pip_requirements`. This may still have rough corners
8992
so please report issues with it in the

python/private/pypi/extension.bzl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,21 @@ You cannot use both the additive_build_content and additive_build_content_file a
467467
get_index_urls = None
468468
if pip_attr.experimental_index_url:
469469
is_reproducible = False
470+
skip_sources = [
471+
normalize_name(s)
472+
for s in pip_attr.simpleapi_skip
473+
]
470474
get_index_urls = lambda ctx, distributions: simpleapi_download(
471475
ctx,
472476
attr = struct(
473477
index_url = pip_attr.experimental_index_url,
474478
extra_index_urls = pip_attr.experimental_extra_index_urls or [],
475479
index_url_overrides = pip_attr.experimental_index_url_overrides or {},
476-
sources = distributions,
480+
sources = [
481+
d
482+
for d in distributions
483+
if normalize_name(d) not in skip_sources
484+
],
477485
envsubst = pip_attr.envsubst,
478486
# Auth related info
479487
netrc = pip_attr.netrc,
@@ -690,6 +698,11 @@ This is equivalent to `--index-url` `pip` option.
690698
If {attr}`download_only` is set, then `sdist` archives will be discarded and `pip.parse` will
691699
operate in wheel-only mode.
692700
:::
701+
702+
:::{versionchanged} VERSION_NEXT_FEATURE
703+
Index metadata will be used to deduct `sha256` values for packages even if the
704+
`sha256` values are not present in the requirements.txt lock file.
705+
:::
693706
""",
694707
),
695708
"experimental_index_url_overrides": attr.string_dict(
@@ -757,6 +770,18 @@ The Python version the dependencies are targetting, in Major.Minor format
757770
If an interpreter isn't explicitly provided (using `python_interpreter` or
758771
`python_interpreter_target`), then the version specified here must have
759772
a corresponding `python.toolchain()` configured.
773+
""",
774+
),
775+
"simpleapi_skip": attr.string_list(
776+
doc = """\
777+
The list of packages to skip fetching metadata for from SimpleAPI index. You should
778+
normally not need this attribute, but in case you do, please report this as a bug
779+
to `rules_python` and use this attribute until the bug is fixed.
780+
781+
EXPERIMENTAL: this may be removed without notice.
782+
783+
:::{versionadded} VERSION_NEXT_FEATURE
784+
:::
760785
""",
761786
),
762787
"whl_modifications": attr.label_keyed_string_dict(

python/private/pypi/parse_requirements.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,10 @@ def _add_dists(*, requirement, index_urls, logger = None):
318318
# First try to find distributions by SHA256 if provided
319319
shas_to_use = requirement.srcs.shas
320320
if not shas_to_use:
321-
shas_to_use = index_urls.sha256s_by_version.get(requirement.srcs.version, [])
321+
version = requirement.srcs.version
322+
shas_to_use = index_urls.sha256s_by_version.get(version, [])
323+
if logger:
324+
logger.warn(lambda: "requirement file has been generated without hashes, will use all hashes for the given version {} that could find on the index:\n {}".format(version, shas_to_use))
322325

323326
for sha256 in shas_to_use:
324327
# For now if the artifact is marked as yanked we just ignore it.

python/private/pypi/simpleapi_download.bzl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,17 @@ def simpleapi_download(
127127

128128
failed_sources = [pkg for pkg in attr.sources if pkg not in found_on_index]
129129
if failed_sources:
130-
_fail("Failed to download metadata for {} for from urls: {}".format(
131-
failed_sources,
132-
index_urls,
133-
))
130+
_fail(
131+
"\n".join([
132+
"Failed to download metadata for {} for from urls: {}.".format(
133+
failed_sources,
134+
index_urls,
135+
),
136+
"If you would like to skip downloading metadata for these packages please add 'simpleapi_skip={}' to your 'pip.parse' call.".format(
137+
render.list(failed_sources),
138+
),
139+
]),
140+
)
134141
return None
135142

136143
if warn_overrides:

tests/pypi/extension/extension_tests.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def _parse(
100100
requirements_linux = None,
101101
requirements_lock = None,
102102
requirements_windows = None,
103+
simpleapi_skip = [],
103104
timeout = 600,
104105
whl_modifications = {},
105106
**kwargs):
@@ -136,6 +137,7 @@ def _parse(
136137
experimental_extra_index_urls = [],
137138
parallel_download = False,
138139
experimental_index_url_overrides = {},
140+
simpleapi_skip = simpleapi_skip,
139141
**kwargs
140142
)
141143

tests/pypi/simpleapi_download/simpleapi_download_tests.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ def _test_fail(env):
110110
)
111111

112112
env.expect.that_collection(fails).contains_exactly([
113-
"""Failed to download metadata for ["foo"] for from urls: ["main", "extra"]""",
113+
"""\
114+
Failed to download metadata for ["foo"] for from urls: ["main", "extra"].
115+
If you would like to skip downloading metadata for these packages please add 'simpleapi_skip=["foo"]' to your 'pip.parse' call.\
116+
""",
114117
])
115118
env.expect.that_collection(calls).contains_exactly([
116119
"extra/foo/",

0 commit comments

Comments
 (0)