Skip to content

Commit 3c5815f

Browse files
author
Alex Helfet
committed
Registry index URLs resolved relative to config path.
1 parent 491f8d6 commit 3c5815f

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/cargo/ops/registry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ pub fn registry_configuration(
324324
)
325325
}
326326
None => {
327-
// Checking out for default index and token
327+
// Checking for default index and token
328328
(
329-
config.get_string("registry.index")?.map(|p| p.val),
329+
config.get_default_registry_index()?.map(|url| url.to_string()),
330330
config.get_string("registry.token")?.map(|p| p.val),
331331
)
332332
}

src/cargo/util/config.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::util::errors::{self, internal, CargoResult, CargoResultExt};
2929
use crate::util::toml as cargo_toml;
3030
use crate::util::Filesystem;
3131
use crate::util::Rustc;
32-
use crate::util::ToUrl;
32+
use crate::util::ToUrlWithBase;
3333
use crate::util::{paths, validate_package_name};
3434

3535
/// Configuration information for cargo. This is not specific to a build, it is information
@@ -667,18 +667,31 @@ impl Config {
667667
validate_package_name(registry, "registry name", "")?;
668668
Ok(
669669
match self.get_string(&format!("registries.{}.index", registry))? {
670-
Some(index) => {
671-
let url = index.val.to_url()?;
672-
if url.password().is_some() {
673-
failure::bail!("Registry URLs may not contain passwords");
674-
}
675-
url
676-
}
670+
Some(index) => self.resolve_registry_index(index)?,
677671
None => failure::bail!("No index found for registry: `{}`", registry),
678672
},
679673
)
680674
}
681675

676+
/// Gets the index for the default registry.
677+
pub fn get_default_registry_index(&self) -> CargoResult<Option<Url>> {
678+
Ok(
679+
match self.get_string("registry.index")? {
680+
Some(index) => Some(self.resolve_registry_index(index)?),
681+
None => None,
682+
},
683+
)
684+
}
685+
686+
fn resolve_registry_index(&self, index: Value<String>) -> CargoResult<Url> {
687+
let base = index.definition.root(&self);
688+
let url = index.val.to_url_with_base(Some(base))?;
689+
if url.password().is_some() {
690+
failure::bail!("Registry URLs may not contain passwords");
691+
}
692+
Ok(url)
693+
}
694+
682695
/// Loads credentials config from the credentials file into the `ConfigValue` object, if
683696
/// present.
684697
fn load_credentials(&self, cfg: &mut ConfigValue) -> CargoResult<()> {
@@ -1648,3 +1661,4 @@ pub fn save_credentials(cfg: &Config, token: String, registry: Option<String>) -
16481661
Ok(())
16491662
}
16501663
}
1664+

src/cargo/util/to_url.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::Path;
1+
use std::path::{Path, PathBuf};
22

33
use url::Url;
44

@@ -22,3 +22,9 @@ impl<'a> ToUrl for &'a Path {
2222
.map_err(|()| failure::format_err!("invalid path url `{}`", self.display()))
2323
}
2424
}
25+
26+
impl<'a> ToUrl for &'a PathBuf {
27+
fn to_url(self) -> CargoResult<Url> {
28+
self.as_path().to_url()
29+
}
30+
}

0 commit comments

Comments
 (0)