Skip to content

Commit ca0163b

Browse files
committed
package cannot depend on itself as a path
1 parent 1f12b88 commit ca0163b

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

src/cargo/core/resolver/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,15 @@ fn check_cycles(resolve: &Resolve) -> CargoResult<()> {
10091009
for id in resolve.iter() {
10101010
let map = graph.entry(id).or_insert_with(BTreeMap::new);
10111011
for (dep_id, listings) in resolve.deps_not_replaced(id) {
1012+
// We don't want to allow a package to depend on itself as a path.
1013+
// See https://github.com/rust-lang/cargo/issues/9518 for more details.
1014+
if id == dep_id && dep_id.source_id().is_path() {
1015+
anyhow::bail!(
1016+
"cyclic package dependency: package `{}` depends on itself.",
1017+
id,
1018+
);
1019+
}
1020+
10121021
let transitive_dep = listings.iter().find(|d| d.is_transitive());
10131022

10141023
if let Some(transitive_dep) = transitive_dep.cloned() {

tests/testsuite/build.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,9 +1660,34 @@ fn self_dependency() {
16601660
.with_status(101)
16611661
.with_stderr(
16621662
"\
1663-
[ERROR] cyclic package dependency: package `test v0.0.0 ([CWD])` depends on itself. Cycle:
1664-
package `test v0.0.0 ([CWD])`
1665-
... which satisfies path dependency `test` of package `test v0.0.0 ([..])`",
1663+
[ERROR] cyclic package dependency: package `test v0.0.0 ([CWD])` depends on itself.",
1664+
)
1665+
.run();
1666+
}
1667+
1668+
#[cargo_test]
1669+
fn dev_self_dependency() {
1670+
let p = project()
1671+
.file(
1672+
"Cargo.toml",
1673+
r#"
1674+
[project]
1675+
name = "foo"
1676+
version = "0.1.0"
1677+
authors = []
1678+
1679+
[dev-dependencies]
1680+
foo = { path = "." }
1681+
"#,
1682+
)
1683+
.file("src/lib.rs", "")
1684+
.build();
1685+
1686+
p.cargo("check")
1687+
.with_status(101)
1688+
.with_stderr(
1689+
"\
1690+
[ERROR] cyclic package dependency: package `foo v0.1.0 ([CWD])` depends on itself.",
16661691
)
16671692
.run();
16681693
}

tests/testsuite/features2.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -695,18 +695,14 @@ fn cyclical_dev_dep() {
695695
)
696696
.build();
697697

698-
// Old way unifies features.
699-
p.cargo("run true").run();
700-
// dev feature should always be enabled in tests.
701-
p.cargo("test").run();
702-
703-
// New behavior.
704-
switch_to_resolver_2(&p);
705-
// Should decouple main.
706-
p.cargo("run false").run();
707-
708-
// And this should be no different.
709-
p.cargo("test").run();
698+
// TODO: Figure out how to properly fix this test case.
699+
p.cargo("run true")
700+
.with_status(101)
701+
.with_stderr(
702+
"\
703+
[ERROR] cyclic package dependency: package `foo v0.1.0 ([CWD])` depends on itself.",
704+
)
705+
.run();
710706
}
711707

712708
#[cargo_test]

tests/testsuite/test.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3366,7 +3366,13 @@ fn cyclic_dev() {
33663366
.file("tests/foo.rs", "extern crate foo;")
33673367
.build();
33683368

3369-
p.cargo("test --workspace").run();
3369+
p.cargo("test --workspace")
3370+
.with_status(101)
3371+
.with_stderr(
3372+
"\
3373+
[ERROR] cyclic package dependency: package `foo v0.1.0 ([CWD])` depends on itself.",
3374+
)
3375+
.run();
33703376
}
33713377

33723378
#[cargo_test]

0 commit comments

Comments
 (0)