If multiple indices contain the same version, use the first index (#5288)

This fixes resolving packages that publish an invalid stub to pypi, such
as tensorrt-llm.

## Summary

In https://github.com/astral-sh/uv/pull/3138 , we implemented
`unsafe-best-match`. However, it seems to not quite work as expected.
When multiple indices contain the same version, it's not clear which
index the current code uses. This PR fixes that to use the first index
the package is in.

## Test Plan

```console
$ echo 'tensorrt-llm==0.11.0' | ./target/debug/uv pip compile - --extra-index-url https://pypi.nvidia.com --python-version=3.10 --index-strategy=unsafe-best-match --annotation-style=line
```
This commit is contained in:
Yorick 2024-07-22 20:03:30 +02:00 committed by GitHub
parent 6492f1a897
commit 0c4627e2f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -232,8 +232,20 @@ impl CandidateSelector {
Self::select_candidate( Self::select_candidate(
version_maps version_maps
.iter() .iter()
.map(|version_map| version_map.iter().rev()) .enumerate()
.kmerge_by(|(version1, _), (version2, _)| version1 > version2), .map(|(map_index, version_map)| {
version_map.iter().rev().map(move |item| (map_index, item))
})
.kmerge_by(
|(index1, (version1, _)), (index2, (version2, _))| match version1
.cmp(version2)
{
std::cmp::Ordering::Equal => index1 < index2,
std::cmp::Ordering::Less => false,
std::cmp::Ordering::Greater => true,
},
)
.map(|(_, item)| item),
package_name, package_name,
range, range,
allow_prerelease, allow_prerelease,
@ -242,8 +254,20 @@ impl CandidateSelector {
Self::select_candidate( Self::select_candidate(
version_maps version_maps
.iter() .iter()
.map(VersionMap::iter) .enumerate()
.kmerge_by(|(version1, _), (version2, _)| version1 < version2), .map(|(map_index, version_map)| {
version_map.iter().map(move |item| (map_index, item))
})
.kmerge_by(
|(index1, (version1, _)), (index2, (version2, _))| match version1
.cmp(version2)
{
std::cmp::Ordering::Equal => index1 < index2,
std::cmp::Ordering::Less => true,
std::cmp::Ordering::Greater => false,
},
)
.map(|(_, item)| item),
package_name, package_name,
range, range,
allow_prerelease, allow_prerelease,