Fix cargo install --index when used with registry.default

This commit is contained in:
Arlo Siemsen 2022-10-27 10:59:07 -05:00
parent 16b097879b
commit 24bf873c83
4 changed files with 38 additions and 12 deletions

View File

@ -126,10 +126,10 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
} else if krates.is_empty() {
from_cwd = true;
SourceId::for_path(config.cwd())?
} else if let Some(registry) = args.registry(config)? {
SourceId::alt_registry(config, &registry)?
} else if let Some(index) = args.get_one::<String>("index") {
SourceId::for_registry(&index.into_url()?)?
} else if let Some(registry) = args.registry(config)? {
SourceId::alt_registry(config, &registry)?
} else {
SourceId::crates_io(config)?
};

View File

@ -1031,11 +1031,8 @@ fn get_source_id(
) -> CargoResult<RegistrySourceIds> {
let sid = match (reg, index) {
(None, None) => SourceId::crates_io(config)?,
(_, Some(i)) => SourceId::for_registry(&i.into_url()?)?,
(Some(r), None) => SourceId::alt_registry(config, r)?,
(None, Some(i)) => SourceId::for_registry(&i.into_url()?)?,
(Some(_), Some(_)) => {
bail!("both `--index` and `--registry` should not be set at the same time")
}
};
// Load source replacements that are built-in to Cargo.
let builtin_replacement_sid = SourceConfigMap::empty(config)?

View File

@ -665,13 +665,23 @@ pub trait ArgMatchesExt {
}
fn registry(&self, config: &Config) -> CargoResult<Option<String>> {
match self._value_of("registry") {
Some(registry) => {
validate_package_name(registry, "registry name", "")?;
Ok(Some(registry.to_string()))
let registry = self._value_of("registry");
let index = self._value_of("index");
let result = match (registry, index) {
(None, None) => config.default_registry()?,
(None, Some(_)) => {
// If --index is set, then do not look at registry.default.
None
}
None => config.default_registry(),
}
(Some(r), None) => {
validate_package_name(r, "registry name", "")?;
Some(r.to_string())
}
(Some(_), Some(_)) => {
bail!("both `--index` and `--registry` should not be set at the same time")
}
};
Ok(result)
}
fn index(&self) -> CargoResult<Option<String>> {

View File

@ -1290,6 +1290,25 @@ fn both_index_and_registry() {
}
}
#[cargo_test]
fn both_index_and_default() {
let p = project().file("src/lib.rs", "").build();
for cmd in &[
"publish",
"owner",
"search",
"yank --version 1.0.0",
"install foo",
] {
p.cargo(cmd)
.env("CARGO_REGISTRY_DEFAULT", "undefined")
.arg(format!("--index=index_url"))
.with_status(101)
.with_stderr("[ERROR] invalid url `index_url`: relative URL without a base")
.run();
}
}
#[cargo_test]
fn sparse_lockfile() {
let _registry = registry::RegistryBuilder::new()