From f35b53eb823eb2ff761edc88e30e31f48613d6c5 Mon Sep 17 00:00:00 2001 From: Pradyun Gedam Date: Sun, 12 Dec 2021 13:00:51 +0000 Subject: [PATCH] Add `--use-deprecated=backtrack-on-build-failures` This serves as an opt-out from build failures causing the entire installation to abort. --- src/pip/_internal/cli/req_command.py | 26 +++++++++++++++++++ .../resolution/resolvelib/factory.py | 16 ++++++++++++ .../resolution/resolvelib/resolver.py | 2 ++ tests/functional/test_new_resolver.py | 19 ++++++++++++++ tests/unit/resolution_resolvelib/conftest.py | 1 + .../resolution_resolvelib/test_resolver.py | 1 + 6 files changed, 65 insertions(+) diff --git a/src/pip/_internal/cli/req_command.py b/src/pip/_internal/cli/req_command.py index 1607c78b187..fd0fdd02ebf 100644 --- a/src/pip/_internal/cli/req_command.py +++ b/src/pip/_internal/cli/req_command.py @@ -227,6 +227,30 @@ def determine_resolver_variant(options: Values) -> str: return "2020-resolver" + @staticmethod + def determine_build_failure_suppression(options: Values) -> bool: + """Determines whether build failures should be suppressed and backtracked on.""" + if "backtrack-on-build-failures" not in options.deprecated_features_enabled: + return False + + if "legacy-resolver" in options.deprecated_features_enabled: + raise CommandError("Cannot backtrack with legacy resolver.") + + deprecated( + reason=( + "Backtracking on build failures can mask issues related to how " + "a package generates metadata or builds a wheel." + ), + gone_in="22.2", + replacement=( + "avoiding known-bad versions by explicitly telling pip to ignore them " + "(either directly as a requirements, or via a constraints file)." + ), + feature_flag=None, + issue=10655, + ) + return True + @classmethod def make_requirement_preparer( cls, @@ -321,6 +345,7 @@ def make_resolver( isolated=options.isolated_mode, use_pep517=use_pep517, ) + suppress_build_failures = cls.determine_build_failure_suppression(options) resolver_variant = cls.determine_resolver_variant(options) # The long import name and duplicated invocation is needed to convince # Mypy into correctly typechecking. Otherwise it would complain the @@ -340,6 +365,7 @@ def make_resolver( force_reinstall=force_reinstall, upgrade_strategy=upgrade_strategy, py_version_info=py_version_info, + suppress_build_failures=suppress_build_failures, ) import pip._internal.resolution.legacy.resolver diff --git a/src/pip/_internal/resolution/resolvelib/factory.py b/src/pip/_internal/resolution/resolvelib/factory.py index fdf95c2f8d8..d2880d76529 100644 --- a/src/pip/_internal/resolution/resolvelib/factory.py +++ b/src/pip/_internal/resolution/resolvelib/factory.py @@ -27,6 +27,7 @@ from pip._internal.exceptions import ( DistributionNotFound, InstallationError, + InstallationSubprocessError, MetadataInconsistent, UnsupportedPythonVersion, UnsupportedWheel, @@ -96,6 +97,7 @@ def __init__( force_reinstall: bool, ignore_installed: bool, ignore_requires_python: bool, + suppress_build_failures: bool, py_version_info: Optional[Tuple[int, ...]] = None, ) -> None: self._finder = finder @@ -106,6 +108,7 @@ def __init__( self._use_user_site = use_user_site self._force_reinstall = force_reinstall self._ignore_requires_python = ignore_requires_python + self._suppress_build_failures = suppress_build_failures self._build_failures: Cache[InstallationError] = {} self._link_candidate_cache: Cache[LinkCandidate] = {} @@ -193,6 +196,13 @@ def _make_candidate_from_link( logger.warning("Discarding %s. %s", link, e) self._build_failures[link] = e return None + except InstallationSubprocessError as e: + if not self._suppress_build_failures: + raise + logger.warning("Discarding %s due to build failure: %e", link, e) + self._build_failures[link] = e + return None + base: BaseCandidate = self._editable_candidate_cache[link] else: if link not in self._link_candidate_cache: @@ -208,6 +218,12 @@ def _make_candidate_from_link( logger.warning("Discarding %s. %s", link, e) self._build_failures[link] = e return None + except InstallationSubprocessError as e: + if not self._suppress_build_failures: + raise + logger.warning("Discarding %s due to build failure: %e", link, e) + self._build_failures[link] = e + return None base = self._link_candidate_cache[link] if not extras: diff --git a/src/pip/_internal/resolution/resolvelib/resolver.py b/src/pip/_internal/resolution/resolvelib/resolver.py index 8ee36d377d8..618f1e1aead 100644 --- a/src/pip/_internal/resolution/resolvelib/resolver.py +++ b/src/pip/_internal/resolution/resolvelib/resolver.py @@ -47,6 +47,7 @@ def __init__( ignore_requires_python: bool, force_reinstall: bool, upgrade_strategy: str, + suppress_build_failures: bool, py_version_info: Optional[Tuple[int, ...]] = None, ): super().__init__() @@ -61,6 +62,7 @@ def __init__( force_reinstall=force_reinstall, ignore_installed=ignore_installed, ignore_requires_python=ignore_requires_python, + suppress_build_failures=suppress_build_failures, py_version_info=py_version_info, ) self.ignore_dependencies = ignore_dependencies diff --git a/tests/functional/test_new_resolver.py b/tests/functional/test_new_resolver.py index 5a0a480e298..00179fc962a 100644 --- a/tests/functional/test_new_resolver.py +++ b/tests/functional/test_new_resolver.py @@ -2329,6 +2329,25 @@ def test_new_resolver_do_not_backtrack_on_build_failure( assert "egg_info" in result.stderr +def test_new_resolver_flag_permits_backtracking_on_build_failure( + script: PipTestEnvironment, +) -> None: + create_basic_sdist_for_package(script, "pkg1", "2.0", fails_egg_info=True) + create_basic_wheel_for_package(script, "pkg1", "1.0") + + script.pip( + "install", + "--use-deprecated=backtrack-on-build-failures", + "--no-cache-dir", + "--no-index", + "--find-links", + script.scratch_path, + "pkg1", + ) + + script.assert_installed(pkg1="1.0") + + def test_new_resolver_works_when_failing_package_builds_are_disallowed( script: PipTestEnvironment, ) -> None: diff --git a/tests/unit/resolution_resolvelib/conftest.py b/tests/unit/resolution_resolvelib/conftest.py index 109b337ff20..261015fb306 100644 --- a/tests/unit/resolution_resolvelib/conftest.py +++ b/tests/unit/resolution_resolvelib/conftest.py @@ -62,6 +62,7 @@ def factory(finder: PackageFinder, preparer: RequirementPreparer) -> Iterator[Fa force_reinstall=False, ignore_installed=False, ignore_requires_python=False, + suppress_build_failures=False, py_version_info=None, ) diff --git a/tests/unit/resolution_resolvelib/test_resolver.py b/tests/unit/resolution_resolvelib/test_resolver.py index 579195b55ea..d1af696b7a2 100644 --- a/tests/unit/resolution_resolvelib/test_resolver.py +++ b/tests/unit/resolution_resolvelib/test_resolver.py @@ -29,6 +29,7 @@ def resolver(preparer: RequirementPreparer, finder: PackageFinder) -> Resolver: ignore_requires_python=False, force_reinstall=False, upgrade_strategy="to-satisfy-only", + suppress_build_failures=False, ) return resolver