Skip to content

Commit

Permalink
If the only path is a loop then counted as the shortest path.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Nov 14, 2023
1 parent cd2e4e3 commit d19273c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
18 changes: 10 additions & 8 deletions src/cargo/util/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,32 @@ impl<'s, N: Eq + Ord + Clone + 's, E: Default + Clone + 's> Graph<N, E> {
{
let mut back_link = BTreeMap::new();
let mut queue = VecDeque::from([pkg]);
let mut bottom = None;
let mut last = pkg;

while let Some(p) = queue.pop_front() {
bottom = Some(p);
last = p;
let mut out_edges = true;
for (child, edge) in fn_edge(&self, p) {
bottom = None;
out_edges = false;
back_link.entry(child).or_insert_with(|| {
queue.push_back(child);
(p, edge)
});
}
if bottom.is_some() {
if out_edges {
break;
}
}

let mut result = Vec::new();
let mut next =
bottom.expect("the only path was a cycle, no dependency graph has this shape");
let mut next = last;
while let Some((p, e)) = back_link.remove(&next) {
result.push((next, Some(e)));
next = p;
}
result.push((next, None));
if result.iter().all(|(n, _)| n != &next) {
result.push((next, None));
}
result.reverse();
#[cfg(debug_assertions)]
{
Expand Down Expand Up @@ -197,7 +199,7 @@ fn path_to_self() {
// Extracted from #12941
let mut new: Graph<i32, ()> = Graph::new();
new.link(0, 0);
assert_eq!(new.path_to_bottom(&0), vec![(&0, None)]);
assert_eq!(new.path_to_bottom(&0), vec![(&0, Some(&()))]);
}

impl<N: Eq + Ord + Clone, E: Default + Clone> Default for Graph<N, E> {
Expand Down
11 changes: 8 additions & 3 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3576,9 +3576,14 @@ fn cyclical_dep_with_missing_feature() {
p.cargo("check")
.with_status(101)
.with_stderr(
"thread 'main' panicked at src/cargo/util/graph.rs:149:20:
the only path was a cycle, no dependency graph has this shape
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace",
"error: failed to select a version for `foo`.
... required by package `foo v0.1.0 ([..]/foo)`
versions that meet the requirements `*` are: 0.1.0
the package `foo` depends on `foo`, with features: `missing` but `foo` does not have these features.
failed to select a version for `foo` which could resolve this conflict",
)
.run();
}
Expand Down

0 comments on commit d19273c

Please sign in to comment.