Skip to content

Commit

Permalink
Support public dependency configuration with workspace deps
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexcrichton committed Oct 13, 2023
1 parent eb2b3f9 commit 756fc40
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,7 @@ pub struct TomlWorkspaceDependency {
#[serde(rename = "default_features")]
default_features2: Option<bool>,
optional: Option<bool>,
public: Option<bool>,
/// This is here to provide a way to see the "unused manifest keys" when deserializing
#[serde(skip_serializing)]
#[serde(flatten)]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions tests/testsuite/pub_priv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 756fc40

Please sign in to comment.