diff --git a/src/cargo/sources/replaced.rs b/src/cargo/sources/replaced.rs index 8c38a52413f..c0ff27954a2 100644 --- a/src/cargo/sources/replaced.rs +++ b/src/cargo/sources/replaced.rs @@ -6,8 +6,6 @@ use crate::sources::IndexSummary; use crate::util::errors::CargoResult; use std::task::Poll; -use anyhow::Context as _; - /// A source that replaces one source with the other. This manages the [source /// replacement] feature. /// @@ -37,6 +35,14 @@ impl<'cfg> ReplacedSource<'cfg> { inner: src, } } + + /// Is this source a built-in replacement of crates.io? + /// + /// Built-in source replacement of crates.io for sparse registry or tests + /// should not show messages indicating source replacement. + fn is_builtin_replacement(&self) -> bool { + self.replace_with.is_crates_io() && self.to_replace.is_crates_io() + } } impl<'cfg> Source for ReplacedSource<'cfg> { @@ -70,10 +76,14 @@ impl<'cfg> Source for ReplacedSource<'cfg> { f(summary.map_summary(|s| s.map_source(replace_with, to_replace))) }) .map_err(|e| { - e.context(format!( - "failed to query replaced source {}", - self.to_replace - )) + if self.is_builtin_replacement() { + e + } else { + e.context(format!( + "failed to query replaced source {}", + self.to_replace + )) + } }) } @@ -87,10 +97,16 @@ impl<'cfg> Source for ReplacedSource<'cfg> { fn download(&mut self, id: PackageId) -> CargoResult { let id = id.with_source_id(self.replace_with); - let pkg = self - .inner - .download(id) - .with_context(|| format!("failed to download replaced source {}", self.to_replace))?; + let pkg = self.inner.download(id).map_err(|e| { + if self.is_builtin_replacement() { + e + } else { + e.context(format!( + "failed to download replaced source {}", + self.to_replace + )) + } + })?; Ok(match pkg { MaybePackage::Ready(pkg) => { MaybePackage::Ready(pkg.map_source(self.replace_with, self.to_replace)) @@ -101,10 +117,16 @@ impl<'cfg> Source for ReplacedSource<'cfg> { fn finish_download(&mut self, id: PackageId, data: Vec) -> CargoResult { let id = id.with_source_id(self.replace_with); - let pkg = self - .inner - .finish_download(id, data) - .with_context(|| format!("failed to download replaced source {}", self.to_replace))?; + let pkg = self.inner.finish_download(id, data).map_err(|e| { + if self.is_builtin_replacement() { + e + } else { + e.context(format!( + "failed to download replaced source {}", + self.to_replace + )) + } + })?; Ok(pkg.map_source(self.replace_with, self.to_replace)) } @@ -118,9 +140,7 @@ impl<'cfg> Source for ReplacedSource<'cfg> { } fn describe(&self) -> String { - if self.replace_with.is_crates_io() && self.to_replace.is_crates_io() { - // Built-in source replacement of crates.io for sparse registry or tests - // doesn't need duplicate description (crates.io replacing crates.io). + if self.is_builtin_replacement() { self.inner.describe() } else { format!( @@ -148,8 +168,15 @@ impl<'cfg> Source for ReplacedSource<'cfg> { } fn block_until_ready(&mut self) -> CargoResult<()> { - self.inner - .block_until_ready() - .with_context(|| format!("failed to update replaced source {}", self.to_replace)) + self.inner.block_until_ready().map_err(|e| { + if self.is_builtin_replacement() { + e + } else { + e.context(format!( + "failed to update replaced source {}", + self.to_replace + )) + } + }) } } diff --git a/tests/testsuite/credential_process.rs b/tests/testsuite/credential_process.rs index e31f675fb78..1317f2b4146 100644 --- a/tests/testsuite/credential_process.rs +++ b/tests/testsuite/credential_process.rs @@ -275,10 +275,7 @@ fn not_found() { r#"[UPDATING] [..] [CREDENTIAL] [..]not_found[..] get crates-io {"v":1[..] -[ERROR] failed to query replaced source registry `crates-io` - -Caused by: - no token found, please run `cargo login` +[ERROR] no token found, please run `cargo login` "#, ) .run(); @@ -314,10 +311,7 @@ fn all_not_found() { r#"[UPDATING] [..] [CREDENTIAL] [..]not_found[..] get crates-io {"v":1,"registry":{"index-url":"[..]","name":"crates-io","headers":[[..]"WWW-Authenticate: Cargo login_url=\"https://test-registry-login/me\""[..]]},"kind":"get","operation":"read"} -[ERROR] failed to query replaced source registry `crates-io` - -Caused by: - no token found, please run `cargo login` +[ERROR] no token found, please run `cargo login` "#, ) .run(); @@ -353,10 +347,7 @@ fn all_not_supported() { r#"[UPDATING] [..] [CREDENTIAL] [..]not_supported[..] get crates-io {"v":1,"registry":{"index-url":"[..]","name":"crates-io","headers":[[..]"WWW-Authenticate: Cargo login_url=\"https://test-registry-login/me\""[..]]},"kind":"get","operation":"read"} -[ERROR] failed to query replaced source registry `crates-io` - -Caused by: - no credential providers could handle the request +[ERROR] no credential providers could handle the request "#, ) .run(); diff --git a/tests/testsuite/registry.rs b/tests/testsuite/registry.rs index 8093d5e9c1a..c442a1edbc1 100644 --- a/tests/testsuite/registry.rs +++ b/tests/testsuite/registry.rs @@ -3664,3 +3664,42 @@ fn differ_only_by_metadata_with_lockfile() { ) .run(); } + +#[cargo_test] +fn builtin_source_replacement() { + // errors for builtin source replacement of crates.io + // should not include mention of source replacement in the error message. + let server = RegistryBuilder::new().build(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + + [dependencies] + bad-cksum = ">= 0.0.0" + "#, + ) + .file("src/main.rs", "fn main() {}") + .build(); + + let pkg = Package::new("bad-cksum", "0.0.1"); + pkg.publish(); + t!(File::create(&pkg.archive_dst())); + + p.cargo("check -v") + .replace_crates_io(&server.index_url()) + .with_status(101) + .with_stderr( + "\ +[UPDATING] [..] index +[DOWNLOADING] crates ... +[DOWNLOADED] bad-cksum [..] +[ERROR] failed to verify the checksum of `bad-cksum v0.0.1` +", + ) + .run(); +}