Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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