Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A project can not depend on itself as a path dependency #9702

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,15 @@ fn check_cycles(resolve: &Resolve) -> CargoResult<()> {
for id in resolve.iter() {
let map = graph.entry(id).or_insert_with(BTreeMap::new);
for (dep_id, listings) in resolve.deps_not_replaced(id) {
// We don't want to allow a package to depend on itself as a path.
// See https://github.com/rust-lang/cargo/issues/9518 for more details.
if id == dep_id && dep_id.source_id().is_path() {
anyhow::bail!(
"cyclic package dependency: package `{}` depends on itself.",
id,
);
}

let transitive_dep = listings.iter().find(|d| d.is_transitive());

if let Some(transitive_dep) = transitive_dep.cloned() {
Expand Down
31 changes: 28 additions & 3 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,9 +1660,34 @@ fn self_dependency() {
.with_status(101)
.with_stderr(
"\
[ERROR] cyclic package dependency: package `test v0.0.0 ([CWD])` depends on itself. Cycle:
package `test v0.0.0 ([CWD])`
... which satisfies path dependency `test` of package `test v0.0.0 ([..])`",
[ERROR] cyclic package dependency: package `test v0.0.0 ([CWD])` depends on itself.",
)
.run();
}

#[cargo_test]
fn dev_self_dependency() {
let p = project()
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.1.0"
authors = []

[dev-dependencies]
foo = { path = "." }
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("check")
.with_status(101)
.with_stderr(
"\
[ERROR] cyclic package dependency: package `foo v0.1.0 ([CWD])` depends on itself.",
)
.run();
}
Expand Down
20 changes: 8 additions & 12 deletions tests/testsuite/features2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,18 +695,14 @@ fn cyclical_dev_dep() {
)
.build();

// Old way unifies features.
p.cargo("run true").run();
// dev feature should always be enabled in tests.
p.cargo("test").run();

// New behavior.
switch_to_resolver_2(&p);
// Should decouple main.
p.cargo("run false").run();

// And this should be no different.
p.cargo("test").run();
// TODO: Figure out how to properly fix this test case.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how to correctly test this after my changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ehuss @alexcrichton Do you have any suggestions on how to modify that test? Thanks.

p.cargo("run true")
.with_status(101)
.with_stderr(
"\
[ERROR] cyclic package dependency: package `foo v0.1.0 ([CWD])` depends on itself.",
)
.run();
}

#[cargo_test]
Expand Down
8 changes: 7 additions & 1 deletion tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3366,7 +3366,13 @@ fn cyclic_dev() {
.file("tests/foo.rs", "extern crate foo;")
.build();

p.cargo("test --workspace").run();
p.cargo("test --workspace")
.with_status(101)
.with_stderr(
"\
[ERROR] cyclic package dependency: package `foo v0.1.0 ([CWD])` depends on itself.",
)
.run();
}

#[cargo_test]
Expand Down