Skip to content

Commit 97baf8c

Browse files
committed
fix: check sourceId and generate warning accordingly
1 parent 06f117b commit 97baf8c

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/cargo/ops/cargo_package/mod.rs

Lines changed: 30 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, gctx, opts.reg_or_index.as_ref())?;
487487
}
488488

489489
if !pkg.manifest().exclude().is_empty() && !pkg.manifest().include().is_empty() {
@@ -808,7 +808,13 @@ 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+
// Checks that the package has some piece of metadata that a human can
812+
// use to tell what the package is about.
813+
fn check_metadata(
814+
pkg: &Package,
815+
gctx: &GlobalContext,
816+
reg_or_index: Option<&RegistryOrIndex>,
817+
) -> CargoResult<()> {
812818
let md = pkg.manifest().metadata();
813819

814820
let mut missing = vec![];
@@ -829,20 +835,29 @@ fn check_metadata(pkg: &Package, gctx: &GlobalContext) -> CargoResult<()> {
829835
);
830836

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

848863
Ok(())

tests/testsuite/package.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7797,13 +7797,10 @@ fn publish_to_alt_registry_warns() {
77977797
.file("src/main.rs", "fn main() {}")
77987798
.build();
77997799

7800-
// Old behavior: warnings appear even for alt registry
7800+
// New behavior: warnings won't appear for alt registry
78017801
p.cargo("publish --dry-run --registry alternative")
78027802
.with_stderr_data(str![[r#"
78037803
[UPDATING] `alternative` index
7804-
[WARNING] manifest has no license, license-file, documentation, homepage or repository
7805-
|
7806-
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
78077804
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
78087805
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
78097806
[VERIFYING] foo v0.1.0 ([ROOT]/foo)

0 commit comments

Comments
 (0)