Skip to content

Commit d4a80f3

Browse files
committed
feat: emit a warning when both package.publish and --index are specified
1 parent fa36e6e commit d4a80f3

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/cargo/ops/registry/publish.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,19 @@ fn resolve_registry_or_index(
802802
let opt_index_or_registry = opts.reg_or_index.clone();
803803

804804
let res = match opt_index_or_registry {
805-
Some(r) => {
806-
validate_registry(&just_pkgs, Some(&r))?;
807-
Some(r)
805+
ref r @ Some(ref registry_or_index) => {
806+
validate_registry(just_pkgs, r.as_ref())?;
807+
808+
let registry_is_specified_by_any_package = just_pkgs
809+
.iter()
810+
.any(|pkg| pkg.publish().as_ref().map(|v| v.len()).unwrap_or(0) > 0);
811+
812+
if registry_is_specified_by_any_package && registry_or_index.is_index() {
813+
opts.gctx.shell().warn(r#"`--index` will ignore registries set by `package.publish` in Cargo.toml, and may cause unexpected push to prohibited registry
814+
help: use `--registry` instead or set `publish = true` in Cargo.toml to suppress this warning"#)?;
815+
}
816+
817+
r.clone()
808818
}
809819
None => {
810820
let reg = super::infer_registry(&just_pkgs)?;

tests/testsuite/publish.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,8 @@ fn publish_when_both_publish_and_index_specified() {
10161016
.arg(registry.token())
10171017
.with_stderr_data(str![[r#"
10181018
[WARNING] `cargo publish --token` is deprecated in favor of using `cargo login` and environment variables
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
10191021
[UPDATING] [..] index
10201022
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
10211023
[PACKAGED] 5 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
@@ -1066,6 +1068,7 @@ fn publish_failed_with_index_and_only_allowed_registry() {
10661068
.arg(registry.index_url().as_str())
10671069
.with_status(101)
10681070
.with_stderr_data(str![[r#"
1071+
...
10691072
[ERROR] command-line argument --index requires --token to be specified
10701073
10711074
"#]])

0 commit comments

Comments
 (0)