Skip to content

Commit b47a161

Browse files
committed
fix: update metadata warning to only warn for crates.io-publishable packages
1 parent e7246c6 commit b47a161

File tree

2 files changed

+152
-13
lines changed

2 files changed

+152
-13
lines changed

src/cargo/ops/cargo_package/mod.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -829,20 +829,31 @@ fn check_metadata(pkg: &Package, gctx: &GlobalContext) -> CargoResult<()> {
829829
);
830830

831831
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 ");
832+
let should_warn = match pkg.publish() {
833+
Some(registries) if registries.is_empty() => true, // publish = false, still warn
834+
Some(registries) => {
835+
// Only warn if crates.io is allowed
836+
registries.iter().any(|r| r == CRATES_IO_REGISTRY)
837+
}
838+
None => true, // No publish field means can publish anywhere, including crates.io
839+
};
840+
841+
if should_warn {
842+
let mut things = missing[..missing.len() - 1].join(", ");
843+
// `things` will be empty if and only if its length is 1 (i.e., the only case
844+
// to have no `or`).
845+
if !things.is_empty() {
846+
things.push_str(" or ");
847+
}
848+
things.push_str(missing.last().unwrap());
849+
850+
gctx.shell().print_report(&[
851+
Level::WARNING.secondary_title(format!("manifest has no {things}"))
852+
.element(Level::NOTE.message("see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info"))
853+
],
854+
false
855+
)?
837856
}
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-
)?
846857
}
847858

848859
Ok(())

tests/testsuite/package.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7739,3 +7739,131 @@ Caused by:
77397739
"#]])
77407740
.run();
77417741
}
7742+
7743+
#[cargo_test]
7744+
fn metadata_check_runs_for_publish_false() {
7745+
// Test that metadata check still runs when publish = false (empty array)
7746+
let p = project()
7747+
.file(
7748+
"Cargo.toml",
7749+
r#"
7750+
[package]
7751+
name = "foo"
7752+
version = "0.0.1"
7753+
edition = "2015"
7754+
authors = []
7755+
publish = false
7756+
"#,
7757+
)
7758+
.file("src/main.rs", "fn main() {}")
7759+
.build();
7760+
7761+
p.cargo("package")
7762+
.with_stderr_data(str![[r#"
7763+
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository
7764+
|
7765+
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
7766+
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
7767+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
7768+
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
7769+
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
7770+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
7771+
7772+
"#]])
7773+
.run();
7774+
}
7775+
7776+
#[cargo_test]
7777+
fn metadata_check_skipped_for_non_crates_io_registry() {
7778+
// Test that metadata warning is skipped when publish only includes non-crates.io registries
7779+
let p = project()
7780+
.file(
7781+
"Cargo.toml",
7782+
r#"
7783+
[package]
7784+
name = "foo"
7785+
version = "0.0.1"
7786+
edition = "2015"
7787+
authors = []
7788+
publish = ["my-registry"]
7789+
"#,
7790+
)
7791+
.file("src/main.rs", "fn main() {}")
7792+
.build();
7793+
7794+
p.cargo("package")
7795+
.with_stderr_data(str![[r#"
7796+
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
7797+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
7798+
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
7799+
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
7800+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
7801+
7802+
"#]])
7803+
.run();
7804+
}
7805+
7806+
#[cargo_test]
7807+
fn metadata_check_runs_for_crates_io_registry() {
7808+
// Test that metadata check runs when publish includes "crates-io"
7809+
let p = project()
7810+
.file(
7811+
"Cargo.toml",
7812+
r#"
7813+
[package]
7814+
name = "foo"
7815+
version = "0.0.1"
7816+
edition = "2015"
7817+
authors = []
7818+
publish = ["crates-io", "my-registry"]
7819+
"#,
7820+
)
7821+
.file("src/main.rs", "fn main() {}")
7822+
.build();
7823+
7824+
p.cargo("package")
7825+
.with_stderr_data(str![[r#"
7826+
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository
7827+
|
7828+
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
7829+
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
7830+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
7831+
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
7832+
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
7833+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
7834+
7835+
"#]])
7836+
.run();
7837+
}
7838+
7839+
#[cargo_test]
7840+
fn metadata_check_runs_when_publish_unspecified() {
7841+
// Test that metadata check runs when publish is not specified (None case)
7842+
let p = project()
7843+
.file(
7844+
"Cargo.toml",
7845+
r#"
7846+
[package]
7847+
name = "foo"
7848+
version = "0.0.1"
7849+
edition = "2015"
7850+
authors = []
7851+
"#,
7852+
)
7853+
.file("src/main.rs", "fn main() {}")
7854+
.build();
7855+
7856+
p.cargo("package")
7857+
.with_stderr_data(str![[r#"
7858+
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository
7859+
|
7860+
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
7861+
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
7862+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
7863+
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
7864+
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
7865+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
7866+
7867+
"#]])
7868+
.run();
7869+
}

0 commit comments

Comments
 (0)