Skip to content

Commit 6aca041

Browse files
committed
Auto merge of #7239 - ehuss:publish-non-remote-registry, r=alexcrichton
Improve error message when using API command with non-remote registry. If you try to use an API command (publish, yank, search, etc.) with a source replacement active (like cargo-vendor or cargo-local-registry), then you get a really weird "failed to fetch" error. This adds a specific check to provide a slightly nicer error. I'm not entirely happy with the wording, but I think it gets the point across.
2 parents 130e11c + cce8742 commit 6aca041

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/cargo/core/source/source_id.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ impl SourceId {
259259
}
260260
}
261261

262+
/// Returns `true` if this source is a "remote" registry.
263+
///
264+
/// "remote" may also mean a file URL to a git index, so it is not
265+
/// necessarily "remote". This just means it is not `local-registry`.
266+
pub fn is_remote_registry(self) -> bool {
267+
match self.inner.kind {
268+
Kind::Registry => true,
269+
_ => false,
270+
}
271+
}
272+
262273
/// Returns `true` if this source from a Git repository.
263274
pub fn is_git(self) -> bool {
264275
match self.inner.kind {

src/cargo/ops/registry.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ fn registry(
345345
} = registry_configuration(config, registry.clone())?;
346346
let token = token.or(token_config);
347347
let sid = get_source_id(config, index_config.or(index), registry)?;
348+
if !sid.is_remote_registry() {
349+
bail!(
350+
"{} does not support API commands.\n\
351+
Check for a source-replacement in .cargo/config.",
352+
sid
353+
);
354+
}
348355
let api_host = {
349356
let _lock = config.acquire_package_cache_lock()?;
350357
let mut src = RegistrySource::remote(sid, &HashSet::new(), config);

tests/testsuite/publish.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,3 +1022,51 @@ fn publish_checks_for_token_before_verify() {
10221022
.with_stderr_contains("[VERIFYING] foo v0.0.1 ([CWD])")
10231023
.run();
10241024
}
1025+
1026+
#[cargo_test]
1027+
fn publish_with_bad_source() {
1028+
let p = project()
1029+
.file(
1030+
".cargo/config",
1031+
r#"
1032+
[source.crates-io]
1033+
replace-with = 'local-registry'
1034+
1035+
[source.local-registry]
1036+
local-registry = 'registry'
1037+
"#,
1038+
)
1039+
.file("src/lib.rs", "")
1040+
.build();
1041+
1042+
p.cargo("publish")
1043+
.with_status(101)
1044+
.with_stderr(
1045+
"\
1046+
[ERROR] registry `[..]/foo/registry` does not support API commands.
1047+
Check for a source-replacement in .cargo/config.
1048+
",
1049+
)
1050+
.run();
1051+
1052+
p.change_file(
1053+
".cargo/config",
1054+
r#"
1055+
[source.crates-io]
1056+
replace-with = "vendored-sources"
1057+
1058+
[source.vendored-sources]
1059+
directory = "vendor"
1060+
"#,
1061+
);
1062+
1063+
p.cargo("publish")
1064+
.with_status(101)
1065+
.with_stderr(
1066+
"\
1067+
[ERROR] dir [..]/foo/vendor does not support API commands.
1068+
Check for a source-replacement in .cargo/config.
1069+
",
1070+
)
1071+
.run();
1072+
}

0 commit comments

Comments
 (0)