Skip to content

Commit

Permalink
fix cycles in when traversing graph
Browse files Browse the repository at this point in the history
due to cycles graph traversal would cause maximum recursive depth reached error

Signed-off-by: Shubh Bapna <[email protected]>
  • Loading branch information
Shubh Bapna committed Dec 6, 2024
1 parent f1bfa8b commit 15bc9e5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/fromager/dependency_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def get_dependency_edges(
visited = set()
for edge in self._depth_first_traversal(
self.nodes[ROOT].children,
set(),
match_dep_types=match_dep_types,
):
if edge.destination_node.key not in visited:
Expand Down Expand Up @@ -278,13 +279,17 @@ def get_install_dependency_versions(

def _depth_first_traversal(
self,
start_node: list[DependencyEdge],
start_edges: list[DependencyEdge],
visited: set[str],
match_dep_types: list[RequirementType] | None = None,
) -> typing.Iterable[DependencyEdge]:
for edge in start_node:
for edge in start_edges:
if edge.destination_node.key in visited:
continue
if match_dep_types and edge.req_type not in match_dep_types:
continue
visited.add(edge.destination_node.key)
yield edge
yield from self._depth_first_traversal(
edge.destination_node.children, match_dep_types
edge.destination_node.children, visited, match_dep_types
)
38 changes: 38 additions & 0 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,41 @@ def test_get_install_dependencies():
for node in graph.get_install_dependencies()
]
assert install_nodes == ["a==2.0", "d==6.0", "b==3.0", "e==6.0"]


def test_cycles_get_install_dependencies():
graph = dependency_graph.DependencyGraph.from_dict(raw_graph)
# create cycle: a depends on d and d depends on a
graph.add_dependency(
parent_name=canonicalize_name("a"),
parent_version=Version("2.0"),
req_type=requirements_file.RequirementType.INSTALL,
req=Requirement("d>=4.0"),
req_version=Version("6.0"),
download_url="url for d",
)

graph.add_dependency(
parent_name=canonicalize_name("d"),
parent_version=Version("6.0"),
req_type=requirements_file.RequirementType.INSTALL,
req=Requirement("a<=2.0"),
req_version=Version("2.0"),
download_url="url for a",
)

# add another duplicate toplevel
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=requirements_file.RequirementType.TOP_LEVEL,
req=Requirement("a<=2.0"),
req_version=Version("2.0"),
download_url="url for a",
)

install_nodes = [
f"{node.to_dict()['canonicalized_name']}=={node.to_dict()['version']}"
for node in graph.get_install_dependencies()
]
assert install_nodes == ["a==2.0", "d==6.0"]

0 comments on commit 15bc9e5

Please sign in to comment.