Skip to content

Commit 59b9cc1

Browse files
committed
Auto merge of #16871 - Veykril:dev-dependency-cycles, r=Veykril
fix: Skip problematic cyclic dev-dependencies Implements a workaround for #14167, notably it does not implement the ideas surfaced in the issue, but takes a simpler to implement approach (and one that is more consistent). Effectively, all this does is discard dev-dependency edges that go from a workspace library target to another workspace library target. This means, using a dev-dependency to another workspace member inside unit tests will always fail to resolve for r-a now, (instead of being order dependent and causing problems elsewhere) while things will work out fine in integration tests, benches, examples etc. This effectively acknowledges package cycles to be okay, but crate graph cycles to be invalid: Quoting #14167 (comment) > Though, if you have “package cycle” in integration tests, you’d have “crate cycle” in unit test. We disallow the latter here, while continuing to support the former (What's missing is to supress diagnostics for such unit tests, though not doing so might be a good deterrent, making devs avoid the pattern altogether)
2 parents d3eeadc + 76fb73a commit 59b9cc1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

crates/project-model/src/workspace.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,24 @@ fn cargo_to_crate_graph(
10911091
continue;
10921092
}
10931093

1094+
// If the dependency is a dev-dependency with both crates being member libraries of
1095+
// the workspace we discard the edge. The reason can be read up on in
1096+
// https://github.com/rust-lang/rust-analyzer/issues/14167
1097+
// but in short, such an edge usually causes some form of cycle in the crate graph
1098+
// wrt to unit tests. Something we cannot reasonable support.
1099+
if dep.kind == DepKind::Dev
1100+
&& matches!(kind, TargetKind::Lib { .. })
1101+
&& cargo[dep.pkg].is_member
1102+
&& cargo[pkg].is_member
1103+
{
1104+
tracing::warn!(
1105+
"Discarding dev-dependency edge from library target `{}` to library target `{}` to prevent potential cycles",
1106+
cargo[dep.pkg].name,
1107+
cargo[pkg].name
1108+
);
1109+
continue;
1110+
}
1111+
10941112
add_dep(crate_graph, from, name.clone(), to)
10951113
}
10961114
}

0 commit comments

Comments
 (0)