Skip to content

Commit 8363559

Browse files
authored
Suppress metadata warnings for non–crates.io publishable packages (#16241)
### What does this PR try to resolve? Related issue: #4840 With this change, Cargo still performs full metadata checks for all packages to preserve validation consistency, but it suppresses warnings for crates that are not publishable to crates.io, such as those with `publish = false` or custom registries (`publish = ["my-registry"]`). ### How to test and review this PR? Unit tests added in `tests/testsuite/package.rs`, can be tested locally by running `cargo test --test package`.
2 parents cd90026 + f73f174 commit 8363559

File tree

4 files changed

+96
-36
lines changed

4 files changed

+96
-36
lines changed

src/cargo/ops/cargo_package/mod.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ fn prepare_archive(
483483
src.load()?;
484484

485485
if opts.check_metadata {
486-
check_metadata(pkg, gctx)?;
486+
check_metadata(pkg, opts.reg_or_index.as_ref(), gctx)?;
487487
}
488488

489489
if !pkg.manifest().exclude().is_empty() && !pkg.manifest().include().is_empty() {
@@ -808,7 +808,11 @@ fn build_lock(
808808

809809
// Checks that the package has some piece of metadata that a human can
810810
// use to tell what the package is about.
811-
fn check_metadata(pkg: &Package, gctx: &GlobalContext) -> CargoResult<()> {
811+
fn check_metadata(
812+
pkg: &Package,
813+
reg_or_index: Option<&RegistryOrIndex>,
814+
gctx: &GlobalContext,
815+
) -> CargoResult<()> {
812816
let md = pkg.manifest().metadata();
813817

814818
let mut missing = vec![];
@@ -829,20 +833,29 @@ fn check_metadata(pkg: &Package, gctx: &GlobalContext) -> CargoResult<()> {
829833
);
830834

831835
if !missing.is_empty() {
832-
let mut things = missing[..missing.len() - 1].join(", ");
833-
// `things` will be empty if and only if its length is 1 (i.e., the only case
834-
// to have no `or`).
835-
if !things.is_empty() {
836-
things.push_str(" or ");
836+
// Only warn if publishing to crates.io based on resolved registry
837+
let should_warn = match reg_or_index {
838+
Some(RegistryOrIndex::Registry(reg_name)) => reg_name == CRATES_IO_REGISTRY,
839+
None => true, // Default is crates.io
840+
Some(RegistryOrIndex::Index(_)) => false, // Custom index, not crates.io
841+
};
842+
843+
if should_warn {
844+
let mut things = missing[..missing.len() - 1].join(", ");
845+
// `things` will be empty if and only if its length is 1 (i.e., the only case
846+
// to have no `or`).
847+
if !things.is_empty() {
848+
things.push_str(" or ");
849+
}
850+
things.push_str(missing.last().unwrap());
851+
852+
gctx.shell().print_report(&[
853+
Level::WARNING.secondary_title(format!("manifest has no {things}"))
854+
.element(Level::NOTE.message("see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info"))
855+
],
856+
false
857+
)?
837858
}
838-
things.push_str(missing.last().unwrap());
839-
840-
gctx.shell().print_report(&[
841-
Level::WARNING.secondary_title(format!("manifest has no {things}"))
842-
.element(Level::NOTE.message("see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info"))
843-
],
844-
false
845-
)?
846859
}
847860

848861
Ok(())

tests/testsuite/alt_registry.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,6 @@ fn publish_with_registry_dependency() {
350350
p.cargo("publish --registry alternative")
351351
.with_stderr_data(str![[r#"
352352
[UPDATING] `alternative` index
353-
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository
354-
|
355-
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
356353
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
357354
[UPDATING] `alternative` index
358355
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
@@ -518,9 +515,6 @@ fn publish_to_alt_registry() {
518515
p.cargo("publish --registry alternative")
519516
.with_stderr_data(str![[r#"
520517
[UPDATING] `alternative` index
521-
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository
522-
|
523-
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
524518
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
525519
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
526520
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
@@ -597,9 +591,6 @@ fn publish_with_crates_io_dep() {
597591
p.cargo("publish --registry alternative")
598592
.with_stderr_data(str![[r#"
599593
[UPDATING] `alternative` index
600-
[WARNING] manifest has no documentation, homepage or repository
601-
|
602-
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
603594
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
604595
[UPDATING] `dummy-registry` index
605596
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)

tests/testsuite/package.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7739,3 +7739,71 @@ Caused by:
77397739
"#]])
77407740
.run();
77417741
}
7742+
7743+
#[cargo_test]
7744+
fn publish_to_crates_io_warns() {
7745+
let p = project()
7746+
.file(
7747+
"Cargo.toml",
7748+
r#"
7749+
[package]
7750+
name = "foo"
7751+
version = "0.1.0"
7752+
description = "foo"
7753+
edition = "2015"
7754+
"#,
7755+
)
7756+
.file("src/main.rs", "fn main() {}")
7757+
.build();
7758+
7759+
p.cargo(&format!("publish --dry-run"))
7760+
.with_stderr_data(str![[r#"
7761+
[UPDATING] crates.io index
7762+
[WARNING] manifest has no license, license-file, documentation, homepage or repository
7763+
|
7764+
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
7765+
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
7766+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
7767+
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
7768+
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
7769+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
7770+
[UPLOADING] foo v0.1.0 ([ROOT]/foo)
7771+
[WARNING] aborting upload due to dry run
7772+
7773+
"#]])
7774+
.run();
7775+
}
7776+
7777+
#[cargo_test]
7778+
fn publish_to_alt_registry_warns() {
7779+
let _alt_reg = registry::RegistryBuilder::new().alternative().build();
7780+
7781+
let p = project()
7782+
.file(
7783+
"Cargo.toml",
7784+
r#"
7785+
[package]
7786+
name = "foo"
7787+
version = "0.1.0"
7788+
description = "foo"
7789+
edition = "2015"
7790+
publish = ["alternative"]
7791+
"#,
7792+
)
7793+
.file("src/main.rs", "fn main() {}")
7794+
.build();
7795+
7796+
p.cargo("publish --dry-run --registry alternative")
7797+
.with_stderr_data(str![[r#"
7798+
[UPDATING] `alternative` index
7799+
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
7800+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
7801+
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
7802+
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
7803+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
7804+
[UPLOADING] foo v0.1.0 ([ROOT]/foo)
7805+
[WARNING] aborting upload due to dry run
7806+
7807+
"#]])
7808+
.run();
7809+
}

tests/testsuite/publish.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,6 @@ fn simple_publish_with_http() {
211211
.with_stderr_data(str![[r#"
212212
[WARNING] `cargo publish --token` is deprecated in favor of using `cargo login` and environment variables
213213
[UPDATING] `dummy-registry` index
214-
[WARNING] manifest has no documentation, homepage or repository
215-
|
216-
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
217214
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
218215
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
219216
[UPLOADING] foo v0.0.1 ([ROOT]/foo)
@@ -255,9 +252,6 @@ fn simple_publish_with_asymmetric() {
255252
.masquerade_as_nightly_cargo(&["asymmetric-token"])
256253
.with_stderr_data(str![[r#"
257254
[UPDATING] `dummy-registry` index
258-
[WARNING] manifest has no documentation, homepage or repository
259-
|
260-
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
261255
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
262256
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
263257
[UPLOADING] foo v0.0.1 ([ROOT]/foo)
@@ -359,9 +353,6 @@ fn simple_with_index() {
359353
.with_stderr_data(str![[r#"
360354
[WARNING] `cargo publish --token` is deprecated in favor of using `cargo login` and environment variables
361355
[UPDATING] `[ROOT]/registry` index
362-
[WARNING] manifest has no documentation, homepage or repository
363-
|
364-
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
365356
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
366357
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
367358
[UPLOADING] foo v0.0.1 ([ROOT]/foo)
@@ -789,9 +780,6 @@ fn dry_run() {
789780
.arg(registry.index_url().as_str())
790781
.with_stderr_data(str![[r#"
791782
[UPDATING] `[ROOT]/registry` index
792-
[WARNING] manifest has no documentation, homepage or repository
793-
|
794-
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
795783
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
796784
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
797785
[VERIFYING] foo v0.0.1 ([ROOT]/foo)

0 commit comments

Comments
 (0)