Skip to content

Commit b8ddbd2

Browse files
committed
Show registry name when using source replacment
1 parent 71f5e92 commit b8ddbd2

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/cargo/core/source/source_id.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::core::PackageId;
2-
use crate::sources::{DirectorySource, CRATES_IO_DOMAIN, CRATES_IO_INDEX};
2+
use crate::sources::{DirectorySource, CRATES_IO_DOMAIN, CRATES_IO_INDEX, CRATES_IO_REGISTRY};
33
use crate::sources::{GitSource, PathSource, RegistrySource};
44
use crate::util::{CanonicalUrl, CargoResult, Config, IntoUrl};
55
use log::trace;
@@ -73,13 +73,13 @@ impl SourceId {
7373
/// Creates a `SourceId` object from the kind and URL.
7474
///
7575
/// The canonical url will be calculated, but the precise field will not
76-
fn new(kind: SourceKind, url: Url) -> CargoResult<SourceId> {
76+
fn new(kind: SourceKind, url: Url, name: Option<&str>) -> CargoResult<SourceId> {
7777
let source_id = SourceId::wrap(SourceIdInner {
7878
kind,
7979
canonical_url: CanonicalUrl::new(&url)?,
8080
url,
8181
precise: None,
82-
name: None,
82+
name: name.map(|n| n.into()),
8383
});
8484
Ok(source_id)
8585
}
@@ -132,12 +132,12 @@ impl SourceId {
132132
}
133133
"registry" => {
134134
let url = url.into_url()?;
135-
Ok(SourceId::new(SourceKind::Registry, url)?
135+
Ok(SourceId::new(SourceKind::Registry, url, None)?
136136
.with_precise(Some("locked".to_string())))
137137
}
138138
"path" => {
139139
let url = url.into_url()?;
140-
SourceId::new(SourceKind::Path, url)
140+
SourceId::new(SourceKind::Path, url, None)
141141
}
142142
kind => Err(anyhow::format_err!("unsupported source protocol: {}", kind)),
143143
}
@@ -155,43 +155,53 @@ impl SourceId {
155155
/// `path`: an absolute path.
156156
pub fn for_path(path: &Path) -> CargoResult<SourceId> {
157157
let url = path.into_url()?;
158-
SourceId::new(SourceKind::Path, url)
158+
SourceId::new(SourceKind::Path, url, None)
159159
}
160160

161161
/// Creates a `SourceId` from a Git reference.
162162
pub fn for_git(url: &Url, reference: GitReference) -> CargoResult<SourceId> {
163-
SourceId::new(SourceKind::Git(reference), url.clone())
163+
SourceId::new(SourceKind::Git(reference), url.clone(), None)
164164
}
165165

166-
/// Creates a SourceId from a registry URL.
166+
/// Creates a SourceId from a remote registry URL when the registry name
167+
/// cannot be determined, e.g. an user passes `--index` directly from CLI.
168+
///
169+
/// Use [`SourceId::for_alt_registry`] if a name can provided, which
170+
/// generates better messages for cargo.
167171
pub fn for_registry(url: &Url) -> CargoResult<SourceId> {
168-
SourceId::new(SourceKind::Registry, url.clone())
172+
SourceId::new(SourceKind::Registry, url.clone(), None)
173+
}
174+
175+
/// Creates a `SourceId` from a remote registry URL with given name.
176+
pub fn for_alt_registry(url: &Url, name: &str) -> CargoResult<SourceId> {
177+
SourceId::new(SourceKind::Registry, url.clone(), Some(name))
169178
}
170179

171180
/// Creates a SourceId from a local registry path.
172181
pub fn for_local_registry(path: &Path) -> CargoResult<SourceId> {
173182
let url = path.into_url()?;
174-
SourceId::new(SourceKind::LocalRegistry, url)
183+
SourceId::new(SourceKind::LocalRegistry, url, None)
175184
}
176185

177186
/// Creates a `SourceId` from a directory path.
178187
pub fn for_directory(path: &Path) -> CargoResult<SourceId> {
179188
let url = path.into_url()?;
180-
SourceId::new(SourceKind::Directory, url)
189+
SourceId::new(SourceKind::Directory, url, None)
181190
}
182191

183192
/// Returns the `SourceId` corresponding to the main repository.
184193
///
185194
/// This is the main cargo registry by default, but it can be overridden in
186-
/// a `.cargo/config`.
195+
/// a `.cargo/config.toml`.
187196
pub fn crates_io(config: &Config) -> CargoResult<SourceId> {
188197
config.crates_io_source_id(|| {
189198
config.check_registry_index_not_set()?;
190199
let url = CRATES_IO_INDEX.into_url().unwrap();
191-
SourceId::for_registry(&url)
200+
SourceId::new(SourceKind::Registry, url, Some(CRATES_IO_REGISTRY))
192201
})
193202
}
194203

204+
/// Gets the `SourceId` associated with given name of the remote regsitry.
195205
pub fn alt_registry(config: &Config, key: &str) -> CargoResult<SourceId> {
196206
let url = config.get_registry_index(key)?;
197207
Ok(SourceId::wrap(SourceIdInner {
@@ -670,15 +680,15 @@ mod tests {
670680
fn github_sources_equal() {
671681
let loc = "https://github.com/foo/bar".into_url().unwrap();
672682
let default = SourceKind::Git(GitReference::DefaultBranch);
673-
let s1 = SourceId::new(default.clone(), loc).unwrap();
683+
let s1 = SourceId::new(default.clone(), loc, None).unwrap();
674684

675685
let loc = "git://github.com/foo/bar".into_url().unwrap();
676-
let s2 = SourceId::new(default, loc.clone()).unwrap();
686+
let s2 = SourceId::new(default, loc.clone(), None).unwrap();
677687

678688
assert_eq!(s1, s2);
679689

680690
let foo = SourceKind::Git(GitReference::Branch("foo".to_string()));
681-
let s3 = SourceId::new(foo, loc).unwrap();
691+
let s3 = SourceId::new(foo, loc, None).unwrap();
682692
assert_ne!(s1, s3);
683693
}
684694
}

src/cargo/sources/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ restore the source replacement configuration to continue the build
207207
let mut srcs = Vec::new();
208208
if let Some(registry) = def.registry {
209209
let url = url(&registry, &format!("source.{}.registry", name))?;
210-
srcs.push(SourceId::for_registry(&url)?);
210+
srcs.push(SourceId::for_alt_registry(&url, &name)?);
211211
}
212212
if let Some(local_registry) = def.local_registry {
213213
let path = local_registry.resolve_path(self.config);

0 commit comments

Comments
 (0)