Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt user to use the backtracking resolver on errors #1719

Merged
merged 10 commits into from
Nov 10, 2022
12 changes: 11 additions & 1 deletion piptools/scripts/compile.py
Expand Up @@ -16,7 +16,7 @@

from .._compat import IS_CLICK_VER_8_PLUS, parse_requirements
from ..cache import DependencyCache
from ..exceptions import PipToolsError
from ..exceptions import NoCandidateFound, PipToolsError
from ..locations import CACHE_DIR
from ..logging import log
from ..repositories import LocalRequirementsRepository, PyPIRepository
Expand Down Expand Up @@ -545,6 +545,16 @@ def cli(
)
results = resolver.resolve(max_rounds=max_rounds)
hashes = resolver.resolve_hashes(results) if generate_hashes else None
except NoCandidateFound as e:
if resolver_cls == LegacyResolver: # pragma: no branch
log.error(
"Using legacy resolver. "
"Consider using backtracking resolver with "
"`--resolver=backtracking`."
)

log.error(str(e))
sys.exit(2)
except PipToolsError as e:
log.error(str(e))
sys.exit(2)
Expand Down
33 changes: 33 additions & 0 deletions tests/test_cli_compile.py
Expand Up @@ -2487,3 +2487,36 @@ def test_preserve_via_requirements_constrained_dependencies_when_run_twice(
for output in (first_out, second_out):
assert output.exit_code == 0, output
assert output.stderr == expected_output


def test_failure_of_legacy_resolver_prompts_for_backtracking(
pip_conf, runner, tmpdir, make_package, make_wheel, current_resolver
):
"""Test that pip-compile prompts to use the backtracking resolver"""
pkgs = [
make_package("a", version="0.1", install_requires=["b==0.1"]),
make_package("a", version="0.2", install_requires=["b==0.2"]),
make_package("b", version="0.1"),
make_package("b", version="0.2"),
make_package("c", version="1", install_requires=["b==0.1", "a"]),
]

dists_dir = tmpdir / "dists"
for pkg in pkgs:
make_wheel(pkg, dists_dir)

with open("requirements.in", "w") as req_in:
req_in.writelines(["c"])

out = runner.invoke(
cli,
["--resolver", current_resolver, "--find-links", str(dists_dir)],
)

if current_resolver == "legacy":
assert out.exit_code == 2, out
assert "Consider using backtracking resolver with" in out.stderr
elif current_resolver == "backtracking":
assert out.exit_code == 0, out
else: # pragma: no cover
raise AssertionError("unreachable")