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

Limit exported-private-dependencies lints to libraries #13135

Merged
merged 3 commits into from
Dec 11, 2023
Merged
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
1 change: 1 addition & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,7 @@ pub fn extern_args(
.require(Feature::public_dependency())
.is_ok()
&& !dep.public
&& unit.target.is_lib()
linyihai marked this conversation as resolved.
Show resolved Hide resolved
{
opts.push("priv");
*unstable_opts = true;
Expand Down
224 changes: 224 additions & 0 deletions tests/testsuite/pub_priv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Another one we should cover is build scripts.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like proc-macros are lib's but I'm assuming those shouldn't be checked with exported_private_dependencies.

Example libs are an odd case.

I'm more mixed about crate types, like cdylibs.

@ehuss any thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another one we should cover is build scripts.

OK, add a test for custom build script

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be good if all libs warn just like a normal rlib. Any lib type can expose types within item signatures, and it seems like it would be good hygiene to be explicit about that.

For proc-macros, it is not possible to have public items (other than the proc-macros themselves), so it is not possible to "leak" anything AFAIK. But I wouldn't go out of my way to try to disable it there.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll go for that. We can always re-visit.

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()
}