Skip to content

Commit aff3bb8

Browse files
committed
Auto merge of #11177 - arlosi:sparse-lockfile, r=weihanglo
Fix sparse registry lockfile urls containing 'registry+sparse+' The `Cargo.lock` file for alternative sparse registries incorrectly lists the url as `registry+sparse+` rather than `sparse+`. Fixes #10963
2 parents 37f39f6 + 10d3b3d commit aff3bb8

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/cargo/core/source/source_id.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,16 @@ impl<'a> fmt::Display for SourceIdAsUrl<'a> {
680680
kind: SourceKind::Registry,
681681
ref url,
682682
..
683-
} => write!(f, "registry+{}", url),
683+
} => {
684+
// For sparse http registry the URL already contains the prefix.
685+
if url.scheme().starts_with("sparse+") {
686+
// e.g. sparse+http://example.com
687+
write!(f, "{url}")
688+
} else {
689+
// e.g. registry+http://example.com
690+
write!(f, "registry+{url}")
691+
}
692+
}
684693
SourceIdInner {
685694
kind: SourceKind::LocalRegistry,
686695
ref url,

tests/testsuite/alt_registry.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Tests for alternative registries.
22
33
use cargo::util::IntoUrl;
4+
use cargo_test_support::compare::assert_match_exact;
45
use cargo_test_support::publish::validate_alt_upload;
56
use cargo_test_support::registry::{self, Package, RegistryBuilder};
67
use cargo_test_support::{basic_manifest, git, paths, project};
@@ -1309,3 +1310,51 @@ fn both_index_and_registry() {
13091310
.run();
13101311
}
13111312
}
1313+
1314+
#[cargo_test]
1315+
fn sparse_lockfile() {
1316+
let _registry = registry::RegistryBuilder::new()
1317+
.http_index()
1318+
.alternative()
1319+
.build();
1320+
Package::new("foo", "0.1.0").alternative(true).publish();
1321+
1322+
let p = project()
1323+
.file(
1324+
"Cargo.toml",
1325+
r#"
1326+
[project]
1327+
name = "a"
1328+
version = "0.5.0"
1329+
authors = []
1330+
1331+
[dependencies]
1332+
foo = { registry = 'alternative', version = '0.1.0'}
1333+
"#,
1334+
)
1335+
.file("src/lib.rs", "")
1336+
.build();
1337+
1338+
p.cargo("-Zsparse-registry generate-lockfile")
1339+
.masquerade_as_nightly_cargo(&["sparse-registry"])
1340+
.run();
1341+
assert_match_exact(
1342+
&p.read_lockfile(),
1343+
r#"# This file is automatically @generated by Cargo.
1344+
# It is not intended for manual editing.
1345+
version = 3
1346+
1347+
[[package]]
1348+
name = "a"
1349+
version = "0.5.0"
1350+
dependencies = [
1351+
"foo",
1352+
]
1353+
1354+
[[package]]
1355+
name = "foo"
1356+
version = "0.1.0"
1357+
source = "sparse+http://[..]/"
1358+
checksum = "f6a200a9339fef960979d94d5c99cbbfd899b6f5a396a55d9775089119050203""#,
1359+
);
1360+
}

0 commit comments

Comments
 (0)