Skip to content

Commit 3945cb9

Browse files
committed
feat: Add "-Zpublic-dependency" for public-dependency feature
1 parent 98079d9 commit 3945cb9

File tree

7 files changed

+182
-20
lines changed

7 files changed

+182
-20
lines changed

src/cargo/core/compiler/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1506,15 +1506,14 @@ pub fn extern_args(
15061506
|dep: &UnitDep, extern_crate_name: InternedString, noprelude: bool| -> CargoResult<()> {
15071507
let mut value = OsString::new();
15081508
let mut opts = Vec::new();
1509-
if unit
1509+
let is_public_dependency_enabled = unit
15101510
.pkg
15111511
.manifest()
15121512
.unstable_features()
15131513
.require(Feature::public_dependency())
15141514
.is_ok()
1515-
&& !dep.public
1516-
&& unit.target.is_lib()
1517-
{
1515+
|| build_runner.bcx.gctx.cli_unstable().public_dependency;
1516+
if !dep.public && unit.target.is_lib() && is_public_dependency_enabled {
15181517
opts.push("priv");
15191518
*unstable_opts = true;
15201519
}

src/cargo/core/features.rs

+2
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ unstable_cli_options!(
765765
panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"),
766766
precise_pre_release: bool = ("Enable pre-release versions to be selected with `update --precise`"),
767767
profile_rustflags: bool = ("Enable the `rustflags` option in profiles in .cargo/config.toml file"),
768+
public_dependency: bool = ("Respect a dependency's `public` field in Cargo.toml to control public/private dependencies"),
768769
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
769770
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
770771
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
@@ -1141,6 +1142,7 @@ impl CliUnstable {
11411142
"mtime-on-use" => self.mtime_on_use = parse_empty(k, v)?,
11421143
"no-index-update" => self.no_index_update = parse_empty(k, v)?,
11431144
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
1145+
"public-dependency" => self.public_dependency = parse_empty(k, v)?,
11441146
"profile-rustflags" => self.profile_rustflags = parse_empty(k, v)?,
11451147
"precise-pre-release" => self.precise_pre_release = parse_empty(k, v)?,
11461148
"trim-paths" => self.trim_paths = parse_empty(k, v)?,

src/cargo/ops/cargo_package.rs

+1
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ fn run_verify(
911911
.unstable_features()
912912
.require(Feature::public_dependency())
913913
.is_ok()
914+
|| ws.gctx().cli_unstable().public_dependency
914915
{
915916
// FIXME: Turn this on at some point in the future
916917
//Some(vec!["-D exported_private_dependencies".to_string()])

src/cargo/util/toml/mod.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,26 @@ pub fn to_real_manifest(
727727
v.unused_keys(),
728728
manifest_gctx.warnings,
729729
);
730+
let mut resolved = resolved;
731+
if !manifest_gctx.gctx.cli_unstable().public_dependency
732+
&& manifest_gctx.gctx.nightly_features_allowed
733+
{
734+
if let manifest::TomlDependency::Detailed(ref mut d) = resolved {
735+
if d.public.is_some() {
736+
d.public = None;
737+
if !manifest_gctx
738+
.features
739+
.require(Feature::public_dependency())
740+
.is_ok()
741+
&& !manifest_gctx.gctx.cli_unstable().public_dependency
742+
{
743+
manifest_gctx.warnings.push(format!(
744+
"Ignoring `public` on dependency {name}. Pass `-Zpublic-dependency` to enable support for it", name = &dep.name_in_toml()
745+
))
746+
}
747+
}
748+
}
749+
}
730750
manifest_gctx.deps.push(dep);
731751
deps.insert(
732752
n.clone(),
@@ -1992,9 +2012,13 @@ fn detailed_dep_to_dependency<P: ResolveToPath + Clone>(
19922012
}
19932013

19942014
if let Some(p) = orig.public {
1995-
manifest_gctx
1996-
.features
1997-
.require(Feature::public_dependency())?;
2015+
let public_feature = manifest_gctx.features.require(Feature::public_dependency());
2016+
if !public_feature.is_ok()
2017+
&& (!manifest_gctx.gctx.cli_unstable().public_dependency
2018+
&& !manifest_gctx.gctx.nightly_features_allowed)
2019+
{
2020+
public_feature?;
2021+
}
19982022

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

tests/build-std/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn enable_build_std(e: &mut Execs, arg: Option<&str>) {
3333
Some(s) => format!("-Zbuild-std={}", s),
3434
None => "-Zbuild-std".to_string(),
3535
};
36-
e.arg(arg);
36+
e.arg(arg).arg("-Zpublic-dependency");
3737
e.masquerade_as_nightly_cargo(&["build-std"]);
3838
}
3939

tests/testsuite/cargo/z_help/stdout.log

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Available unstable (nightly-only) flags:
2525
-Z panic-abort-tests Enable support to run tests with -Cpanic=abort
2626
-Z precise-pre-release Enable pre-release versions to be selected with `update --precise`
2727
-Z profile-rustflags Enable the `rustflags` option in profiles in .cargo/config.toml file
28+
-Z public-dependency Respect a dependency's `public` field in Cargo.toml to control public/private dependencies
2829
-Z publish-timeout Enable the `publish.timeout` key in .cargo/config.toml file
2930
-Z rustdoc-map Allow passing external documentation mappings to rustdoc
3031
-Z rustdoc-scrape-examples Allows Rustdoc to scrape code examples from reverse-dependencies

tests/testsuite/pub_priv.rs

+147-12
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,15 @@ fn requires_feature() {
136136

137137
p.cargo("check --message-format=short")
138138
.masquerade_as_nightly_cargo(&["public-dependency"])
139-
.with_status(101)
140139
.with_stderr(
141140
"\
142-
error: failed to parse manifest at `[..]`
143-
144-
Caused by:
145-
feature `public-dependency` is required
146-
147-
The package requires the Cargo feature called `public-dependency`, \
148-
but that feature is not stabilized in this version of Cargo (1.[..]).
149-
Consider adding `cargo-features = [\"public-dependency\"]` to the top of Cargo.toml \
150-
(above the [package] table) to tell Cargo you are opting in to use this unstable feature.
151-
See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#public-dependency \
152-
for more information about the status of this feature.
141+
[WARNING] Ignoring `public` on dependency pub_dep. Pass `-Zpublic-dependency` to enable support for it
142+
[UPDATING] `[..]` index
143+
[DOWNLOADING] crates ...
144+
[DOWNLOADED] pub_dep v0.1.0 ([..])
145+
[CHECKING] pub_dep v0.1.0
146+
[CHECKING] foo v0.0.1 ([CWD])
147+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]
153148
",
154149
)
155150
.run()
@@ -198,6 +193,47 @@ Caused by:
198193
.run()
199194
}
200195

196+
#[cargo_test]
197+
fn pub_dev_dependency_without_feature() {
198+
Package::new("pub_dep", "0.1.0")
199+
.file("src/lib.rs", "pub struct FromPub;")
200+
.publish();
201+
202+
let p = project()
203+
.file(
204+
"Cargo.toml",
205+
r#"
206+
[package]
207+
name = "foo"
208+
version = "0.0.1"
209+
210+
[dev-dependencies]
211+
pub_dep = {version = "0.1.0", public = true}
212+
"#,
213+
)
214+
.file(
215+
"src/lib.rs",
216+
"
217+
extern crate pub_dep;
218+
pub fn use_pub(_: pub_dep::FromPub) {}
219+
",
220+
)
221+
.build();
222+
223+
p.cargo("check --message-format=short")
224+
.masquerade_as_nightly_cargo(&["public-dependency"])
225+
.with_status(101)
226+
.with_stderr(
227+
"\
228+
[ERROR] failed to parse manifest at `[..]`
229+
230+
Caused by:
231+
'public' specifier can only be used on regular dependencies, not Development dependencies
232+
",
233+
)
234+
.run()
235+
}
236+
201237
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
202238
fn workspace_pub_disallowed() {
203239
Package::new("foo1", "0.1.0")
@@ -534,3 +570,102 @@ fn publish_package_with_public_dependency() {
534570
)
535571
.run()
536572
}
573+
574+
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
575+
fn verify_mix_cargo_feature_z() {
576+
Package::new("dep", "0.1.0")
577+
.file("src/lib.rs", "pub struct FromDep;")
578+
.publish();
579+
Package::new("priv_dep", "0.1.0")
580+
.file("src/lib.rs", "pub struct FromPriv;")
581+
.publish();
582+
Package::new("pub_dep", "0.1.0")
583+
.file("src/lib.rs", "pub struct FromPub;")
584+
.publish();
585+
let p = project()
586+
.file(
587+
"Cargo.toml",
588+
r#"
589+
cargo-features = ["public-dependency"]
590+
[package]
591+
name = "foo"
592+
version = "0.0.1"
593+
594+
[dependencies]
595+
dep = "0.1.0"
596+
priv_dep = {version = "0.1.0", public = false}
597+
pub_dep = {version = "0.1.0", public = true}
598+
"#,
599+
)
600+
.file(
601+
"src/lib.rs",
602+
"
603+
extern crate dep;
604+
extern crate priv_dep;
605+
extern crate pub_dep;
606+
pub fn use_dep(_: dep::FromDep) {}
607+
pub fn use_priv(_: priv_dep::FromPriv) {}
608+
pub fn use_pub(_: pub_dep::FromPub) {}
609+
",
610+
)
611+
.build();
612+
613+
p.cargo("check -Zpublic-dependency --message-format=short")
614+
.masquerade_as_nightly_cargo(&["public-dependency"])
615+
.with_stderr_contains(
616+
"\
617+
src/lib.rs:5:13: warning: type `FromDep` from private dependency 'dep' in public interface
618+
src/lib.rs:6:13: warning: type `FromPriv` from private dependency 'priv_dep' in public interface
619+
",
620+
)
621+
.run();
622+
}
623+
624+
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
625+
fn verify_z_public_dependency() {
626+
Package::new("dep", "0.1.0")
627+
.file("src/lib.rs", "pub struct FromDep;")
628+
.publish();
629+
Package::new("priv_dep", "0.1.0")
630+
.file("src/lib.rs", "pub struct FromPriv;")
631+
.publish();
632+
Package::new("pub_dep", "0.1.0")
633+
.file("src/lib.rs", "pub struct FromPub;")
634+
.publish();
635+
let p = project()
636+
.file(
637+
"Cargo.toml",
638+
r#"
639+
[package]
640+
name = "foo"
641+
version = "0.0.1"
642+
643+
[dependencies]
644+
dep = "0.1.0"
645+
priv_dep = {version = "0.1.0", public = false}
646+
pub_dep = {version = "0.1.0", public = true}
647+
"#,
648+
)
649+
.file(
650+
"src/lib.rs",
651+
"
652+
extern crate dep;
653+
extern crate priv_dep;
654+
extern crate pub_dep;
655+
pub fn use_dep(_: dep::FromDep) {}
656+
pub fn use_priv(_: priv_dep::FromPriv) {}
657+
pub fn use_pub(_: pub_dep::FromPub) {}
658+
",
659+
)
660+
.build();
661+
662+
p.cargo("check -Zpublic-dependency --message-format=short")
663+
.masquerade_as_nightly_cargo(&["public-dependency"])
664+
.with_stderr_contains(
665+
"\
666+
src/lib.rs:5:13: warning: type `FromDep` from private dependency 'dep' in public interface
667+
src/lib.rs:6:13: warning: type `FromPriv` from private dependency 'priv_dep' in public interface
668+
",
669+
)
670+
.run();
671+
}

0 commit comments

Comments
 (0)