diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 14aa9814831..77e131e1efc 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -1434,6 +1434,7 @@ pub fn extern_args( .require(Feature::public_dependency()) .is_ok() && !dep.public + && unit.target.is_lib() { opts.push("priv"); *unstable_opts = true; diff --git a/tests/testsuite/pub_priv.rs b/tests/testsuite/pub_priv.rs index b2160e0fa11..9792bfd7423 100644 --- a/tests/testsuite/pub_priv.rs +++ b/tests/testsuite/pub_priv.rs @@ -246,3 +246,227 @@ fn workspace_dep_made_public() { .masquerade_as_nightly_cargo(&["public-dependency"]) .run() } + +#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")] +fn allow_priv_in_tests() { + Package::new("priv_dep", "0.1.0") + .file("src/lib.rs", "pub struct FromPriv;") + .publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["public-dependency"] + + [package] + name = "foo" + version = "0.0.1" + + [dependencies] + priv_dep = {version = "0.1.0", public = false} + "#, + ) + .file( + "tests/mod.rs", + " + extern crate priv_dep; + pub fn use_priv(_: priv_dep::FromPriv) {} + ", + ) + .build(); + + p.cargo("check --tests --message-format=short") + .masquerade_as_nightly_cargo(&["public-dependency"]) + .with_stderr( + "\ +[UPDATING] `[..]` index +[DOWNLOADING] crates ... +[DOWNLOADED] priv_dep v0.1.0 ([..]) +[CHECKING] priv_dep v0.1.0 +[CHECKING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run() +} + +#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")] +fn allow_priv_in_benchs() { + Package::new("priv_dep", "0.1.0") + .file("src/lib.rs", "pub struct FromPriv;") + .publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["public-dependency"] + + [package] + name = "foo" + version = "0.0.1" + + [dependencies] + priv_dep = {version = "0.1.0", public = false} + "#, + ) + .file( + "benches/mod.rs", + " + extern crate priv_dep; + pub fn use_priv(_: priv_dep::FromPriv) {} + ", + ) + .build(); + + p.cargo("check --benches --message-format=short") + .masquerade_as_nightly_cargo(&["public-dependency"]) + .with_stderr( + "\ +[UPDATING] `[..]` index +[DOWNLOADING] crates ... +[DOWNLOADED] priv_dep v0.1.0 ([..]) +[CHECKING] priv_dep v0.1.0 +[CHECKING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run() +} + +#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")] +fn allow_priv_in_bins() { + Package::new("priv_dep", "0.1.0") + .file("src/lib.rs", "pub struct FromPriv;") + .publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["public-dependency"] + + [package] + name = "foo" + version = "0.0.1" + + [dependencies] + priv_dep = {version = "0.1.0", public = false} + "#, + ) + .file( + "src/main.rs", + " + extern crate priv_dep; + pub fn use_priv(_: priv_dep::FromPriv) {} + fn main() {} + ", + ) + .build(); + + p.cargo("check --bins --message-format=short") + .masquerade_as_nightly_cargo(&["public-dependency"]) + .with_stderr( + "\ +[UPDATING] `[..]` index +[DOWNLOADING] crates ... +[DOWNLOADED] priv_dep v0.1.0 ([..]) +[CHECKING] priv_dep v0.1.0 +[CHECKING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run() +} + +#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")] +fn allow_priv_in_examples() { + Package::new("priv_dep", "0.1.0") + .file("src/lib.rs", "pub struct FromPriv;") + .publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["public-dependency"] + + [package] + name = "foo" + version = "0.0.1" + + [dependencies] + priv_dep = {version = "0.1.0", public = false} + "#, + ) + .file( + "examples/lib.rs", + " + extern crate priv_dep; + pub fn use_priv(_: priv_dep::FromPriv) {} + fn main() {} + ", + ) + .build(); + + p.cargo("check --examples --message-format=short") + .masquerade_as_nightly_cargo(&["public-dependency"]) + .with_stderr( + "\ +[UPDATING] `[..]` index +[DOWNLOADING] crates ... +[DOWNLOADED] priv_dep v0.1.0 ([..]) +[CHECKING] priv_dep v0.1.0 +[CHECKING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run() +} + +#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")] +fn allow_priv_in_custom_build() { + Package::new("priv_dep", "0.1.0") + .file("src/lib.rs", "pub struct FromPriv;") + .publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["public-dependency"] + + [package] + name = "foo" + version = "0.0.1" + + [build-dependencies] + priv_dep = "0.1.0" + "#, + ) + .file("src/main.rs", "fn main() {}") + .file( + "build.rs", + " + extern crate priv_dep; + pub fn use_priv(_: priv_dep::FromPriv) {} + fn main() {} + ", + ) + .build(); + + p.cargo("check --all-targets --message-format=short") + .masquerade_as_nightly_cargo(&["public-dependency"]) + .with_stderr( + "\ +[UPDATING] `[..]` index +[DOWNLOADING] crates ... +[DOWNLOADED] priv_dep v0.1.0 ([..]) +[COMPILING] priv_dep v0.1.0 +[COMPILING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run() +}