Skip to content

Commit

Permalink
Prevent 'public' specifier from being used on non-Normal dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron1011 committed Apr 26, 2019
1 parent 8c8824f commit df4d209
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ impl Dependency {

/// Sets whether the dependency is public.
pub fn set_public(&mut self, public: bool) -> &mut Dependency {
// Setting 'public' only makes sense for normal dependencies
assert_eq!(self.kind(), Kind::Normal);
Rc::make_mut(&mut self.inner).public = public;
self
}
Expand Down
5 changes: 5 additions & 0 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,11 @@ impl DetailedTomlDependency {

if let Some(p) = self.public {
cx.features.require(Feature::public_dependency())?;

if dep.kind() != Kind::Normal {
bail!("'public' specifier can only be used on regular dependencies, not {:?} dependencies", dep.kind());
}

dep.set_public(p);
}
Ok(dep)
Expand Down
44 changes: 44 additions & 0 deletions tests/testsuite/pub_priv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,47 @@ consider adding `cargo-features = [\"public-dependency\"]` to the manifest
)
.run()
}


#[test]
fn pub_dev_dependency() {
Package::new("pub_dep", "0.1.0")
.file("src/lib.rs", "pub struct FromPub;")
.publish();

let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["public-dependency"]
[package]
name = "foo"
version = "0.0.1"
[dev-dependencies]
pub_dep = {version = "0.1.0", public = true}
"#,
)
.file(
"src/lib.rs",
"
extern crate pub_dep;
pub fn use_pub(_: pub_dep::FromPub) {}
",
)
.build();

p.cargo("build --message-format=short")
.masquerade_as_nightly_cargo()
.with_status(101)
.with_stderr(
"\
error: failed to parse manifest at `[..]`
Caused by:
'public' specifier can only be used on regular dependencies, not Development dependencies
",
)
.run()
}

0 comments on commit df4d209

Please sign in to comment.