From 756fc4084e1755579e3543b6aca09ab6f3b48be3 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 13 Oct 2023 00:32:05 -0700 Subject: [PATCH] Support `public` dependency configuration with workspace deps This commit updates the processing of `workspace = true` dependencies in the `[dependencies]` section to process the `public` field. Previously this field was ignored and didn't get plumbed through as configured. --- src/cargo/util/toml/mod.rs | 9 ++++++- tests/testsuite/pub_priv.rs | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) 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() +}