Skip to content

Commit

Permalink
Auto merge of #7239 - ehuss:publish-non-remote-registry, r=alexcrichton
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Aug 12, 2019
2 parents 130e11c + cce8742 commit 6aca041
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ impl SourceId {
}
}

/// Returns `true` if this source is a "remote" registry.
///
/// "remote" may also mean a file URL to a git index, so it is not
/// necessarily "remote". This just means it is not `local-registry`.
pub fn is_remote_registry(self) -> bool {
match self.inner.kind {
Kind::Registry => true,
_ => false,
}
}

/// Returns `true` if this source from a Git repository.
pub fn is_git(self) -> bool {
match self.inner.kind {
Expand Down
7 changes: 7 additions & 0 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ fn registry(
} = registry_configuration(config, registry.clone())?;
let token = token.or(token_config);
let sid = get_source_id(config, index_config.or(index), registry)?;
if !sid.is_remote_registry() {
bail!(
"{} does not support API commands.\n\
Check for a source-replacement in .cargo/config.",
sid
);
}
let api_host = {
let _lock = config.acquire_package_cache_lock()?;
let mut src = RegistrySource::remote(sid, &HashSet::new(), config);
Expand Down
48 changes: 48 additions & 0 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,3 +1022,51 @@ fn publish_checks_for_token_before_verify() {
.with_stderr_contains("[VERIFYING] foo v0.0.1 ([CWD])")
.run();
}

#[cargo_test]
fn publish_with_bad_source() {
let p = project()
.file(
".cargo/config",
r#"
[source.crates-io]
replace-with = 'local-registry'
[source.local-registry]
local-registry = 'registry'
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("publish")
.with_status(101)
.with_stderr(
"\
[ERROR] registry `[..]/foo/registry` does not support API commands.
Check for a source-replacement in .cargo/config.
",
)
.run();

p.change_file(
".cargo/config",
r#"
[source.crates-io]
replace-with = "vendored-sources"
[source.vendored-sources]
directory = "vendor"
"#,
);

p.cargo("publish")
.with_status(101)
.with_stderr(
"\
[ERROR] dir [..]/foo/vendor does not support API commands.
Check for a source-replacement in .cargo/config.
",
)
.run();
}

0 comments on commit 6aca041

Please sign in to comment.