diff --git a/news/4581.fixed.md b/news/4581.fixed.md new file mode 100644 index 0000000000..fb60ae1b6e --- /dev/null +++ b/news/4581.fixed.md @@ -0,0 +1,3 @@ +(pypi) The {obj}`pip` extension now evaluates from `MODULE.bazel.lock` alone +when an extra index hosts only some of the requirements +([#4581](https://github.com/bazel-contrib/rules_python/issues/4581)). diff --git a/python/private/pypi/pypi_cache.bzl b/python/private/pypi/pypi_cache.bzl index fd6498b596..bce9b3a9d8 100644 --- a/python/private/pypi/pypi_cache.bzl +++ b/python/private/pypi/pypi_cache.bzl @@ -73,7 +73,7 @@ def _pypi_cache_setdefault(self, key, parsed_result): # Filter the packages to only what is needed before writing to the facts cache filtered = _filter_packages(parsed_result, versions) - return self._facts.setdefault(index_url, filtered) + return self._facts.setdefault(index_url, filtered, versions) def _pypi_cache_get(self, key): """Return the parsed result from the cache. @@ -101,7 +101,7 @@ def _pypi_cache_get(self, key): # We might be using something from memory that is not yet stored in facts (e.g. we processed # the requirements.txt for one Python version and the deps got cached, but new python # version means different deps, which may add extras. - self._facts.setdefault(index_url, cached) + self._facts.setdefault(index_url, cached, versions) return cached @@ -204,7 +204,7 @@ def facts_cache(known_facts, facts_version = _FACT_VERSION): versions, facts_version, ), - setdefault = lambda url, value: _store_facts(facts, facts_version, url, value), + setdefault = lambda url, value, versions = None: _store_facts(facts, facts_version, url, value, versions), known_facts = known_facts, facts = facts, ) @@ -217,6 +217,7 @@ def _get_from_facts(facts, known_facts, index_url, requested_versions, facts_ver if type(requested_versions) == "dict": result = {} for pkg in requested_versions.keys(): + # An empty URL means that the package is not on this index. url = known_facts.get(_index_fact_key(pkg, index_url)) if url == None: # If the results are incomplete, return None, so that we can @@ -271,7 +272,12 @@ def _get_from_facts(facts, known_facts, index_url, requested_versions, facts_ver yanked = yanked, )) - if not known_sources: + missing_key = _missing_fact_key(index_url) + missing = [v for v in known_facts.get(missing_key, []) if v in requested_versions] + for version in missing: + retrieved_versions[version] = True + + if not retrieved_versions: # We found nothing in facts return None @@ -284,7 +290,10 @@ def _get_from_facts(facts, known_facts, index_url, requested_versions, facts_ver # entries are copied as-is instead of re-encoding the decoded structs, so # that a no-op run leaves the lockfile unchanged. facts["fact_version"] = facts_version - facts.setdefault(fact_key, {}).update(used_entries) + if used_entries: + facts.setdefault(fact_key, {}).update(used_entries) + if missing: + _store_missing_versions(facts, missing_key, missing) return struct( whls = known_sources.get("whls", {}), @@ -295,7 +304,7 @@ def _get_from_facts(facts, known_facts, index_url, requested_versions, facts_ver }, ) -def _store_facts(facts, fact_version, index_url, value): +def _store_facts(facts, fact_version, index_url, value, versions = None): """Store values as facts in the lock file. The main idea is to ensure that the lock file is small and it is only @@ -317,11 +326,48 @@ def _store_facts(facts, fact_version, index_url, value): "@": { "": "", # see `_encode_dist_entry` }, + + # The requested versions that the package page does not have, e.g. + # `foo@https://pypi.org/simple#missing` + "@#missing": ["", ...], } ``` + A requested package that is not on an index page is stored with an empty + URL, and the requested versions that a package page does not have are + stored as missing. This keeps the facts complete for every request, so + that an extra index that hosts only some of the packages or versions does + not need to be called again on every evaluation. + Adding a new field to the encoded entry requires bumping `_FACT_VERSION`. + + Args: + facts: {type}`dict` The facts to write to. + fact_version: {type}`str` The facts schema version. + index_url: {type}`str` The index URL or the package page URL. + value: The filtered result to store, can be `None`. + versions: The requested packages for an index page (a dict) or the + requested versions for a package page (a list). + + Returns: + The `value`. """ + if type(versions) == "dict": + for pkg in versions: + if pkg not in (value or {}): + facts["fact_version"] = fact_version + facts[_index_fact_key(pkg, index_url)] = "" + elif versions: + found = {} + if value: + for dists in (value.sdists, value.whls): + for d in dists.values(): + found[d.version] = True + missing = [v for v in versions if v not in found] + if missing: + facts["fact_version"] = fact_version + _store_missing_versions(facts, _missing_fact_key(index_url), missing) + if not value: return value @@ -380,6 +426,24 @@ def _dist_fact_key(index_url): root_url, _, distribution = index_url.rstrip("/").rpartition("/") return distribution + "@" + root_url.rstrip("/") +def _missing_fact_key(index_url): + """Return the fact key for the versions that a package page does not have. + + Args: + index_url: {type}`str` The URL of the package page in the index, e.g. + `https://pypi.org/simple/foo/`. + + Returns: + {type}`str` The fact key, e.g. `foo@https://pypi.org/simple#missing`. + """ + return _dist_fact_key(index_url) + "#missing" + +def _store_missing_versions(facts, missing_key, versions): + """Add `versions` to the sorted list of missing versions in `facts`.""" + known = {v: None for v in facts.get(missing_key, [])} + known.update({v: None for v in versions}) + facts[missing_key] = sorted(known.keys()) + def _encode_dist_entry(digest, dist): """Encode a distribution into a compact string. diff --git a/tests/pypi/pypi_cache/pypi_cache_tests.bzl b/tests/pypi/pypi_cache/pypi_cache_tests.bzl index 7578fba57e..6b3b15d7b2 100644 --- a/tests/pypi/pypi_cache/pypi_cache_tests.bzl +++ b/tests/pypi/pypi_cache/pypi_cache_tests.bzl @@ -820,6 +820,76 @@ def _test_pypi_cache_reads_index_urls_from_facts_drops_unaccessed(env): _tests.append(_test_pypi_cache_reads_index_urls_from_facts_drops_unaccessed) +def _test_pypi_cache_index_urls_facts_store_missing_packages(env): + """Verifies that packages missing from an index are stored, so the facts are complete.""" + cache = _cache(env, mctx = mocks.mctx(facts = {})) + key = ("https://pypi.org/simple/", "https://pypi.org/simple/", {"pkg-a": None, "pkg-c": None}) + cache.setdefault(key, { + "pkg-a": "https://pypi.org/simple/pkg-a/", + "pkg-b": "https://pypi.org/simple/pkg-b/", + }) + cache.expect_facts().contains_exactly({ + "fact_version": "v3", + "pkg-a@https://pypi.org/simple/": "https://pypi.org/simple/pkg-a/", + "pkg-c@https://pypi.org/simple/": "", + }) + + # A new evaluation can answer from the facts without calling the index. + cache2 = _cache(env, mctx = mocks.mctx(facts = cache.raw_facts())) + cache2.get(key).contains_exactly({ + "pkg-a": "https://pypi.org/simple/pkg-a/", + "pkg-c": "", + }) + cache2.expect_facts().contains_exactly(cache.raw_facts()) + + # An index that has none of the requested packages is also complete. + key = ("https://pypi.org/simple/", "https://pypi.org/simple/", {"pkg-c": None}) + cache2.get(key).contains_exactly({"pkg-c": ""}) + +_tests.append(_test_pypi_cache_index_urls_facts_store_missing_packages) + +def _test_pypi_cache_dist_facts_store_missing_versions(env): + """Verifies that versions missing from a package page are stored, so the facts are complete.""" + whl = struct( + digest = "sha256:sha_whl", + version = "1.0.0", + filename = "pkg-1.0.0-py3-none-any.whl", + url = "https://pypi.org/files/pkg-1.0.0-py3-none-any.whl", + metadata_url = "", + metadata_sha256 = "", + yanked = None, + ) + cache = _cache(env, mctx = mocks.mctx(facts = {})) + key = ("https://{PYPI_INDEX_URL}/pkg/", "https://pypi.org/simple/pkg/", ["1.0.0", "2.0.0"]) + cache.setdefault(key, struct( + sdists = {}, + whls = {"sha256:sha_whl": whl}, + hashes_by_version = {"1.0.0": ["sha256:sha_whl"]}, + )) + cache.expect_facts().contains_exactly({ + "fact_version": "v3", + "pkg@https://{PYPI_INDEX_URL}": { + "https://pypi.org/files/pkg-1.0.0-py3-none-any.whl": "sha256:sha_whl", + }, + "pkg@https://{PYPI_INDEX_URL}#missing": ["2.0.0"], + }) + + # A new evaluation can answer from the facts without calling the index. + cache2 = _cache(env, mctx = mocks.mctx(facts = cache.raw_facts())) + got = cache2.get(key) + got.whls().contains_exactly({"sha256:sha_whl": whl}) + got.sdists().contains_exactly({}) + got.hashes_by_version().contains_exactly({"1.0.0": ["sha256:sha_whl"]}) + cache2.expect_facts().contains_exactly(cache.raw_facts()) + + # A package page that has none of the requested versions is also complete. + key = ("https://{PYPI_INDEX_URL}/pkg/", "https://pypi.org/simple/pkg/", ["2.0.0"]) + got = cache2.get(key) + got.whls().contains_exactly({}) + got.sdists().contains_exactly({}) + +_tests.append(_test_pypi_cache_dist_facts_store_missing_versions) + def pypi_cache_test_suite(name): test_suite( name = name,