Skip to content

Commit 8e32885

Browse files
committed
feat: emit a warning when both package.publish and --index are specified
1 parent 5b41cea commit 8e32885

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed

src/cargo/ops/registry/publish.rs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -797,28 +797,51 @@ fn package_list(pkgs: impl IntoIterator<Item = PackageId>, final_sep: &str) -> S
797797

798798
fn resolve_registry_or_index(
799799
opts: &PublishOpts<'_>,
800-
just_pkgs: &[&Package],
800+
pkgs: &[&Package],
801801
) -> CargoResult<Option<RegistryOrIndex>> {
802-
Ok(match opts.reg_or_index.clone() {
803-
Some(r) => {
804-
validate_registry(&just_pkgs, Some(&r))?;
805-
Some(r)
806-
}
807-
None => {
808-
let reg = super::infer_registry(&just_pkgs)?;
809-
validate_registry(&just_pkgs, reg.as_ref())?;
810-
if let Some(RegistryOrIndex::Registry(registry)) = &reg {
811-
if registry != CRATES_IO_REGISTRY {
812-
// Don't warn for crates.io.
813-
opts.gctx.shell().note(&format!(
814-
"found `{}` as only allowed registry. Publishing to it automatically.",
815-
registry
816-
))?;
802+
let opt_index_or_registry = opts.reg_or_index.clone();
803+
let registry_is_specified_by_any_package = pkgs
804+
.iter()
805+
.any(|pkg| pkg.publish().as_ref().map(|v| v.len()).unwrap_or(0) > 0);
806+
807+
let res = match (opt_index_or_registry, registry_is_specified_by_any_package) {
808+
// both `package.publish` and `--index` or `--registry` are specified
809+
(Some(opt_reg_or_index), true) => {
810+
validate_registry(pkgs, Some(&opt_reg_or_index))?;
811+
812+
match opt_reg_or_index {
813+
r @ RegistryOrIndex::Registry(_) => Some(r),
814+
r @ RegistryOrIndex::Index(_) => {
815+
opts.gctx.shell().warn(r#"`--index` will ignore registries set by `package.publish` in Cargo.toml, and may cause unexpected push to prohibited registry
816+
help: use `--registry` instead or set `publish = true` in Cargo.toml to suppress this warning"#)?;
817+
818+
Some(r)
817819
}
818820
}
819-
reg
820821
}
821-
})
822+
// only `package.publish` section
823+
(None, true) => {
824+
let registry_or_index = super::infer_registry(pkgs)?;
825+
if let Some(RegistryOrIndex::Registry(ref registry)) = registry_or_index
826+
&& registry != CRATES_IO_REGISTRY
827+
{
828+
// Don't warn for crates.io.
829+
opts.gctx.shell().note(&format!(
830+
"found `{}` as only allowed registry. Publishing to it automatically.",
831+
registry
832+
))?;
833+
}
834+
835+
registry_or_index
836+
}
837+
(Some(opt_reg), false) => {
838+
validate_registry(pkgs, Some(&opt_reg))?;
839+
Some(opt_reg)
840+
}
841+
(None, false) => None,
842+
};
843+
844+
Ok(res)
822845
}
823846

824847
fn validate_registry(pkgs: &[&Package], reg_or_index: Option<&RegistryOrIndex>) -> CargoResult<()> {

tests/testsuite/publish.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,17 +1016,9 @@ fn publish_when_both_publish_and_index_specified() {
10161016
.arg(registry.token())
10171017
.with_stderr_data(str![[r#"
10181018
...
1019-
[WARNING] `cargo publish --token` is deprecated in favor of using `cargo login` and environment variables
1020-
[UPDATING] [..] index
1021-
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
1022-
[PACKAGED] 5 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
1023-
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
1024-
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
1025-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
1026-
[UPLOADING] foo v0.0.1 ([ROOT]/foo)
1027-
[UPLOADED] foo v0.0.1 to registry [..]
1028-
[NOTE] waiting for foo v0.0.1 to be available at registry [..]
1029-
[HELP] you may press ctrl-c to skip waiting; the crate should be available shortly
1019+
[WARNING] `--index` will ignore registries set by `package.publish` in Cargo.toml, and may cause unexpected push to prohibited registry
1020+
[HELP] use `--registry` instead or set `publish = true` in Cargo.toml to suppress this warning
1021+
...
10301022
[PUBLISHED] foo v0.0.1 at registry [..]
10311023
"#]])
10321024
.run();
@@ -1067,6 +1059,8 @@ fn publish_failed_with_index_and_only_allowed_registry() {
10671059
.arg(registry.index_url().as_str())
10681060
.with_status(101)
10691061
.with_stderr_data(str![[r#"
1062+
[WARNING] `--index` will ignore registries set by `package.publish` in Cargo.toml, and may cause unexpected push to prohibited registry
1063+
[HELP] use `--registry` instead or set `publish = true` in Cargo.toml to suppress this warning
10701064
[ERROR] command-line argument --index requires --token to be specified
10711065
10721066
"#]])

0 commit comments

Comments
 (0)