Skip to content

Commit 24bf873

Browse files
committed
Fix cargo install --index when used with registry.default
1 parent 16b0978 commit 24bf873

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

src/bin/cargo/commands/install.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
126126
} else if krates.is_empty() {
127127
from_cwd = true;
128128
SourceId::for_path(config.cwd())?
129-
} else if let Some(registry) = args.registry(config)? {
130-
SourceId::alt_registry(config, &registry)?
131129
} else if let Some(index) = args.get_one::<String>("index") {
132130
SourceId::for_registry(&index.into_url()?)?
131+
} else if let Some(registry) = args.registry(config)? {
132+
SourceId::alt_registry(config, &registry)?
133133
} else {
134134
SourceId::crates_io(config)?
135135
};

src/cargo/ops/registry.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,11 +1031,8 @@ fn get_source_id(
10311031
) -> CargoResult<RegistrySourceIds> {
10321032
let sid = match (reg, index) {
10331033
(None, None) => SourceId::crates_io(config)?,
1034+
(_, Some(i)) => SourceId::for_registry(&i.into_url()?)?,
10341035
(Some(r), None) => SourceId::alt_registry(config, r)?,
1035-
(None, Some(i)) => SourceId::for_registry(&i.into_url()?)?,
1036-
(Some(_), Some(_)) => {
1037-
bail!("both `--index` and `--registry` should not be set at the same time")
1038-
}
10391036
};
10401037
// Load source replacements that are built-in to Cargo.
10411038
let builtin_replacement_sid = SourceConfigMap::empty(config)?

src/cargo/util/command_prelude.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -665,13 +665,23 @@ pub trait ArgMatchesExt {
665665
}
666666

667667
fn registry(&self, config: &Config) -> CargoResult<Option<String>> {
668-
match self._value_of("registry") {
669-
Some(registry) => {
670-
validate_package_name(registry, "registry name", "")?;
671-
Ok(Some(registry.to_string()))
668+
let registry = self._value_of("registry");
669+
let index = self._value_of("index");
670+
let result = match (registry, index) {
671+
(None, None) => config.default_registry()?,
672+
(None, Some(_)) => {
673+
// If --index is set, then do not look at registry.default.
674+
None
672675
}
673-
None => config.default_registry(),
674-
}
676+
(Some(r), None) => {
677+
validate_package_name(r, "registry name", "")?;
678+
Some(r.to_string())
679+
}
680+
(Some(_), Some(_)) => {
681+
bail!("both `--index` and `--registry` should not be set at the same time")
682+
}
683+
};
684+
Ok(result)
675685
}
676686

677687
fn index(&self) -> CargoResult<Option<String>> {

tests/testsuite/alt_registry.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,25 @@ fn both_index_and_registry() {
12901290
}
12911291
}
12921292

1293+
#[cargo_test]
1294+
fn both_index_and_default() {
1295+
let p = project().file("src/lib.rs", "").build();
1296+
for cmd in &[
1297+
"publish",
1298+
"owner",
1299+
"search",
1300+
"yank --version 1.0.0",
1301+
"install foo",
1302+
] {
1303+
p.cargo(cmd)
1304+
.env("CARGO_REGISTRY_DEFAULT", "undefined")
1305+
.arg(format!("--index=index_url"))
1306+
.with_status(101)
1307+
.with_stderr("[ERROR] invalid url `index_url`: relative URL without a base")
1308+
.run();
1309+
}
1310+
}
1311+
12931312
#[cargo_test]
12941313
fn sparse_lockfile() {
12951314
let _registry = registry::RegistryBuilder::new()

0 commit comments

Comments
 (0)