diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index c51a67c5074..f532cb35286 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -680,7 +680,16 @@ impl<'a> fmt::Display for SourceIdAsUrl<'a> { kind: SourceKind::Registry, ref url, .. - } => write!(f, "registry+{}", url), + } => { + // For sparse http registry the URL already contains the prefix. + if url.scheme().starts_with("sparse+") { + // e.g. sparse+http://example.com + write!(f, "{url}") + } else { + // e.g. registry+http://example.com + write!(f, "registry+{url}") + } + } SourceIdInner { kind: SourceKind::LocalRegistry, ref url, diff --git a/tests/testsuite/alt_registry.rs b/tests/testsuite/alt_registry.rs index f529e3b7060..1d760580fc8 100644 --- a/tests/testsuite/alt_registry.rs +++ b/tests/testsuite/alt_registry.rs @@ -1,6 +1,7 @@ //! Tests for alternative registries. use cargo::util::IntoUrl; +use cargo_test_support::compare::assert_match_exact; use cargo_test_support::publish::validate_alt_upload; use cargo_test_support::registry::{self, Package, RegistryBuilder}; use cargo_test_support::{basic_manifest, git, paths, project}; @@ -1309,3 +1310,51 @@ fn both_index_and_registry() { .run(); } } + +#[cargo_test] +fn sparse_lockfile() { + let _registry = registry::RegistryBuilder::new() + .http_index() + .alternative() + .build(); + Package::new("foo", "0.1.0").alternative(true).publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + [project] + name = "a" + version = "0.5.0" + authors = [] + + [dependencies] + foo = { registry = 'alternative', version = '0.1.0'} + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("-Zsparse-registry generate-lockfile") + .masquerade_as_nightly_cargo(&["sparse-registry"]) + .run(); + assert_match_exact( + &p.read_lockfile(), + r#"# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "a" +version = "0.5.0" +dependencies = [ + "foo", +] + +[[package]] +name = "foo" +version = "0.1.0" +source = "sparse+http://[..]/" +checksum = "f6a200a9339fef960979d94d5c99cbbfd899b6f5a396a55d9775089119050203""#, + ); +}