From 24bf873c83a902c1a26c0b7e1ab0df64c96135ef Mon Sep 17 00:00:00 2001 From: Arlo Siemsen Date: Thu, 27 Oct 2022 10:59:07 -0500 Subject: [PATCH] Fix cargo install --index when used with registry.default --- src/bin/cargo/commands/install.rs | 4 ++-- src/cargo/ops/registry.rs | 5 +---- src/cargo/util/command_prelude.rs | 22 ++++++++++++++++------ tests/testsuite/alt_registry.rs | 19 +++++++++++++++++++ 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 22e297a1b3a..790bfd2d7bc 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -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, ®istry)? } else if let Some(index) = args.get_one::("index") { SourceId::for_registry(&index.into_url()?)? + } else if let Some(registry) = args.registry(config)? { + SourceId::alt_registry(config, ®istry)? } else { SourceId::crates_io(config)? }; diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 30d7546cc84..cc37222f8c0 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -1031,11 +1031,8 @@ fn get_source_id( ) -> CargoResult { 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)? diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 15d9e61ede4..2145dbdeee7 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -665,13 +665,23 @@ pub trait ArgMatchesExt { } fn registry(&self, config: &Config) -> CargoResult> { - 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> { diff --git a/tests/testsuite/alt_registry.rs b/tests/testsuite/alt_registry.rs index b4a896dff38..e041a9a7a74 100644 --- a/tests/testsuite/alt_registry.rs +++ b/tests/testsuite/alt_registry.rs @@ -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()