|
27 | 27 | from packaging.requirements import Requirement |
28 | 28 | from packaging.utils import canonicalize_name |
29 | 29 | from packaging.version import Version |
| 30 | +from resolvelib.resolvers import ResolverException |
30 | 31 |
|
31 | 32 | from fromager import bootstrapper, build_environment |
32 | 33 | from fromager.bootstrapper._build import Build |
@@ -692,6 +693,145 @@ def test_returns_empty_list(self, tmp_context: WorkContext) -> None: |
692 | 693 | assert item.run(bt) == [] |
693 | 694 |
|
694 | 695 |
|
| 696 | +class TestEnrichResolutionError: |
| 697 | + """Tests for _enrich_resolution_error, used by _handle_phase_error.""" |
| 698 | + |
| 699 | + def _why_snapshot(self) -> list[tuple[RequirementType, Requirement, Version]]: |
| 700 | + return [ |
| 701 | + (RequirementType.TOP_LEVEL, Requirement("its-hub"), Version("1.0")), |
| 702 | + (RequirementType.INSTALL, Requirement("reward-hub"), Version("2.0")), |
| 703 | + (RequirementType.INSTALL, Requirement("vllm"), Version("3.0")), |
| 704 | + ] |
| 705 | + |
| 706 | + def test_resolver_exception_gets_dependency_chain( |
| 707 | + self, tmp_context: WorkContext |
| 708 | + ) -> None: |
| 709 | + """Regression test for #1214: name the whole chain, not just the |
| 710 | +
|
| 711 | + top-level package. |
| 712 | + """ |
| 713 | + bt = bootstrapper.Bootstrapper(tmp_context) |
| 714 | + item = _make_resolve_item( |
| 715 | + req="flashinfer-python==0.6.8.post1", why_snapshot=self._why_snapshot() |
| 716 | + ) |
| 717 | + err = ResolverException("found no match") |
| 718 | + |
| 719 | + enriched = bt._enrich_resolution_error(item, err) |
| 720 | + |
| 721 | + assert isinstance(enriched, ResolverException) |
| 722 | + assert str(enriched) == ( |
| 723 | + "found no match (dependency chain: its-hub==1.0 -> " |
| 724 | + "reward-hub==2.0 -> vllm==3.0 -> flashinfer-python==0.6.8.post1)" |
| 725 | + ) |
| 726 | + |
| 727 | + def test_runtime_error_gets_dependency_chain( |
| 728 | + self, tmp_context: WorkContext |
| 729 | + ) -> None: |
| 730 | + """Regression test for a gap in the earlier fix: a plain RuntimeError |
| 731 | +
|
| 732 | + (e.g. "Could not resolve any versions for ...") also gets the chain, |
| 733 | + not just ResolverException. |
| 734 | + """ |
| 735 | + bt = bootstrapper.Bootstrapper(tmp_context) |
| 736 | + item = _make_resolve_item( |
| 737 | + req="flashinfer-python==0.6.8.post1", why_snapshot=self._why_snapshot() |
| 738 | + ) |
| 739 | + err = RuntimeError("Could not resolve any versions for flashinfer-python") |
| 740 | + |
| 741 | + enriched = bt._enrich_resolution_error(item, err) |
| 742 | + |
| 743 | + assert isinstance(enriched, RuntimeError) |
| 744 | + assert str(enriched) == ( |
| 745 | + "Could not resolve any versions for flashinfer-python " |
| 746 | + "(dependency chain: its-hub==1.0 -> reward-hub==2.0 -> " |
| 747 | + "vllm==3.0 -> flashinfer-python==0.6.8.post1)" |
| 748 | + ) |
| 749 | + |
| 750 | + def test_unknown_exception_type_falls_back_to_runtime_error( |
| 751 | + self, tmp_context: WorkContext |
| 752 | + ) -> None: |
| 753 | + """`type(err)(msg)` isn't safe for arbitrary exception classes, so an |
| 754 | +
|
| 755 | + unrecognized type is wrapped in a RuntimeError that keeps the |
| 756 | + original type name visible, instead of risking a TypeError trying |
| 757 | + to reconstruct it. |
| 758 | + """ |
| 759 | + bt = bootstrapper.Bootstrapper(tmp_context) |
| 760 | + item = _make_resolve_item( |
| 761 | + req="flashinfer-python==0.6.8.post1", why_snapshot=self._why_snapshot() |
| 762 | + ) |
| 763 | + err = ValueError("some unexpected failure") |
| 764 | + |
| 765 | + enriched = bt._enrich_resolution_error(item, err) |
| 766 | + |
| 767 | + assert isinstance(enriched, RuntimeError) |
| 768 | + assert "ValueError: some unexpected failure" in str(enriched) |
| 769 | + assert "dependency chain: its-hub==1.0" in str(enriched) |
| 770 | + |
| 771 | + def test_no_chain_returns_original_object_unchanged( |
| 772 | + self, tmp_context: WorkContext |
| 773 | + ) -> None: |
| 774 | + """Top-level requirements have no chain, so the original exception |
| 775 | +
|
| 776 | + object is returned as-is (identity preserved, nothing reconstructed). |
| 777 | + """ |
| 778 | + bt = bootstrapper.Bootstrapper(tmp_context) |
| 779 | + item = _make_resolve_item() # default why_snapshot=[] |
| 780 | + err = ResolverException("found no match") |
| 781 | + |
| 782 | + enriched = bt._enrich_resolution_error(item, err) |
| 783 | + |
| 784 | + assert enriched is err |
| 785 | + |
| 786 | + def test_non_resolve_phase_returns_original_object_unchanged( |
| 787 | + self, tmp_context: WorkContext |
| 788 | + ) -> None: |
| 789 | + """Only RESOLVE-phase failures get a chain suffix.""" |
| 790 | + bt = bootstrapper.Bootstrapper(tmp_context) |
| 791 | + item = _make_build_item(phase=BootstrapPhase.BUILD) |
| 792 | + item.work_item.why_snapshot = self._why_snapshot() |
| 793 | + err = RuntimeError("build failed") |
| 794 | + |
| 795 | + enriched = bt._enrich_resolution_error(item, err) |
| 796 | + |
| 797 | + assert enriched is err |
| 798 | + |
| 799 | + def test_enriched_exception_chains_via_context_not_cause( |
| 800 | + self, tmp_context: WorkContext |
| 801 | + ) -> None: |
| 802 | + """When actually raised (not just constructed), the enriched |
| 803 | +
|
| 804 | + exception must carry the original as `__context__` (implicit |
| 805 | + chaining, preserves the real traceback for `exc_info=True` |
| 806 | + logging) while leaving `__cause__` unset, so |
| 807 | + `__main__._format_exception()` - which only follows `__cause__` - |
| 808 | + doesn't print the original message a second time via |
| 809 | + "... because ...". See #1243 for that formatter's own, |
| 810 | + independent duplication bug. |
| 811 | + """ |
| 812 | + from fromager import __main__ |
| 813 | + |
| 814 | + bt = bootstrapper.Bootstrapper(tmp_context) |
| 815 | + item = _make_resolve_item( |
| 816 | + req="flashinfer-python==0.6.8.post1", why_snapshot=self._why_snapshot() |
| 817 | + ) |
| 818 | + original = ResolverException("found no match") |
| 819 | + |
| 820 | + try: |
| 821 | + raise original |
| 822 | + except ResolverException as caught: |
| 823 | + enriched = bt._enrich_resolution_error(item, caught) |
| 824 | + try: |
| 825 | + raise enriched |
| 826 | + except ResolverException as raised: |
| 827 | + final = raised |
| 828 | + |
| 829 | + assert final.__cause__ is None |
| 830 | + assert final.__context__ is original |
| 831 | + formatted = __main__._format_exception(final) |
| 832 | + assert formatted.count("found no match") == 1 |
| 833 | + |
| 834 | + |
695 | 835 | class TestHandlePhaseError: |
696 | 836 | # -- RESOLVE phase errors -- |
697 | 837 |
|
@@ -741,6 +881,75 @@ def test_resolve_error_in_multiple_versions_mode_continues( |
741 | 881 | assert key in bt._failed_versions |
742 | 882 | assert bt._failed_versions[key] is err |
743 | 883 |
|
| 884 | + def test_resolve_error_enriched_before_test_mode_record( |
| 885 | + self, tmp_context: WorkContext |
| 886 | + ) -> None: |
| 887 | + """Gap identified in review: test mode must record the enriched |
| 888 | +
|
| 889 | + message, not the raw one, so `failed_packages[].exception_message` |
| 890 | + shows the real dependency chain. |
| 891 | + """ |
| 892 | + bt = bootstrapper.Bootstrapper(tmp_context, test_mode=True) |
| 893 | + why_snapshot = [ |
| 894 | + (RequirementType.TOP_LEVEL, Requirement("its-hub"), Version("1.0")), |
| 895 | + ] |
| 896 | + item = _make_resolve_item( |
| 897 | + req="flashinfer-python==0.6.8.post1", why_snapshot=why_snapshot |
| 898 | + ) |
| 899 | + err = ResolverException("found no match") |
| 900 | + |
| 901 | + bt._handle_phase_error(item, err) |
| 902 | + |
| 903 | + assert len(bt.failed_packages) == 1 |
| 904 | + assert ( |
| 905 | + "dependency chain: its-hub==1.0" |
| 906 | + in (bt.failed_packages[0]["exception_message"]) |
| 907 | + ) |
| 908 | + |
| 909 | + def test_resolve_error_enriched_before_multiple_versions_record( |
| 910 | + self, tmp_context: WorkContext |
| 911 | + ) -> None: |
| 912 | + """Gap identified in review: multiple-versions mode must record the |
| 913 | +
|
| 914 | + enriched message too. |
| 915 | + """ |
| 916 | + bt = bootstrapper.Bootstrapper(tmp_context, multiple_versions=True) |
| 917 | + why_snapshot = [ |
| 918 | + (RequirementType.TOP_LEVEL, Requirement("its-hub"), Version("1.0")), |
| 919 | + ] |
| 920 | + item = _make_resolve_item( |
| 921 | + req="flashinfer-python==0.6.8.post1", why_snapshot=why_snapshot |
| 922 | + ) |
| 923 | + err = ResolverException("found no match") |
| 924 | + |
| 925 | + bt._handle_phase_error(item, err) |
| 926 | + |
| 927 | + key = (canonicalize_name("flashinfer-python"), "unresolved") |
| 928 | + assert "dependency chain: its-hub==1.0" in str(bt._failed_versions[key]) |
| 929 | + |
| 930 | + def test_resolve_error_enriched_before_normal_mode_raise( |
| 931 | + self, tmp_context: WorkContext |
| 932 | + ) -> None: |
| 933 | + """Gap this feature originally targeted: a plain RuntimeError |
| 934 | +
|
| 935 | + ("Could not resolve any versions...") also gets the chain in |
| 936 | + normal (fail-fast) mode, not just ResolverException. |
| 937 | + """ |
| 938 | + bt = bootstrapper.Bootstrapper(tmp_context) |
| 939 | + why_snapshot = [ |
| 940 | + (RequirementType.TOP_LEVEL, Requirement("its-hub"), Version("1.0")), |
| 941 | + ] |
| 942 | + item = _make_resolve_item( |
| 943 | + req="flashinfer-python==0.6.8.post1", why_snapshot=why_snapshot |
| 944 | + ) |
| 945 | + err = RuntimeError("Could not resolve any versions for flashinfer-python") |
| 946 | + |
| 947 | + with pytest.raises(RuntimeError, match=r"dependency chain: its-hub==1\.0"): |
| 948 | + try: |
| 949 | + raise err |
| 950 | + except RuntimeError: |
| 951 | + bt._handle_phase_error(item, err) |
| 952 | + |
744 | 953 | # -- Build phase errors in test mode -- |
745 | 954 |
|
746 | 955 | def test_build_phase_test_mode_fallback_success( |
|
0 commit comments