Skip to content

Commit

Permalink
Auto merge of #11302 - arlosi:install-default, r=weihanglo
Browse files Browse the repository at this point in the history
Fix cargo install --index when used with registry.default

Setting `registry.default` causes the `args.registry` call to return the default registry as if it were passed through `--registry`, which leaves the `--index` argument ignored in `cargo install`, since `registry` is checked first.

Fixes #11301 by checking for `index` before `registry`.

Note that if you try to pass both `--index` and `--registry`, then a command-line parser error (correctly) occurs:
```
The argument '--registry <REGISTRY>' cannot be used with '--index <INDEX>'
```
  • Loading branch information
bors committed Nov 16, 2022
2 parents 16b0978 + 24bf873 commit b690ab4
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 b690ab4

Please sign in to comment.