Skip to content

Commit d19273c

Browse files
committed
If the only path is a loop then counted as the shortest path.
1 parent cd2e4e3 commit d19273c

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

Diff for: src/cargo/util/graph.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -128,30 +128,32 @@ impl<'s, N: Eq + Ord + Clone + 's, E: Default + Clone + 's> Graph<N, E> {
128128
{
129129
let mut back_link = BTreeMap::new();
130130
let mut queue = VecDeque::from([pkg]);
131-
let mut bottom = None;
131+
let mut last = pkg;
132132

133133
while let Some(p) = queue.pop_front() {
134-
bottom = Some(p);
134+
last = p;
135+
let mut out_edges = true;
135136
for (child, edge) in fn_edge(&self, p) {
136-
bottom = None;
137+
out_edges = false;
137138
back_link.entry(child).or_insert_with(|| {
138139
queue.push_back(child);
139140
(p, edge)
140141
});
141142
}
142-
if bottom.is_some() {
143+
if out_edges {
143144
break;
144145
}
145146
}
146147

147148
let mut result = Vec::new();
148-
let mut next =
149-
bottom.expect("the only path was a cycle, no dependency graph has this shape");
149+
let mut next = last;
150150
while let Some((p, e)) = back_link.remove(&next) {
151151
result.push((next, Some(e)));
152152
next = p;
153153
}
154-
result.push((next, None));
154+
if result.iter().all(|(n, _)| n != &next) {
155+
result.push((next, None));
156+
}
155157
result.reverse();
156158
#[cfg(debug_assertions)]
157159
{
@@ -197,7 +199,7 @@ fn path_to_self() {
197199
// Extracted from #12941
198200
let mut new: Graph<i32, ()> = Graph::new();
199201
new.link(0, 0);
200-
assert_eq!(new.path_to_bottom(&0), vec![(&0, None)]);
202+
assert_eq!(new.path_to_bottom(&0), vec![(&0, Some(&()))]);
201203
}
202204

203205
impl<N: Eq + Ord + Clone, E: Default + Clone> Default for Graph<N, E> {

Diff for: tests/testsuite/test.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -3576,9 +3576,14 @@ fn cyclical_dep_with_missing_feature() {
35763576
p.cargo("check")
35773577
.with_status(101)
35783578
.with_stderr(
3579-
"thread 'main' panicked at src/cargo/util/graph.rs:149:20:
3580-
the only path was a cycle, no dependency graph has this shape
3581-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace",
3579+
"error: failed to select a version for `foo`.
3580+
... required by package `foo v0.1.0 ([..]/foo)`
3581+
versions that meet the requirements `*` are: 0.1.0
3582+
3583+
the package `foo` depends on `foo`, with features: `missing` but `foo` does not have these features.
3584+
3585+
3586+
failed to select a version for `foo` which could resolve this conflict",
35823587
)
35833588
.run();
35843589
}

0 commit comments

Comments
 (0)