Skip to content

Commit d97bbc8

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

File tree

2 files changed

+96
-20
lines changed

2 files changed

+96
-20
lines changed

src/cargo/ops/registry/publish.rs

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
126126
}
127127

128128
let just_pkgs: Vec<_> = pkgs.iter().map(|p| p.0).collect();
129-
let reg_or_index = match opts.reg_or_index.clone() {
130-
Some(r) => {
131-
validate_registry(&just_pkgs, Some(&r))?;
132-
Some(r)
133-
}
134-
None => {
135-
let reg = super::infer_registry(&just_pkgs)?;
136-
validate_registry(&just_pkgs, reg.as_ref())?;
137-
if let Some(RegistryOrIndex::Registry(registry)) = &reg {
138-
if registry != CRATES_IO_REGISTRY {
139-
// Don't warn for crates.io.
140-
opts.gctx.shell().note(&format!(
141-
"found `{}` as only allowed registry. Publishing to it automatically.",
142-
registry
143-
))?;
144-
}
145-
}
146-
reg
147-
}
148-
};
129+
let reg_or_index = resolve_registory_or_index(opts, &just_pkgs)?;
149130

150131
// This is only used to confirm that we can create a token before we build the package.
151132
// This causes the credential provider to be called an extra time, but keeps the same order of errors.
@@ -814,6 +795,55 @@ fn package_list(pkgs: impl IntoIterator<Item = PackageId>, final_sep: &str) -> S
814795
}
815796
}
816797

798+
fn resolve_registory_or_index(
799+
opts: &PublishOpts<'_>,
800+
pkgs: &[&Package],
801+
) -> CargoResult<Option<RegistryOrIndex>> {
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)
819+
}
820+
}
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)
845+
}
846+
817847
fn validate_registry(pkgs: &[&Package], reg_or_index: Option<&RegistryOrIndex>) -> CargoResult<()> {
818848
let reg_name = match reg_or_index {
819849
Some(RegistryOrIndex::Registry(r)) => Some(r.as_str()),

tests/testsuite/publish.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,51 @@ fn publish_implicitly_to_only_allowed_registry() {
979979
);
980980
}
981981

982+
#[cargo_test]
983+
fn publish_warn_when_both_publish_and_index_specified() {
984+
let registry = RegistryBuilder::new()
985+
.http_api()
986+
.http_index()
987+
.alternative()
988+
.build();
989+
990+
let p = project().build();
991+
992+
let _ = repo(&paths::root().join("foo"))
993+
.file(
994+
"Cargo.toml",
995+
r#"
996+
[package]
997+
name = "foo"
998+
version = "0.0.1"
999+
edition = "2015"
1000+
authors = []
1001+
license = "MIT"
1002+
description = "foo"
1003+
documentation = "foo"
1004+
homepage = "foo"
1005+
repository = "foo"
1006+
publish = ["registry"]
1007+
"#,
1008+
)
1009+
.file("src/main.rs", "fn main() {}")
1010+
.build();
1011+
1012+
p.cargo("publish")
1013+
.arg("--index")
1014+
.arg(registry.index_url().as_str())
1015+
.arg("--token")
1016+
.arg(registry.token())
1017+
.with_stderr_data(str![[r#"
1018+
...
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+
...
1022+
[PUBLISHED] foo v0.0.1 at registry [..]
1023+
"#]])
1024+
.run();
1025+
}
1026+
9821027
#[cargo_test]
9831028
fn publish_failed_with_index_and_only_allowed_registry() {
9841029
let registry = RegistryBuilder::new()
@@ -1014,6 +1059,7 @@ fn publish_failed_with_index_and_only_allowed_registry() {
10141059
.arg(registry.index_url().as_str())
10151060
.with_status(101)
10161061
.with_stderr_data(str![[r#"
1062+
...
10171063
[ERROR] command-line argument --index requires --token to be specified
10181064
10191065
"#]])

0 commit comments

Comments
 (0)