Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 5 additions & 1 deletion crates/xtask-bump-check/src/xtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use cargo::core::Registry;
use cargo::core::SourceId;
use cargo::core::Workspace;
use cargo::core::dependency::Dependency;
use cargo::sources::IndexSummary;
use cargo::sources::source::QueryKind;
use cargo::util::cache_lock::CacheLockMode;
use cargo::util::command_prelude::*;
Expand Down Expand Up @@ -445,7 +446,10 @@ fn check_crates_io<'a>(
let query = Dependency::parse(*name, Some(&version_req), source_id)?;
// Exact to avoid returning all for path/git
let possibilities =
futures::executor::block_on(registry.query_vec(&query, QueryKind::Exact))?;
futures::executor::block_on(registry.query_vec(&query, QueryKind::Exact))?
.into_iter()
.filter(|s| matches!(s, IndexSummary::Candidate(_)))
.collect::<Vec<_>>();
if possibilities.is_empty() {
tracing::trace!("dep `{name}` has no version greater than or equal to `{current}`");
} else {
Expand Down
6 changes: 5 additions & 1 deletion src/cargo/core/compiler/future_incompat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

use crate::core::compiler::BuildContext;
use crate::core::{Dependency, PackageId, Workspace};
use crate::sources::IndexSummary;
use crate::sources::SourceConfigMap;
use crate::sources::source::QueryKind;
use crate::util::CargoResult;
Expand Down Expand Up @@ -342,7 +343,10 @@ fn get_updates(ws: &Workspace<'_>, package_ids: &BTreeSet<PackageId>) -> Option<
for (pkg_id, summaries) in summaries {
let mut updated_versions: Vec<_> = summaries
.iter()
.map(|summary| summary.as_summary().version())
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s.version()),
_ => None,
})
.filter(|version| *version > pkg_id.version())
.collect();
updated_versions.sort();
Expand Down
41 changes: 19 additions & 22 deletions src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ pub struct PackageRegistry<'gctx> {
/// This is constructed via [`PackageRegistry::register_lock`].
/// See also [`LockedMap`].
locked: LockedMap,
/// Packages allowed to be used, even if they are yanked.
yanked_whitelist: RefCell<HashSet<PackageId>>,
source_config: SourceConfigMap<'gctx>,

