Skip to content

Commit 5e05a62

Browse files
Respect index priority when storing credentials (#8256)
## Summary Closes #8248.
1 parent 31bf8eb commit 5e05a62

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

crates/uv-distribution-types/src/index_url.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{Index, Verbatim};
1515

1616
static PYPI_URL: LazyLock<Url> = LazyLock::new(|| Url::parse("https://pypi.org/simple").unwrap());
1717

18-
static DEFAULT_INDEX_URL: LazyLock<Index> = LazyLock::new(|| {
18+
static DEFAULT_INDEX: LazyLock<Index> = LazyLock::new(|| {
1919
Index::from_index_url(IndexUrl::Pypi(VerbatimUrl::from_url(PYPI_URL.clone())))
2020
});
2121

@@ -235,7 +235,7 @@ impl<'a> IndexLocations {
235235
.iter()
236236
.filter(move |index| index.name.as_ref().map_or(true, |name| seen.insert(name)))
237237
.find(|index| index.default && !index.explicit)
238-
.or_else(|| Some(&DEFAULT_INDEX_URL))
238+
.or_else(|| Some(&DEFAULT_INDEX))
239239
}
240240
}
241241

@@ -299,16 +299,41 @@ impl<'a> IndexLocations {
299299
}
300300
}
301301

302-
/// Return an iterator over all allowed [`Index`] entries.
302+
/// Return a vector containing all allowed [`Index`] entries.
303303
///
304-
/// This includes explicit indexes, implicit indexes flat indexes, and the default index.
304+
/// This includes explicit indexes, implicit indexes, flat indexes, and the default index.
305305
///
306-
/// If `no_index` was enabled, then this always returns an empty iterator.
307-
pub fn allowed_indexes(&'a self) -> impl Iterator<Item = &'a Index> + 'a {
308-
self.flat_indexes()
309-
.chain(self.explicit_indexes())
310-
.chain(self.implicit_indexes())
311-
.chain(self.default_index())
306+
/// The indexes will be returned in the order in which they were defined, such that the
307+
/// last-defined index is the last item in the vector.
308+
pub fn allowed_indexes(&'a self) -> Vec<&'a Index> {
309+
if self.no_index {
310+
self.flat_index.iter().rev().collect()
311+
} else {
312+
let mut indexes = vec![];
313+
314+
let mut seen = FxHashSet::default();
315+
let mut default = false;
316+
for index in {
317+
self.indexes
318+
.iter()
319+
.chain(self.flat_index.iter())
320+
.filter(move |index| index.name.as_ref().map_or(true, |name| seen.insert(name)))
321+
} {
322+
if index.default && !index.explicit {
323+
if default {
324+
continue;
325+
}
326+
default = true;
327+
}
328+
indexes.push(index);
329+
}
330+
if !default {
331+
indexes.push(&*DEFAULT_INDEX);
332+
}
333+
334+
indexes.reverse();
335+
indexes
336+
}
312337
}
313338
}
314339

@@ -337,7 +362,7 @@ impl<'a> IndexUrls {
337362
.iter()
338363
.filter(move |index| index.name.as_ref().map_or(true, |name| seen.insert(name)))
339364
.find(|index| index.default && !index.explicit)
340-
.or_else(|| Some(&DEFAULT_INDEX_URL))
365+
.or_else(|| Some(&DEFAULT_INDEX))
341366
}
342367
}
343368

crates/uv-distribution/src/index/registry_wheel_index.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::hash_map::Entry;
22

3-
use rustc_hash::FxHashMap;
3+
use rustc_hash::{FxHashMap, FxHashSet};
44

55
use uv_cache::{Cache, CacheBucket, WheelCache};
66
use uv_distribution_types::{CachedRegistryDist, Hashed, Index, IndexLocations, IndexUrl};
@@ -82,7 +82,12 @@ impl<'a> RegistryWheelIndex<'a> {
8282
) -> Vec<IndexEntry<'index>> {
8383
let mut entries = vec![];
8484

85+
let mut seen = FxHashSet::default();
8586
for index in index_locations.allowed_indexes() {
87+
if !seen.insert(index.url()) {
88+
continue;
89+
}
90+
8691
// Index all the wheels that were downloaded directly from the registry.
8792
let wheel_dir = cache.shard(
8893
CacheBucket::Wheels,

crates/uv-resolver/src/lock/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,7 @@ impl Lock {
10581058
let remotes = indexes.map(|locations| {
10591059
locations
10601060
.allowed_indexes()
1061+
.into_iter()
10611062
.filter_map(|index| match index.url() {
10621063
IndexUrl::Pypi(_) | IndexUrl::Url(_) => {
10631064
Some(UrlString::from(index.url().redacted()))
@@ -1070,6 +1071,7 @@ impl Lock {
10701071
let locals = indexes.map(|locations| {
10711072
locations
10721073
.allowed_indexes()
1074+
.into_iter()
10731075
.filter_map(|index| match index.url() {
10741076
IndexUrl::Pypi(_) | IndexUrl::Url(_) => None,
10751077
IndexUrl::Path(url) => {

0 commit comments

Comments
 (0)