Skip to content

Commit b9a3cf4

Browse files
committed
Fix Cargo removing the sparse+ prefix from sparse URLs in .crates.toml
1 parent f3de2de commit b9a3cf4

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/cargo/core/source/source_id.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ impl SourceId {
8181
///
8282
/// The canonical url will be calculated, but the precise field will not
8383
fn new(kind: SourceKind, url: Url, name: Option<&str>) -> CargoResult<SourceId> {
84+
if kind == SourceKind::SparseRegistry {
85+
assert!(url.as_str().starts_with("sparse+"));
86+
}
8487
let source_id = SourceId::wrap(SourceIdInner {
8588
kind,
8689
canonical_url: CanonicalUrl::new(&url)?,
@@ -152,7 +155,7 @@ impl SourceId {
152155
.with_precise(Some("locked".to_string())))
153156
}
154157
"sparse" => {
155-
let url = url.into_url()?;
158+
let url = string.into_url()?;
156159
Ok(SourceId::new(SourceKind::SparseRegistry, url, None)?
157160
.with_precise(Some("locked".to_string())))
158161
}
@@ -721,6 +724,7 @@ impl<'a> fmt::Display for SourceIdAsUrl<'a> {
721724
ref url,
722725
..
723726
} => {
727+
// Sparse registry URL already includes the `sparse+` prefix
724728
write!(f, "{url}")
725729
}
726730
SourceIdInner {
@@ -864,4 +868,14 @@ mod tests {
864868
assert_eq!(gen_hash(source_id), 17459999773908528552);
865869
assert_eq!(crate::util::hex::short_hash(&source_id), "6568fe2c2fab5bfe");
866870
}
871+
872+
#[test]
873+
fn serde_roundtrip() {
874+
let url = "sparse+https://my-crates.io/".into_url().unwrap();
875+
let source_id = SourceId::for_registry(&url).unwrap();
876+
let formatted = format!("{}", source_id.as_url());
877+
let deserialized = SourceId::from_url(&formatted).unwrap();
878+
assert_eq!(formatted, "sparse+https://my-crates.io/");
879+
assert_eq!(source_id, deserialized);
880+
}
867881
}

src/cargo/util/canonical_url.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::util::{errors::CargoResult, IntoUrl};
1+
use crate::util::errors::CargoResult;
22
use std::hash::{self, Hash};
33
use url::Url;
44

@@ -56,17 +56,6 @@ impl CanonicalUrl {
5656
url.path_segments_mut().unwrap().pop().push(&last);
5757
}
5858

59-
// Ignore the protocol specifier (if any).
60-
if url.scheme().starts_with("sparse+") {
61-
// NOTE: it is illegal to use set_scheme to change sparse+http(s) to http(s).
62-
url = url
63-
.to_string()
64-
.strip_prefix("sparse+")
65-
.expect("we just found that prefix")
66-
.into_url()
67-
.expect("a valid url without a protocol specifier should still be valid");
68-
}
69-
7059
Ok(CanonicalUrl(url))
7160
}
7261

0 commit comments

Comments
 (0)