/// Patches registered during calls to [`PackageRegistry::patch`].
Expand Down Expand Up @@ -203,7 +201,6 @@ impl<'gctx> PackageRegistry<'gctx> {
overrides: RefCell::new(Vec::new()),
source_config,
locked: HashMap::new(),
yanked_whitelist: RefCell::new(HashSet::new()),
patches: HashMap::new(),
patches_locked: false,
patches_available: HashMap::new(),
Expand Down Expand Up @@ -284,15 +281,6 @@ impl<'gctx> PackageRegistry<'gctx> {
self.add_source(source, Kind::Override);
}

/// Allows a group of package to be available to query even if they are yanked.
pub fn add_to_yanked_whitelist(&self, iter: impl Iterator<Item = PackageId>) {
let pkgs = iter.collect::<Vec<_>>();
for (_, source) in self.sources.borrow().iter() {
source.add_to_yanked_whitelist(&pkgs);
}
self.yanked_whitelist.borrow_mut().extend(pkgs);
}

/// remove all residual state from previous lock files.
pub fn clear_lock(&mut self) {
trace!("clear_lock");
Expand Down Expand Up @@ -414,7 +402,9 @@ impl<'gctx> PackageRegistry<'gctx> {
let mut summaries = Vec::new();
source
.query(&dep, QueryKind::Exact, &mut |s| {
summaries.push(s.into_summary())
if let IndexSummary::Candidate(summary) = s {
summaries.push(summary)
}
})
.await
.with_context(|| format!("unable to update {}", source.source_id()))
Expand Down Expand Up @@ -537,12 +527,6 @@ impl<'gctx> PackageRegistry<'gctx> {
.with_context(|| format!("unable to update {}", source_id))?;
assert_eq!(source.source_id(), source_id);

let yanked_whitelist = self.yanked_whitelist.borrow();
if !yanked_whitelist.is_empty() {
let pkgs: Vec<_> = yanked_whitelist.iter().copied().collect();
source.add_to_yanked_whitelist(&pkgs);
}

if kind == Kind::Override {
self.overrides.borrow_mut().push(source_id);
}
Expand Down Expand Up @@ -577,7 +561,11 @@ impl<'gctx> PackageRegistry<'gctx> {
.borrow()
.get(s)
.unwrap()
.query(&dep, QueryKind::Exact, &mut |s| results = Some(s))
.query(&dep, QueryKind::Exact, &mut |s| {
if let IndexSummary::Candidate(_) = &s {
results = Some(s);
}
})
.await?;
if results.is_some() {
return Ok(results);
Expand Down Expand Up @@ -991,7 +979,13 @@ async fn summary_for_patch(
Vec::new()
});

let orig_matches = orig_matches.into_iter().map(|s| s.into_summary()).collect();
let orig_matches = orig_matches
.into_iter()
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
})
.collect();

let summary = Box::pin(summary_for_patch(
original_patch,
Expand Down Expand Up @@ -1022,7 +1016,10 @@ async fn summary_for_patch(
});
let mut vers = name_summaries
.iter()
.map(|summary| summary.as_summary().version())
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s.version()),
_ => None,
})
.collect::<Vec<_>>();
let found = match vers.len() {
0 => "".to_string(),
Expand Down
45 changes: 31 additions & 14 deletions src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::core::resolver::{
use crate::core::{
Dependency, FeatureValue, PackageId, PackageIdSpec, PackageIdSpecQuery, Registry, Summary,
};
use crate::sources::IndexSummary;
use crate::sources::source::QueryKind;
use crate::util::LocalPollAdapter;
use crate::util::closest_msg;
Expand Down Expand Up @@ -68,8 +69,23 @@ impl<'a, T: Registry> RegistryQueryerAsync<'a, T> {
let (dep, first_version) = key;
let mut summaries = Vec::new();
self.registry
.query(dep, QueryKind::Exact, &mut |s| {
summaries.push(s.into_summary());
.query(dep, QueryKind::Exact, &mut |s| match s {
IndexSummary::Candidate(summary) => summaries.push(summary),
// Prefer yanked only when
//
// * it is recorded in lock file or a `[patch]` entry
// * it is specified in `cargo update --precise`
IndexSummary::Yanked(summary) => {
let pkg_id = summary.package_id();
let allow_precise = pkg_id
.source_id()
.precise_registry_version(pkg_id.name().as_str())
.is_some_and(|(_, to)| to == pkg_id.version());
if allow_precise || self.version_prefs.should_prefer(&pkg_id) {
Comment on lines +74 to +84

@epage epage Jun 10, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this beyond what the current code does for this to be just a refactor?

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, its because this logic got moved up into this layer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, why isn't this needed at other call sites?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, is it because this code is only active within this branch?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you answered yourself :)

summaries.push(summary);
}
}
_ => {}
})
.await?;

Expand All @@ -92,20 +108,21 @@ impl<'a, T: Registry> RegistryQueryerAsync<'a, T> {
.registry
.query_vec(dep, QueryKind::Exact)
.await?
.into_iter();
let s = summaries
.next()
.ok_or_else(|| {
anyhow::format_err!(
"no matching package for override `{}` found\n\
.into_iter()
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
});
let s = summaries.next().ok_or_else(|| {
anyhow::format_err!(
"no matching package for override `{}` found\n\
location searched: {}\n\
version required: {}",
spec,
dep.source_id(),
dep.version_req()
)
})?
.into_summary();
spec,
dep.source_id(),
dep.version_req()
)
})?;
let summaries = summaries.collect::<Vec<_>>();
if !summaries.is_empty() {
let bullets = summaries
Expand Down
8 changes: 7 additions & 1 deletion src/cargo/core/resolver/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,13 @@ fn alt_versions(registry: &impl Registry, dep: &Dependency) -> Option<CargoResul
Ok(candidates) => candidates,
Err(e) => return Some(Err(e)),
};
let mut candidates: Vec<_> = candidates.into_iter().map(|s| s.into_summary()).collect();
let mut candidates: Vec<_> = candidates
.into_iter()
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
})
.collect();
candidates.sort_unstable_by(|a, b| b.version().cmp(a.version()));
if candidates.is_empty() {
None
Expand Down
22 changes: 12 additions & 10 deletions src/cargo/core/resolver/version_prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ impl VersionPreferences {
self.publish_time = Some(publish_time);
}

/// Whether the given package is preferred.
pub fn should_prefer(&self, pkg_id: &PackageId) -> bool {
self.try_to_use.contains(pkg_id)
|| self
.prefer_patch_deps
.get(&pkg_id.name())
.map(|deps| deps.iter().any(|d| d.matches_id(*pkg_id)))
.unwrap_or(false)
}

/// Sort (and filter) the given vector of summaries in-place
///
/// Note: all summaries presumed to be for the same package.
Expand All @@ -75,14 +85,6 @@ impl VersionPreferences {
summaries: &mut Vec<Summary>,
first_version: Option<VersionOrdering>,
) {
let should_prefer = |pkg_id: &PackageId| {
self.try_to_use.contains(pkg_id)
|| self
.prefer_patch_deps
.get(&pkg_id.name())
.map(|deps| deps.iter().any(|d| d.matches_id(*pkg_id)))
.unwrap_or(false)
};
if let Some(max_publish_time) = self.publish_time {
summaries.retain(|s| {
if let Some(summary_publish_time) = s.pubtime() {
Expand All @@ -93,8 +95,8 @@ impl VersionPreferences {
});
}
summaries.sort_unstable_by(|a, b| {
let prefer_a = should_prefer(&a.package_id());
let prefer_b = should_prefer(&b.package_id());
let prefer_a = self.should_prefer(&a.package_id());
let prefer_b = self.should_prefer(&b.package_id());
let previous_cmp = prefer_a.cmp(&prefer_b).reverse();
if previous_cmp != Ordering::Equal {
return previous_cmp;
Expand Down
3 changes: 0 additions & 3 deletions src/cargo/core/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,6 @@ impl SourceId {
}

/// Creates an implementation of `Source` corresponding to this ID.
///
/// To allow yanked packages through queries,
/// call [`Source::add_to_yanked_whitelist`] on the returned source.
pub fn load<'a>(self, gctx: &'a GlobalContext) -> CargoResult<Box<dyn Source + 'a>> {
trace!("loading SourceId; {}", self);
match self.inner.kind {
Expand Down
16 changes: 13 additions & 3 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::core::Workspace;
use crate::core::dependency::DepKind;
use crate::core::registry::PackageRegistry;
use crate::ops::resolve_ws;
use crate::sources::IndexSummary;
use crate::sources::source::QueryKind;
use crate::util::OptVersionReq;
use crate::util::cache_lock::CacheLockMode;
Expand Down Expand Up @@ -838,7 +839,10 @@ fn get_latest_dependency(

let mut possibilities: Vec<_> = possibilities
.into_iter()
.map(|s| s.into_summary())
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
})
.collect();

possibilities.sort_by_key(|s| {
Expand Down Expand Up @@ -964,7 +968,10 @@ fn select_package(

let possibilities: Vec<_> = possibilities
.into_iter()
.map(|s| s.into_summary())
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
})
.collect();

match possibilities.len() {
Expand Down Expand Up @@ -1191,7 +1198,10 @@ fn populate_available_features(
// in the lock file for a given version requirement.
let lowest_common_denominator = possibilities
.iter()
.map(|s| s.as_summary())
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
})
.min_by_key(|s| {
// Fallback to a pre-release if no official release is available by sorting them as
// more.
Expand Down
25 changes: 20 additions & 5 deletions src/cargo/ops/cargo_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ fn upgrade_dependency(
let latest = if !possibilities.is_empty() {
possibilities
.iter()
.map(|s| s.as_summary())
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
})
.map(|s| s.version())
.filter(|v| !v.is_prerelease())
.max()
Expand Down Expand Up @@ -799,7 +802,10 @@ fn report_latest(possibilities: &[IndexSummary], change: &PackageChange) -> Opti

let compat_ver_compat_msrv_summary = possibilities
.iter()
.map(|s| s.as_summary())
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
})
.filter(|s| {
if let (Some(summary_rust_version), Some(required_rust_version)) =
(s.rust_version(), required_rust_version)
Expand All @@ -821,7 +827,10 @@ fn report_latest(possibilities: &[IndexSummary], change: &PackageChange) -> Opti
if !change.is_transitive.unwrap_or(true) {
let incompat_ver_compat_msrv_summary = possibilities
.iter()
.map(|s| s.as_summary())
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
})
.filter(|s| {
if let (Some(summary_rust_version), Some(required_rust_version)) =
(s.rust_version(), required_rust_version)
Expand All @@ -843,7 +852,10 @@ fn report_latest(possibilities: &[IndexSummary], change: &PackageChange) -> Opti

let compat_ver_summary = possibilities
.iter()
.map(|s| s.as_summary())
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
})
.filter(|s| package_id.version() != s.version() && version_req.matches(s.version()))
.max_by_key(|s| s.version());
if let Some(summary) = compat_ver_summary {
Expand All @@ -860,7 +872,10 @@ fn report_latest(possibilities: &[IndexSummary], change: &PackageChange) -> Opti
if !change.is_transitive.unwrap_or(true) {
let incompat_ver_summary = possibilities
.iter()
.map(|s| s.as_summary())
.filter_map(|s| match s {
IndexSummary::Candidate(s) => Some(s),
_ => None,
})
.filter(|s| is_latest(s.version(), package_id.version()))
.max_by_key(|s| s.version());
if let Some(summary) = incompat_ver_summary {
Expand Down
Loading