Skip to content

Commit

Permalink
Fix cargo install --index when used with registry.default
Browse files Browse the repository at this point in the history
  • Loading branch information
arlosi committed Nov 15, 2022
1 parent 16b0978 commit 24bf873
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
};
Expand Down
5 changes: 1 addition & 4 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down
22 changes: 16 additions & 6 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down
19 changes: 19 additions & 0 deletions tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 24bf873

Please sign in to comment.