Skip to content

Commit cb6f958

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

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

src/cargo/ops/registry/publish.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -797,30 +797,44 @@ 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>> {
802802
let opt_index_or_registry = opts.reg_or_index.clone();
803803

804-
Ok(match opt_index_or_registry {
805-
Some(r) => {
806-
validate_registry(&just_pkgs, Some(&r))?;
807-
Some(r)
804+
let res = match opt_index_or_registry {
805+
ref r @ Some(ref registry_or_index) => {
806+
validate_registry(pkgs, r.as_ref())?;
807+
808+
let registry_is_specified_by_any_package = 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 => {
810-
let reg = super::infer_registry(&just_pkgs)?;
811-
validate_registry(&just_pkgs, reg.as_ref())?;
812-
if let Some(RegistryOrIndex::Registry(registry)) = &reg {
813-
if registry != CRATES_IO_REGISTRY {
814-
// Don't warn for crates.io.
815-
opts.gctx.shell().note(&format!(
816-
"found `{}` as only allowed registry. Publishing to it automatically.",
817-
registry
818-
))?;
819-
}
820+
let registry_or_index = super::infer_registry(pkgs)?;
821+
validate_registry(pkgs, registry_or_index.as_ref())?;
822+
823+
if let Some(RegistryOrIndex::Registry(ref registry)) = registry_or_index
824+
&& registry != CRATES_IO_REGISTRY
825+
{
826+
// Don't warn for crates.io.
827+
opts.gctx.shell().note(&format!(
828+
"found `{}` as only allowed registry. Publishing to it automatically.",
829+
registry
830+
))?;
820831
}
821-
reg
832+
833+
registry_or_index
822834
}
823-
})
835+
};
836+
837+
Ok(res)
824838
}
825839

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

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)