Skip to content

Commit 341214d

Browse files
Merge pull request #1241 from raravind007/fix-1214-resolver-error-dependency-chain
Include dependency chain in resolution error messages
2 parents 6370148 + 844deb0 commit 341214d

2 files changed

Lines changed: 260 additions & 1 deletion

File tree

‎src/fromager/bootstrapper/_bootstrapper.py‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from packaging.requirements import Requirement
1515
from packaging.utils import NormalizedName, canonicalize_name
1616
from packaging.version import Version
17+
from resolvelib.resolvers import ResolverException
1718

1819
from .. import (
1920
bootstrap_requirement_resolver,
@@ -897,6 +898,54 @@ def filter_deps_satisfied_by_build_system(
897898
unsatisfied.add(dep)
898899
return unsatisfied
899900

901+
def _dependency_chain_suffix(self, wi: WorkItem) -> str:
902+
"""Describe the requirement chain that pulled in a failed dependency.
903+
904+
Resolution failures are logged with whatever top-level package
905+
happens to be active in the logging context at the time, which can
906+
make an unrelated package look like the culprit when the actual
907+
conflict is several levels down the dependency tree (see #1214).
908+
This spells out the real chain, top-down, so the message is
909+
accurate even when the log prefix is not.
910+
911+
Callers should only invoke this when ``wi.why_snapshot`` is
912+
non-empty (i.e. not a top-level requirement).
913+
"""
914+
chain = " -> ".join(
915+
f"{req.name}=={version}" for _, req, version in wi.why_snapshot
916+
)
917+
return f" (dependency chain: {chain} -> {wi.req})"
918+
919+
def _enrich_resolution_error(self, item: Phase, err: Exception) -> Exception:
920+
"""Attach the dependency chain to a RESOLVE-phase failure.
921+
922+
Returns ``err`` unchanged for non-RESOLVE phases or top-level
923+
requirements (no chain to add). Otherwise returns a *new* exception
924+
of the same shape with the chain appended. The caller
925+
(``_handle_phase_error()``) raises this new exception with no
926+
`from` clause while still inside the original exception's active
927+
handler; Python then sets the new exception's `__context__` to the
928+
original one (keeping it available for traceback/debugging) while
929+
leaving `__cause__` unset, so `__main__._format_exception()` -
930+
which only follows `__cause__` - doesn't print the original message
931+
a second time via "... because ...". See #1243 for the pre-existing,
932+
independent duplication bug in that formatter.
933+
934+
`type(err)(msg)` isn't guaranteed to work for every exception
935+
class, so only the two shapes that actually reach this code path
936+
are reconstructed directly; anything else falls back to a
937+
`RuntimeError` that keeps the original type name in the message.
938+
"""
939+
wi = item.work_item
940+
if not isinstance(item, Resolve) or not wi.why_snapshot:
941+
return err
942+
msg = f"{err}{self._dependency_chain_suffix(wi)}"
943+
if isinstance(err, ResolverException):
944+
return ResolverException(msg)
945+
if isinstance(err, RuntimeError):
946+
return RuntimeError(msg)
947+
return RuntimeError(f"{type(err).__name__}: {msg}")
948+
900949
def _handle_phase_error(
901950
self,
902951
item: Phase,
@@ -910,6 +959,7 @@ def _handle_phase_error(
910959
wi = item.work_item
911960
# Resolution failures: recoverable in test mode and multiple versions mode
912961
if isinstance(item, Resolve):
962+
err = self._enrich_resolution_error(item, err)
913963
if self.test_mode:
914964
self.record_test_mode_failure(wi.req, None, err, "resolution")
915965
if self.multiple_versions:
@@ -928,7 +978,7 @@ def _handle_phase_error(
928978
f"failed during {type(item).phase} phase",
929979
)
930980
return []
931-
raise
981+
raise err
932982

933983
# Test mode: try prebuilt fallback for build-related phases
934984
if self.test_mode:

‎tests/test_bootstrapper_iterative.py‎

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from packaging.requirements import Requirement
2828
from packaging.utils import canonicalize_name
2929
from packaging.version import Version
30+
from resolvelib.resolvers import ResolverException
3031

3132
from fromager import bootstrapper, build_environment
3233
from fromager.bootstrapper._build import Build
@@ -692,6 +693,145 @@ def test_returns_empty_list(self, tmp_context: WorkContext) -> None:
692693
assert item.run(bt) == []
693694

694695

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+
695835
class TestHandlePhaseError:
696836
# -- RESOLVE phase errors --
697837

@@ -741,6 +881,75 @@ def test_resolve_error_in_multiple_versions_mode_continues(
741881
assert key in bt._failed_versions
742882
assert bt._failed_versions[key] is err
743883

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+
744953
# -- Build phase errors in test mode --
745954

746955
def test_build_phase_test_mode_fallback_success(

0 commit comments

Comments
 (0)