diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 08e8f4e31d4..ee4ce743e1e 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -2080,6 +2080,7 @@ pub struct TomlWorkspaceDependency { #[serde(rename = "default_features")] default_features2: Option, optional: Option, + public: Option, /// This is here to provide a way to see the "unused manifest keys" when deserializing #[serde(skip_serializing)] #[serde(flatten)] @@ -2114,11 +2115,12 @@ impl TomlWorkspaceDependency { if let Some(false) = self.default_features.or(self.default_features2) { default_features_msg(name, None, cx); } - if self.optional.is_some() || self.features.is_some() { + if self.optional.is_some() || self.features.is_some() || self.public.is_some() { TomlDependency::Detailed(DetailedTomlDependency { version: Some(s), optional: self.optional, features: self.features.clone(), + public: self.public, ..Default::default() }) } else { @@ -2150,6 +2152,11 @@ impl TomlWorkspaceDependency { } _ => {} } + // Inherit the workspace configuration for `public` unless + // it's explicitly specified for this dependency. + if let Some(public) = self.public { + d.public = Some(public); + } d.add_features(self.features.clone()); d.update_optional(self.optional); TomlDependency::Detailed(d) diff --git a/tests/testsuite/pub_priv.rs b/tests/testsuite/pub_priv.rs index 83c6a49f89a..b2160e0fa11 100644 --- a/tests/testsuite/pub_priv.rs +++ b/tests/testsuite/pub_priv.rs @@ -197,3 +197,52 @@ Caused by: ) .run() } + +#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")] +fn workspace_dep_made_public() { + Package::new("foo1", "0.1.0") + .file("src/lib.rs", "pub struct FromFoo;") + .publish(); + Package::new("foo2", "0.1.0") + .file("src/lib.rs", "pub struct FromFoo;") + .publish(); + Package::new("foo3", "0.1.0") + .file("src/lib.rs", "pub struct FromFoo;") + .publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["public-dependency"] + + [package] + name = "foo" + version = "0.0.1" + + [workspace.dependencies] + foo1 = "0.1.0" + foo2 = { version = "0.1.0", public = true } + foo3 = { version = "0.1.0", public = false } + + [dependencies] + foo1 = { workspace = true, public = true } + foo2 = { workspace = true } + foo3 = { workspace = true, public = true } + "#, + ) + .file( + "src/lib.rs", + " + #![deny(exported_private_dependencies)] + pub fn use_priv1(_: foo1::FromFoo) {} + pub fn use_priv2(_: foo2::FromFoo) {} + pub fn use_priv3(_: foo3::FromFoo) {} + ", + ) + .build(); + + p.cargo("check") + .masquerade_as_nightly_cargo(&["public-dependency"]) + .run() +}