|
1 | 1 | use super::super::build_types::*; |
2 | 2 | use crate::helpers; |
3 | | -use ahash::AHashSet; |
| 3 | +use std::collections::{HashMap, VecDeque}; |
4 | 4 |
|
5 | 5 | pub fn find(modules: &Vec<(&String, &Module)>) -> Vec<String> { |
6 | | - let mut visited: AHashSet<String> = AHashSet::new(); |
7 | | - let mut stack: Vec<String> = vec![]; |
| 6 | + // If a cycle was found, find the shortest cycle using BFS |
| 7 | + find_shortest_cycle(modules) |
| 8 | +} |
8 | 9 |
|
9 | | - // we want to sort the module names so that we always return the same |
10 | | - // dependency cycle (there can be more than one) |
11 | | - let mut module_names = modules |
12 | | - .iter() |
13 | | - .map(|(name, _)| name.to_string()) |
14 | | - .collect::<Vec<String>>(); |
| 10 | +fn find_shortest_cycle(modules: &Vec<(&String, &Module)>) -> Vec<String> { |
| 11 | + let mut shortest_cycle: Vec<String> = Vec::new(); |
15 | 12 |
|
16 | | - module_names.sort(); |
17 | | - for module_name in module_names { |
18 | | - if find_dependency_cycle_helper(&module_name, modules, &mut visited, &mut stack) { |
19 | | - return stack; |
| 13 | + // Build a graph representation for easier traversal |
| 14 | + let mut graph: HashMap<String, Vec<String>> = HashMap::new(); |
| 15 | + for (name, module) in modules { |
| 16 | + let deps = module.deps.iter().cloned().collect(); |
| 17 | + graph.insert(name.to_string(), deps); |
| 18 | + } |
| 19 | + |
| 20 | + // Try BFS from each node to find the shortest cycle |
| 21 | + for start_node in graph.keys() { |
| 22 | + let start = start_node.clone(); |
| 23 | + if let Some(cycle) = find_cycle_bfs(&start, &graph) { |
| 24 | + if shortest_cycle.is_empty() || cycle.len() < shortest_cycle.len() { |
| 25 | + shortest_cycle = cycle; |
| 26 | + } |
20 | 27 | } |
21 | | - visited.clear(); |
22 | | - stack.clear(); |
23 | 28 | } |
24 | | - stack |
| 29 | + |
| 30 | + shortest_cycle |
25 | 31 | } |
26 | 32 |
|
27 | | -fn find_dependency_cycle_helper( |
28 | | - module_name: &String, |
29 | | - modules: &Vec<(&String, &Module)>, |
30 | | - visited: &mut AHashSet<String>, |
31 | | - stack: &mut Vec<String>, |
32 | | -) -> bool { |
33 | | - if let Some(module) = modules |
34 | | - .iter() |
35 | | - .find(|(name, _)| *name == module_name) |
36 | | - .map(|(_, module)| module) |
37 | | - { |
38 | | - visited.insert(module_name.to_string()); |
39 | | - // if the module is a mlmap (namespace), we don't want to show this in the path |
40 | | - // because the namespace is not a module the user created, so only add source files |
41 | | - // to the stack |
42 | | - if let SourceType::SourceFile(_) = module.source_type { |
43 | | - stack.push(module_name.to_string()) |
44 | | - } |
45 | | - for dep in &module.deps { |
46 | | - if !visited.contains(dep) { |
47 | | - if find_dependency_cycle_helper(dep, modules, visited, stack) { |
48 | | - return true; |
| 33 | +fn find_cycle_bfs(start: &String, graph: &HashMap<String, Vec<String>>) -> Option<Vec<String>> { |
| 34 | + // Use a BFS to find the shortest cycle |
| 35 | + let mut queue = VecDeque::new(); |
| 36 | + // Store node -> (distance, parent) |
| 37 | + let mut visited: HashMap<String, (usize, Option<String>)> = HashMap::new(); |
| 38 | + |
| 39 | + // Initialize with start node |
| 40 | + visited.insert(start.clone(), (0, None)); |
| 41 | + queue.push_back(start.clone()); |
| 42 | + |
| 43 | + while let Some(current) = queue.pop_front() { |
| 44 | + let (dist, _) = *visited.get(¤t).unwrap(); |
| 45 | + |
| 46 | + // Check all neighbors |
| 47 | + if let Some(neighbors) = graph.get(¤t) { |
| 48 | + for neighbor in neighbors { |
| 49 | + // If we found the start node again, we have a cycle |
| 50 | + if neighbor == start { |
| 51 | + // Reconstruct the cycle |
| 52 | + let mut path = Vec::new(); |
| 53 | + path.push(start.clone()); |
| 54 | + |
| 55 | + // Backtrack from current to start using parent pointers |
| 56 | + let mut curr = current.clone(); |
| 57 | + while curr != *start { |
| 58 | + path.push(curr.clone()); |
| 59 | + curr = visited.get(&curr).unwrap().1.clone().unwrap(); |
| 60 | + } |
| 61 | + |
| 62 | + return Some(path); |
| 63 | + } |
| 64 | + |
| 65 | + // If not visited, add to queue |
| 66 | + if !visited.contains_key(neighbor) { |
| 67 | + visited.insert(neighbor.clone(), (dist + 1, Some(current.clone()))); |
| 68 | + queue.push_back(neighbor.clone()); |
49 | 69 | } |
50 | | - } else if stack.contains(dep) { |
51 | | - stack.push(dep.to_string()); |
52 | | - return true; |
53 | 70 | } |
54 | 71 | } |
55 | | - // because we only pushed source files to the stack, we also only need to |
56 | | - // pop these from the stack if we don't find a dependency cycle |
57 | | - if let SourceType::SourceFile(_) = module.source_type { |
58 | | - let _ = stack.pop(); |
59 | | - } |
60 | | - return false; |
61 | 72 | } |
62 | | - false |
| 73 | + |
| 74 | + None |
63 | 75 | } |
64 | 76 |
|
65 | 77 | pub fn format(cycle: &[String]) -> String { |
| 78 | + let mut cycle = cycle.to_vec(); |
| 79 | + cycle.reverse(); |
| 80 | + // add the first module to the end of the cycle |
| 81 | + cycle.push(cycle[0].clone()); |
| 82 | + |
66 | 83 | cycle |
67 | 84 | .iter() |
68 | 85 | .map(|s| helpers::format_namespaced_module_name(s)) |
69 | 86 | .collect::<Vec<String>>() |
70 | | - .join(" -> ") |
| 87 | + .join("\n → ") |
71 | 88 | } |
0 commit comments