Skip to content

Commit 3684f52

Browse files
committed
feat: emit a warning when both package.publish and --index are specified
1 parent 2194a3a commit 3684f52

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,6 @@ 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
10201019
[UPDATING] [..] index
10211020
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
10221021
[PACKAGED] 5 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
@@ -1067,6 +1066,7 @@ fn publish_failed_with_index_and_only_allowed_registry() {
10671066
.arg(registry.index_url().as_str())
10681067
.with_status(101)
10691068
.with_stderr_data(str![[r#"
1069+
...
10701070
[ERROR] command-line argument --index requires --token to be specified
10711071
10721072
"#]])

0 commit comments

Comments
 (0)