Skip to content

Commit 4a0345b

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

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

src/cargo/ops/cargo_package/mod.rs

Lines changed: 26 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,9 @@ 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(pkg: &Package, gctx: &GlobalContext, reg_or_index: Option<&RegistryOrIndex>) -> CargoResult<()> {
812814
let md = pkg.manifest().metadata();
813815

814816
let mut missing = vec![];
@@ -829,20 +831,29 @@ fn check_metadata(pkg: &Package, gctx: &GlobalContext) -> CargoResult<()> {
829831
);
830832

831833
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 ");
834+
// Only warn if publishing to crates.io based on resolved registry
835+
let should_warn = match reg_or_index {
836+
Some(RegistryOrIndex::Registry(reg_name)) => reg_name == CRATES_IO_REGISTRY,
837+
None => true, // Default is crates.io
838+
Some(RegistryOrIndex::Index(_)) => false, // Custom index, not 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: 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)