cargo/core/registry.rs
1//! Types that hold source information for a group of packages.
2//!
3//! The primary type you're looking for is [`PackageRegistry`]. It is an
4//! abstraction over multiple [`Source`]s. [`PackageRegistry`] also implements
5//! the [`Registry`] trait, allowing a dependency resolver to query necessary
6//! package metadata (i.e., [Summary]) from it.
7//!
8//! Not to be confused with [`crate::sources::registry`] and [`crate::ops::registry`].
9//! The former is just one kind of source,
10//! while the latter involves operations on the registry Web API.
11
12use std::cell::RefCell;
13use std::collections::{HashMap, HashSet};
14
15use crate::core::{Dependency, PackageId, PackageSet, Patch, SourceId, Summary};
16use crate::sources::IndexSummary;
17use crate::sources::config::SourceConfigMap;
18use crate::sources::source::QueryKind;
19use crate::sources::source::Source;
20use crate::sources::source::SourceMap;
21use crate::util::errors::CargoResult;
22use crate::util::interning::InternedString;
23use crate::util::{CanonicalUrl, GlobalContext};
24use anyhow::Context as _;
25use cargo_util_terminal::report::Level;
26use futures::stream::FuturesUnordered;
27use itertools::Itertools;
28use tracing::{debug, trace};
29use url::Url;
30
31/// An abstraction provides a set of methods for querying source information
32/// about a group of packages, without leaking too much implementation details
33/// of the actual registry.
34///
35/// As of 2024-04, only [`PackageRegistry`] and `MyRegistry` in resolver-tests
36/// are found implementing this.
37///
38/// See also the [`Source`] trait, as many of the methods here mirror and
39/// abstract over its functionalities.
40#[allow(async_fn_in_trait)]
41pub trait Registry {
42 /// Attempt to find the packages that match a dependency request.
43 async fn query(
44 &self,
45 dep: &Dependency,
46 kind: QueryKind,
47 f: &mut dyn FnMut(IndexSummary),
48 ) -> CargoResult<()>;
49
50 /// Gathers the result from [`Registry::query`] as a list of [`IndexSummary`] items
51 /// when they become available.
52 async fn query_vec(&self, dep: &Dependency, kind: QueryKind) -> CargoResult<Vec<IndexSummary>> {
53 let mut ret = Vec::new();
54 self.query(dep, kind, &mut |s| ret.push(s))
55 .await
56 .map(|()| ret)
57 }
58
59 /// Gets the description of a source, to provide useful messages.
60 fn describe_source(&self, source: SourceId) -> String;
61
62 /// Checks if a source is replaced with some other source.
63 fn is_replaced(&self, source: SourceId) -> bool;
64}
65
66/// This structure represents a registry of known packages. It internally
67/// contains a number of [`Source`] instances which are used to load a
68/// [`Package`] from.
69///
70/// The resolution phase of Cargo uses this to drive knowledge about new
71/// packages as well as querying for lists of new packages.
72/// It is here that sources are updated (e.g., network operations) and
73/// overrides/patches are handled.
74///
75/// The general idea behind this registry is that it is centered around the
76/// [`SourceMap`] structure, contained within which is a mapping of a [`SourceId`]
77/// to a [`Source`]. Each [`Source`] in the map has been updated (using network
78/// operations if necessary) and is ready to be queried for packages.
79///
80/// [`Package`]: crate::core::Package
81pub struct PackageRegistry<'gctx> {
82 gctx: &'gctx GlobalContext,
83 sources: RefCell<SourceMap<'gctx>>,
84
85 /// A list of sources which are considered "path-overrides" which take
86 /// precedent when querying for packages.
87 overrides: RefCell<Vec<SourceId>>,
88
89 /// Use for tracking sources that are already loaded into the registry.
90 // Note that each SourceId does not take into account its `precise` field
91 // when hashing or testing for equality. When adding a new `SourceId`, we
92 // want to avoid duplicates in the `SourceMap` (to prevent re-updating the
93 // same git repo twice for example), but we also want to ensure that the
94 // loaded source is always updated.
95 //
96 // Sources with a `precise` field normally don't need to be updated because
97 // their contents are already on disk, but sources without a `precise` field
98 // almost always need to be updated. If we have a cached `Source` for a
99 // precise `SourceId`, then when we add a new `SourceId` that is not precise
100 // we want to ensure that the underlying source is updated.
101 //
102 // This is basically a long-winded way of saying that we want to know
103 // precisely what the keys of `sources` are, so this is a mapping of key to
104 // what exactly the key is.
105 source_ids: RefCell<HashMap<SourceId, (SourceId, Kind)>>,
106
107 /// This is constructed via [`PackageRegistry::register_lock`].
108 /// See also [`LockedMap`].
109 locked: LockedMap,
110 source_config: SourceConfigMap<'gctx>,
111
112 /// Patches registered during calls to [`PackageRegistry::patch`].
113 ///
114 /// These are available for `query` after calling [`PackageRegistry::lock_patches`],
115 /// which `lock`s them all to specific versions.
116 patches: HashMap<CanonicalUrl, Vec<Summary>>,
117 /// Whether patches are locked. That is, they are available to resolution.
118 ///
119 /// See [`PackageRegistry::lock_patches`] and [`PackageRegistry::patch`] for more.
120 patches_locked: bool,
121 /// Patches available for each source.
122 ///
123 /// This is for determining whether a dependency entry from a lockfile
124 /// happened through `[patch]`, during calls to [`lock`] to rewrite
125 /// summaries to point directly at these patched entries.
126 ///
127 /// This is constructed during calls to [`PackageRegistry::patch`],
128 /// along with the `patches` field, thoough these entries never get locked.
129 patches_available: HashMap<CanonicalUrl, Vec<PackageId>>,
130}
131
132/// A map of all "locked packages" which is filled in when parsing a lock file
133/// and is used to guide dependency resolution by altering summaries as they're
134/// queried from this source.
135///
136/// This map can be thought of as a glorified `Vec<MySummary>` where `MySummary`
137/// has a `PackageId` for which package it represents as well as a list of
138/// `PackageId` for the resolved dependencies. The hash map is otherwise
139/// structured though for easy access throughout this registry.
140type LockedMap = HashMap<
141 // The first level of key-ing done in this hash map is the source that
142 // dependencies come from, identified by a `SourceId`.
143 // The next level is keyed by the name of the package...
144 (SourceId, InternedString),
145 // ... and the value here is a list of tuples. The first element of each
146 // tuple is a package which has the source/name used to get to this
147 // point. The second element of each tuple is the list of locked
148 // dependencies that the first element has.
149 Vec<(PackageId, Vec<PackageId>)>,
150>;
151
152/// Kinds of sources a [`PackageRegistry`] has loaded.
153#[derive(PartialEq, Eq, Clone, Copy)]
154enum Kind {
155 /// A source from a [path override].
156 ///
157 /// [path overrides]: https://doc.rust-lang.org/nightly/cargo/reference/overriding-dependencies.html#paths-overrides
158 Override,
159 /// A source that is locked and not going to change.
160 ///
161 /// For example, sources of workspace members are loaded during the
162 /// workspace initialization, so not allowed to change.
163 Locked,
164 /// A source that is not locked nor a path-override.
165 Normal,
166}
167
168/// This tuple is an argument to [`PackageRegistry::patch`].
169///
170/// * The first element is the patch definition straight from the manifest.
171/// * The second element is an optional variant where the patch has been locked.
172/// It is the patch locked to a specific version found in Cargo.lock.
173/// This will be `None` if `Cargo.lock` doesn't exist,
174/// or the patch did not match any existing entries in `Cargo.lock`.
175pub type PatchDependency<'a> = (&'a Patch, Option<LockedPatchDependency>);
176
177/// Argument to [`PackageRegistry::patch`] which is information about a `[patch]`
178/// directive that we found in a lockfile, if present.
179pub struct LockedPatchDependency {
180 /// The original `Dependency` directive, except "locked" so it's version
181 /// requirement is Locked to `foo` and its `SourceId` has a "precise" listed.
182 pub dependency: Dependency,
183 /// The `PackageId` that was previously found in a lock file which
184 /// `dependency` matches.
185 pub package_id: PackageId,
186 /// Something only used for backwards compatibility with the v2 lock file
187 /// format where `branch=master` is considered the same as `DefaultBranch`.
188 /// For more comments on this see the code in `ops/resolve.rs`.
189 pub alt_package_id: Option<PackageId>,
190}
191
192impl<'gctx> PackageRegistry<'gctx> {
193 pub fn new_with_source_config(
194 gctx: &'gctx GlobalContext,
195 source_config: SourceConfigMap<'gctx>,
196 ) -> CargoResult<PackageRegistry<'gctx>> {
197 Ok(PackageRegistry {
198 gctx,
199 sources: RefCell::new(SourceMap::new()),
200 source_ids: RefCell::new(HashMap::new()),
201 overrides: RefCell::new(Vec::new()),
202 source_config,
203 locked: HashMap::new(),
204 patches: HashMap::new(),
205 patches_locked: false,
206 patches_available: HashMap::new(),
207 })
208 }
209
210 pub fn get(self, package_ids: &[PackageId]) -> CargoResult<PackageSet<'gctx>> {
211 trace!("getting packages; sources={}", self.sources.borrow().len());
212 PackageSet::new(package_ids, self.sources.into_inner(), self.gctx)
213 }
214
215 /// Ensures the [`Source`] of the given [`SourceId`] is loaded.
216 fn ensure_loaded(&self, namespace: SourceId, kind: Kind) -> CargoResult<()> {
217 match self.source_ids.borrow().get(&namespace) {
218 // We've previously loaded this source, and we've already locked it,
219 // so we're not allowed to change it even if `namespace` has a
220 // slightly different precise version listed.
221 Some((_, Kind::Locked)) => {
222 debug!("load/locked {}", namespace);
223 return Ok(());
224 }
225
226 // If the previous source was not a precise source, then we can be
227 // sure that it's already been updated if we've already loaded it.
228 Some((previous, _)) if !previous.has_precise() => {
229 debug!("load/precise {}", namespace);
230 return Ok(());
231 }
232
233 // If the previous source has the same precise version as we do,
234 // then we're done, otherwise we need to move forward
235 // updating this source.
236 Some((previous, _)) => {
237 if previous.has_same_precise_as(namespace) {
238 debug!("load/match {}", namespace);
239 return Ok(());
240 }
241 debug!("load/mismatch {}", namespace);
242 }
243 None => {
244 debug!("load/missing {}", namespace);
245 }
246 }
247
248 self.load(namespace, kind)?;
249
250 // Ensure `shell` is not already in use,
251 // regardless of which source is used and how it happens to behave this time
252 self.gctx.debug_assert_shell_not_borrowed();
253 Ok(())
254 }
255
256 pub fn add_sources(&mut self, ids: impl IntoIterator<Item = SourceId>) -> CargoResult<()> {
257 for id in ids {
258 self.ensure_loaded(id, Kind::Locked)?;
259 }
260 Ok(())
261 }
262
263 /// Adds a source which will be locked.
264 /// Useful for path sources such as the source of a workspace member.
265 pub fn add_preloaded(&mut self, source: Box<dyn Source + 'gctx>) {
266 self.add_source(source, Kind::Locked);
267 }
268
269 /// Adds a source to the registry.
270 fn add_source(&self, source: Box<dyn Source + 'gctx>, kind: Kind) {
271 let id = source.source_id();
272 self.sources.borrow_mut().insert(source);
273 self.source_ids.borrow_mut().insert(id, (id, kind));
274 }
275
276 /// Adds a source from a [path override].
277 ///
278 /// [path override]: https://doc.rust-lang.org/nightly/cargo/reference/overriding-dependencies.html#paths-overrides
279 pub fn add_override(&mut self, source: Box<dyn Source + 'gctx>) {
280 self.overrides.borrow_mut().push(source.source_id());
281 self.add_source(source, Kind::Override);
282 }
283
284 /// remove all residual state from previous lock files.
285 pub fn clear_lock(&mut self) {
286 trace!("clear_lock");
287 self.locked = HashMap::new();
288 }
289
290 /// Registers one "locked package" to the registry, for guiding the
291 /// dependency resolution. See [`LockedMap`] for more.
292 pub fn register_lock(&mut self, id: PackageId, deps: Vec<PackageId>) {
293 trace!("register_lock: {}", id);
294 for dep in deps.iter() {
295 trace!("\t-> {}", dep);
296 }
297 let sub_vec = self
298 .locked
299 .entry((id.source_id(), id.name()))
300 .or_insert_with(Vec::new);
301 sub_vec.push((id, deps));
302 }
303
304 /// Insert a `[patch]` section into this registry.
305 ///
306 /// This method will insert a `[patch]` section for the `url` specified,
307 /// with the given list of dependencies. The `url` specified is the URL of
308 /// the source to patch (for example this is `crates-io` in the manifest).
309 /// The `deps` is an array of all the entries in the `[patch]` section of
310 /// the manifest.
311 ///
312 /// Here the `patch_deps` will be resolved to a precise version and stored
313 /// internally for future calls to `query` below.
314 ///
315 /// Note that the patch list specified here *will not* be available to
316 /// [`Registry::query`] until [`PackageRegistry::lock_patches`] is called
317 /// below, which should be called once all patches have been added.
318 ///
319 /// The return value is a `Vec` of patches that should *not* be locked.
320 /// This happens when the patch is locked, but the patch has been updated
321 /// so the locked value is no longer correct.
322 #[tracing::instrument(skip(self, patch_deps))]
323 pub fn patch(
324 &mut self,
325 url: &Url,
326 patch_deps: &[PatchDependency<'_>],
327 ) -> CargoResult<Vec<(Patch, PackageId)>> {
328 // NOTE: None of this code is aware of required features. If a patch
329 // is missing a required feature, you end up with an "unused patch"
330 // warning, which is very hard to understand. Ideally the warning
331 // would be tailored to indicate *why* it is unused.
332 let canonical = CanonicalUrl::new(url)?;
333
334 // Return value of patches that shouldn't be locked.
335 let mut unlock_patches = Vec::new();
336
337 // First up we need to actually resolve each `patch_deps` specification
338 // to precisely one summary. We're not using the `query` method below
339 // as it internally uses maps we're building up as part of this method
340 // (`patches_available` and `patches`). Instead we're going straight to
341 // the source to load information from it.
342 //
343 // Remember that each dependency listed in `[patch]` has to resolve to
344 // precisely one package, so that's why we're just creating a flat list
345 // of summaries which should be the same length as `deps` above.
346
347 let pending = FuturesUnordered::new();
348
349 for (orig_patch, locked) in patch_deps {
350 // Use the locked patch if it exists, otherwise use the original.
351 let dep = match locked {
352 Some(lock) => &lock.dependency,
353 None => &orig_patch.dep,
354 };
355 debug!(
356 "registering a patch for `{}` with `{}`",
357 url,
358 dep.package_name()
359 );
360
361 let mut unused_fields = Vec::new();
362 if dep.features().len() != 0 {
363 unused_fields.push("`features`");
364 }
365 if !dep.uses_default_features() {
366 unused_fields.push("`default-features`")
367 }
368 if !unused_fields.is_empty() {
369 self.source_config.gctx().shell().print_report(
370 &[Level::WARNING
371 .secondary_title(format!(
372 "unused field in patch for `{}`: {}",
373 dep.package_name(),
374 unused_fields.join(", ")
375 ))
376 .element(Level::HELP.message(format!(
377 "configure {} in the `dependencies` entry",
378 unused_fields.join(", ")
379 )))],
380 false,
381 )?;
382 }
383
384 // Go straight to the source for resolving `dep`. Load it as we
385 // normally would and then ask it directly for the list of summaries
386 // corresponding to this `dep`.
387 self.ensure_loaded(dep.source_id(), Kind::Normal)
388 .with_context(|| {
389 format!(
390 "failed to load source for dependency `{}`",
391 dep.package_name()
392 )
393 })?;
394
395 let source = self
396 .sources
397 .borrow()
398 .get(dep.source_id())
399 .expect("loaded source not present")
400 .clone();
401 pending.push(async move {
402 let mut summaries = Vec::new();
403 source
404 .query(&dep, QueryKind::Exact, &mut |s| {
405 if let IndexSummary::Candidate(summary) = s {
406 summaries.push(summary)
407 }
408 })
409 .await
410 .with_context(|| format!("unable to update {}", source.source_id()))
411 .with_context(|| {
412 format!(
413 "failed to load source for dependency `{}`",
414 dep.package_name()
415 )
416 })?;
417
418 let (summary, should_unlock) =
419 summary_for_patch(&orig_patch, url, &locked, summaries, source.as_ref())
420 .await?;
421 Ok::<_, anyhow::Error>((orig_patch, dep, summary, should_unlock))
422 });
423 }
424
425 let unlocked_summaries = crate::util::block_on_stream(pending).map(|next| {
426 let (orig_patch, dep, summary, should_unlock) = next?;
427 debug!(
428 "patch summary is {:?} should_unlock={:?}",
429 summary, should_unlock
430 );
431 if let Some(unlock_id) = should_unlock {
432 unlock_patches.push(((*orig_patch).clone(), unlock_id));
433 }
434
435 if *summary.package_id().source_id().canonical_url() == canonical {
436 return Err(anyhow::anyhow!(
437 "patch for `{}` points to the same source, but patches must point to different sources\n\
438 help: check `{}` patch definition for `{}` in `{}`",
439 dep.package_name(),
440 dep.package_name(),
441 url,
442 orig_patch.loc
443 ));
444 }
445 Ok(summary)
446 }).collect::<CargoResult<Vec<_>>>()?;
447
448 let mut name_and_version = HashSet::new();
449 for summary in unlocked_summaries.iter() {
450 let name = summary.package_id().name();
451 let version = summary.package_id().version();
452 if !name_and_version.insert((name, version)) {
453 let duplicate_locations = patch_deps
454 .iter()
455 .filter(|&p| p.0.dep.package_name() == name)
456 .map(|p| format!("`{}`", p.0.loc))
457 .unique()
458 .join(", ");
459 return Err(anyhow::anyhow!(
460 "several `[patch]` entries resolving to same version `{} v{}`\n\
461 help: check `{}` patch definitions for `{}` in {}",
462 name,
463 version,
464 name,
465 url,
466 duplicate_locations
467 ));
468 }
469 }
470
471 // Calculate a list of all patches available for this source.
472 let mut ids = Vec::new();
473 for (summary, (_, lock)) in unlocked_summaries.iter().zip(patch_deps) {
474 ids.push(summary.package_id());
475 // This is subtle where the list of `ids` for a canonical URL is
476 // extend with possibly two ids per summary. This is done to handle
477 // the transition from the v2->v3 lock file format where in v2
478 // DefaultBranch was either DefaultBranch or Branch("master") for
479 // git dependencies. In this case if `summary.package_id()` is
480 // Branch("master") then alt_package_id will be DefaultBranch. This
481 // signifies that there's a patch available for either of those
482 // dependency directives if we see them in the dependency graph.
483 if let Some(lock) = lock {
484 ids.extend(lock.alt_package_id);
485 }
486 }
487 self.patches_available.insert(canonical.clone(), ids);
488
489 // Note that we do not use `lock` here to lock summaries! That step
490 // happens later once `lock_patches` is invoked. In the meantime though
491 // we want to fill in the `patches_available` map (later used in the
492 // `lock` method) and otherwise store the unlocked summaries in
493 // `patches` to get locked in a future call to `lock_patches`.
494 self.patches.insert(canonical, unlocked_summaries);
495
496 Ok(unlock_patches)
497 }
498
499 /// Lock all patch summaries added via [`patch`](Self::patch),
500 /// making them available to resolution via [`Registry::query`].
501 pub fn lock_patches(&mut self) {
502 assert!(!self.patches_locked);
503 for summaries in self.patches.values_mut() {
504 for summary in summaries {
505 debug!("locking patch {:?}", summary);
506 *summary = lock(&self.locked, &self.patches_available, summary.clone());
507 }
508 }
509 self.patches_locked = true;
510 }
511
512 /// Gets all patches grouped by the source URLs they are going to patch.
513 ///
514 /// These patches are mainly collected from [`patch`](Self::patch).
515 /// They might not be the same as patches actually used during dependency resolving.
516 pub fn patches(&self) -> &HashMap<CanonicalUrl, Vec<Summary>> {
517 &self.patches
518 }
519
520 /// Loads the [`Source`] for a given [`SourceId`] to this registry, making
521 /// them available to resolution.
522 fn load(&self, source_id: SourceId, kind: Kind) -> CargoResult<()> {
523 debug!("loading source {}", source_id);
524 let source = self
525 .source_config
526 .load(source_id)
527 .with_context(|| format!("unable to update {}", source_id))?;
528 assert_eq!(source.source_id(), source_id);
529
530 if kind == Kind::Override {
531 self.overrides.borrow_mut().push(source_id);
532 }
533 self.add_source(source, kind);
534
535 // If we have an imprecise version then we don't know what we're going
536 // to look for, so we always attempt to perform an update here.
537 //
538 // If we have a precise version, then we'll update lazily during the
539 // querying phase. Note that precise in this case is only
540 // `"locked"` as other values indicate a `cargo update
541 // --precise` request
542 if !source_id.has_locked_precise() {
543 self.sources
544 .borrow()
545 .get(source_id)
546 .unwrap()
547 .invalidate_cache();
548 } else {
549 debug!("skipping update due to locked registry");
550 }
551 Ok(())
552 }
553
554 /// Queries path overrides from this registry.
555 async fn query_overrides(&self, dep: &Dependency) -> CargoResult<Option<Summary>> {
556 let overrides = self.overrides.borrow();
557 for &s in overrides.iter() {
558 let dep = Dependency::new_override(dep.package_name(), s);
559 let mut results = None;
560 self.sources
561 .borrow()
562 .get(s)
563 .unwrap()
564 .query(&dep, QueryKind::Exact, &mut |s| {
565 if let IndexSummary::Candidate(s) = s {
566 results = Some(s);
567 }
568 })
569 .await?;
570 if results.is_some() {
571 return Ok(results);
572 }
573 }
574 Ok(None)
575 }
576
577 /// This function is used to transform a summary to another locked summary
578 /// if possible. This is where the concept of a lock file comes into play.
579 ///
580 /// If a summary points at a package ID which was previously locked, then we
581 /// override the summary's ID itself, as well as all dependencies, to be
582 /// rewritten to the locked versions. This will transform the summary's
583 /// source to a precise source (listed in the locked version) as well as
584 /// transforming all of the dependencies from range requirements on
585 /// imprecise sources to exact requirements on precise sources.
586 ///
587 /// If a summary does not point at a package ID which was previously locked,
588 /// or if any dependencies were added and don't have a previously listed
589 /// version, we still want to avoid updating as many dependencies as
590 /// possible to keep the graph stable. In this case we map all of the
591 /// summary's dependencies to be rewritten to a locked version wherever
592 /// possible. If we're unable to map a dependency though, we just pass it on
593 /// through.
594 pub fn lock(&self, summary: Summary) -> Summary {
595 assert!(self.patches_locked);
596 lock(&self.locked, &self.patches_available, summary)
597 }
598
599 fn warn_bad_override(
600 &self,
601 override_summary: &Summary,
602 real_summary: &Summary,
603 ) -> CargoResult<()> {
604 let mut real_deps = real_summary.dependencies().iter().collect::<Vec<_>>();
605
606 let boilerplate = "\
607This is currently allowed but is known to produce buggy behavior with spurious
608recompiles and changes to the crate graph. Path overrides unfortunately were
609never intended to support this feature, so for now this message is just a
610warning. In the future, however, this message will become a hard error.
611
612To change the dependency graph via an override it's recommended to use the
613`[patch]` feature of Cargo instead of the path override feature. This is
614documented online at the url below for more information.
615
616https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html
617";
618
619 for dep in override_summary.dependencies() {
620 if let Some(i) = real_deps.iter().position(|d| dep == *d) {
621 real_deps.remove(i);
622 continue;
623 }
624 let msg = format!(
625 "path override for crate `{}` has altered the original list of\n\
626 dependencies; the dependency on `{}` was either added or\n\
627 modified to not match the previously resolved version\n\n\
628 {}",
629 override_summary.package_id().name(),
630 dep.package_name(),
631 boilerplate
632 );
633 self.source_config.gctx().shell().warn(&msg)?;
634 return Ok(());
635 }
636
637 if let Some(dep) = real_deps.get(0) {
638 let msg = format!(
639 "path override for crate `{}` has altered the original list of\n\
640 dependencies; the dependency on `{}` was removed\n\n\
641 {}",
642 override_summary.package_id().name(),
643 dep.package_name(),
644 boilerplate
645 );
646 self.source_config.gctx().shell().warn(&msg)?;
647 return Ok(());
648 }
649
650 Ok(())
651 }
652}
653
654impl<'gctx> Registry for PackageRegistry<'gctx> {
655 async fn query(
656 &self,
657 dep: &Dependency,
658 kind: QueryKind,
659 f: &mut dyn FnMut(IndexSummary),
660 ) -> CargoResult<()> {
661 assert!(self.patches_locked);
662 // Look for an override and get ready to query the real source.
663 let override_summary = self.query_overrides(dep).await?;
664
665 // Next up on our list of candidates is to check the `[patch]` section
666 // of the manifest. Here we look through all patches relevant to the
667 // source that `dep` points to, and then we match name/version. Note
668 // that we don't use `dep.matches(..)` because the patches, by definition,
669 // come from a different source. This means that `dep.matches(..)` will
670 // always return false, when what we really care about is the name/version match.
671 let mut patches = Vec::<Summary>::new();
672 if let Some(extra) = self.patches.get(dep.source_id().canonical_url()) {
673 patches.extend(
674 extra
675 .iter()
676 .filter(|s| dep.matches_ignoring_source(s.package_id()))
677 .cloned(),
678 );
679 }
680
681 // A crucial feature of the `[patch]` feature is that we don't query the
682 // actual registry if we have a "locked" dependency. A locked dep basically
683 // just means a version constraint of `=a.b.c`, and because patches take
684 // priority over the actual source then if we have a candidate we're done.
685 if patches.len() == 1 && dep.is_locked() {
686 let patch = patches.remove(0);
687 match override_summary {
688 Some(override_summary) => {
689 self.warn_bad_override(&override_summary, &patch)?;
690 let override_summary = self.lock(override_summary);
691 f(IndexSummary::Candidate(override_summary));
692 }
693 None => f(IndexSummary::Candidate(patch)),
694 }
695
696 return Ok(());
697 }
698
699 if !patches.is_empty() {
700 debug!(
701 "found {} patches with an unlocked dep on `{}` at {} \
702 with `{}`, \
703 looking at sources",
704 patches.len(),
705 dep.package_name(),
706 dep.source_id(),
707 dep.version_req()
708 );
709 }
710
711 // Ensure the requested source_id is loaded
712 self.ensure_loaded(dep.source_id(), Kind::Normal)
713 .with_context(|| {
714 format!(
715 "failed to load source for dependency `{}`",
716 dep.package_name()
717 )
718 })?;
719
720 // Helper function to add context for query errors.
721 async fn query_with_context(
722 source: &dyn Source,
723 dep: &Dependency,
724 kind: QueryKind,
725 f: &mut dyn FnMut(IndexSummary),
726 ) -> CargoResult<()> {
727 source
728 .query(dep, kind, f)
729 .await
730 .with_context(|| format!("unable to update {}", source.source_id()))
731 .with_context(|| {
732 format!(
733 "failed to load source for dependency `{}`",
734 dep.package_name()
735 )
736 })
737 }
738
739 let source = self.sources.borrow().get(dep.source_id()).cloned();
740 match (override_summary, source) {
741 (Some(_), None) => {
742 return Err(anyhow::anyhow!("override found but no real ones"));
743 }
744 (None, None) => return Ok(()),
745
746 // If we don't have an override then we just ship everything upstairs after locking the summary
747 (None, Some(source)) => {
748 for patch in patches.iter() {
749 f(IndexSummary::Candidate(patch.clone()));
750 }
751
752 // Our sources shouldn't ever come back to us with two summaries
753 // that have the same version. We could, however, have an `[patch]`
754 // section which is in use to override a version in the registry.
755 // This means that if our `summary` in this loop has the same
756 // version as something in `patches` that we've already selected,
757 // then we skip this `summary`.
758 let locked = &self.locked;
759 let all_patches = &self.patches_available;
760 let callback = &mut |summary: IndexSummary| {
761 for patch in patches.iter() {
762 let patch = patch.package_id().version();
763 if summary.package_id().version() == patch {
764 return;
765 }
766 }
767 let summary = summary.map_summary(|summary| lock(locked, all_patches, summary));
768 f(summary)
769 };
770 return query_with_context(&*source, dep, kind, callback).await;
771 }
772
773 // If we have an override summary then we query the source to sanity check its results.
774 // We don't actually use any of the summaries it gives us though.
775 (Some(override_summary), Some(source)) => {
776 if !patches.is_empty() {
777 return Err(anyhow::anyhow!("found patches and a path override"));
778 }
779 let mut n = 0;
780 let mut to_warn = None;
781 let callback = &mut |summary| {
782 n += 1;
783 match summary {
784 IndexSummary::Candidate(summary)
785 | IndexSummary::Yanked(summary)
786 | IndexSummary::Offline(summary)
787 | IndexSummary::Unsupported(summary, _)
788 | IndexSummary::Invalid(summary) => {
789 to_warn = Some(summary);
790 }
791 }
792 };
793 query_with_context(&*source, dep, kind, callback).await?;
794 if n > 1 {
795 return Err(anyhow::anyhow!("found an override with a non-locked list"));
796 }
797 if let Some(to_warn) = to_warn {
798 self.warn_bad_override(&override_summary, &to_warn)?;
799 }
800 let override_summary = self.lock(override_summary);
801 f(IndexSummary::Candidate(override_summary));
802 }
803 }
804
805 Ok(())
806 }
807
808 fn describe_source(&self, id: SourceId) -> String {
809 match self.sources.borrow().get(id) {
810 Some(src) => src.describe(),
811 None => id.to_string(),
812 }
813 }
814
815 fn is_replaced(&self, id: SourceId) -> bool {
816 match self.sources.borrow().get(id) {
817 Some(src) => src.is_replaced(),
818 None => false,
819 }
820 }
821}
822
823/// See [`PackageRegistry::lock`].
824fn lock(
825 locked: &LockedMap,
826 patches: &HashMap<CanonicalUrl, Vec<PackageId>>,
827 summary: Summary,
828) -> Summary {
829 let pair = locked
830 .get(&(summary.source_id(), summary.name()))
831 .and_then(|vec| vec.iter().find(|&&(id, _)| id == summary.package_id()));
832
833 trace!("locking summary of {}", summary.package_id());
834
835 // Lock the summary's ID if possible
836 let summary = match pair {
837 Some((precise, _)) => summary.override_id(*precise),
838 None => summary,
839 };
840 summary.map_dependencies(|dep| {
841 trace!(
842 "\t{}/{}/{}",
843 dep.package_name(),
844 dep.version_req(),
845 dep.source_id()
846 );
847
848 // If we've got a known set of overrides for this summary, then
849 // one of a few cases can arise:
850 //
851 // 1. We have a lock entry for this dependency from the same
852 // source as it's listed as coming from. In this case we make
853 // sure to lock to precisely the given package ID.
854 //
855 // 2. We have a lock entry for this dependency, but it's from a
856 // different source than what's listed, or the version
857 // requirement has changed. In this case we must discard the
858 // locked version because the dependency needs to be
859 // re-resolved.
860 //
861 // 3. We have a lock entry for this dependency, but it's from a
862 // different source than what's listed. This lock though happens
863 // through `[patch]`, so we want to preserve it.
864 //
865 // 4. We don't have a lock entry for this dependency, in which
866 // case it was likely an optional dependency which wasn't
867 // included previously so we just pass it through anyway.
868 //
869 // Cases 1/2 are handled by `matches_id`, case 3 is handled specially,
870 // and case 4 is handled by falling through to the logic below.
871 if let Some((_, locked_deps)) = pair {
872 let locked = locked_deps.iter().find(|&&id| {
873 // If the dependency matches the package id exactly then we've
874 // found a match, this is the id the dependency was previously
875 // locked to.
876 if dep.matches_id(id) {
877 return true;
878 }
879
880 // If the name/version doesn't match, then we definitely don't
881 // have a match whatsoever. Otherwise we need to check
882 // `[patch]`...
883 if !dep.matches_ignoring_source(id) {
884 return false;
885 }
886
887 // ... so here we look up the dependency url in the patches
888 // map, and we see if `id` is contained in the list of patches
889 // for that url. If it is then this lock is still valid,
890 // otherwise the lock is no longer valid.
891 match patches.get(dep.source_id().canonical_url()) {
892 Some(list) => list.contains(&id),
893 None => false,
894 }
895 });
896
897 if let Some(&locked) = locked {
898 trace!("\tfirst hit on {}", locked);
899 let mut dep = dep;
900
901 // If we found a locked version where the sources match, then
902 // we can `lock_to` to get an exact lock on this dependency.
903 // Otherwise we got a lock via `[patch]` so we only lock the
904 // version requirement, not the source.
905 if locked.source_id() == dep.source_id() {
906 dep.lock_to(locked);
907 } else {
908 dep.lock_version(locked.version());
909 }
910 return dep;
911 }
912 }
913
914 // If this dependency did not have a locked version, then we query
915 // all known locked packages to see if they match this dependency.
916 // If anything does then we lock it to that and move on.
917 let v = locked
918 .get(&(dep.source_id(), dep.package_name()))
919 .and_then(|vec| vec.iter().find(|&&(id, _)| dep.matches_id(id)));
920 if let Some(&(id, _)) = v {
921 trace!("\tsecond hit on {}", id);
922 let mut dep = dep;
923 dep.lock_to(id);
924 return dep;
925 }
926
927 trace!("\tnope, unlocked");
928 dep
929 })
930}
931
932/// A helper for selecting the summary, or generating a helpful error message.
933///
934/// Returns a tuple that the first element is the summary selected. The second
935/// is a package ID indicating that the patch entry should be unlocked. This
936/// happens when a match cannot be found with the `locked` one, but found one
937/// via the original patch, so we need to inform the resolver to "unlock" it.
938async fn summary_for_patch(
939 original_patch: &Patch,
940 orig_patch_url: &Url,
941 locked: &Option<LockedPatchDependency>,
942 mut summaries: Vec<Summary>,
943 source: &dyn Source,
944) -> CargoResult<(Summary, Option<PackageId>)> {
945 if summaries.len() == 1 {
946 return Ok((summaries.pop().unwrap(), None));
947 }
948 if summaries.len() > 1 {
949 // TODO: In the future, it might be nice to add all of these
950 // candidates so that version selection would just pick the
951 // appropriate one. However, as this is currently structured, if we
952 // added these all as patches, the unselected versions would end up in
953 // the "unused patch" listing, and trigger a warning. It might take a
954 // fair bit of restructuring to make that work cleanly, and there
955 // isn't any demand at this time to support that.
956 let mut vers: Vec<_> = summaries.iter().map(|summary| summary.version()).collect();
957 vers.sort();
958 let versions: Vec<_> = vers.into_iter().map(|v| v.to_string()).collect();
959 return Err(anyhow::anyhow!(
960 "patch for `{}` in `{}` resolved to more than one candidate\n\
961 note: found versions: {}\n\
962 help: check `{}` patch definition for `{}` in `{}`\n\
963 help: select only one package using `version = \"={}\"`",
964 &original_patch.dep.package_name(),
965 &original_patch.dep.source_id(),
966 versions.join(", "),
967 &original_patch.dep.package_name(),
968 orig_patch_url,
969 original_patch.loc,
970 versions.last().unwrap()
971 ));
972 }
973 assert!(summaries.is_empty());
974 // No summaries found, try to help the user figure out what is wrong.
975 if let Some(locked) = locked {
976 // Since the locked patch did not match anything, try the unlocked one.
977 let orig_matches = source
978 .query_vec(&original_patch.dep, QueryKind::Exact)
979 .await
980 .unwrap_or_else(|e| {
981 tracing::warn!(
982 "could not determine unlocked summaries for dep {:?}: {:?}",
983 &original_patch.dep,
984 e
985 );
986 Vec::new()
987 });
988
989 let orig_matches = orig_matches
990 .into_iter()
991 .filter_map(|s| match s {
992 IndexSummary::Candidate(s) => Some(s),
993 _ => None,
994 })
995 .collect();
996
997 let summary = Box::pin(summary_for_patch(
998 original_patch,
999 orig_patch_url,
1000 &None,
1001 orig_matches,
1002 source,
1003 ))
1004 .await?;
1005 return Ok((summary.0, Some(locked.package_id)));
1006 }
1007 // Try checking if there are *any* packages that match this by name.
1008 let name_only_dep = Dependency::new_override(
1009 original_patch.dep.package_name(),
1010 original_patch.dep.source_id(),
1011 );
1012
1013 let name_summaries = source
1014 .query_vec(&name_only_dep, QueryKind::Exact)
1015 .await
1016 .unwrap_or_else(|e| {
1017 tracing::warn!(
1018 "failed to do name-only summary query for {:?}: {:?}",
1019 name_only_dep,
1020 e
1021 );
1022 Vec::new()
1023 });
1024 let mut vers = name_summaries
1025 .iter()
1026 .filter_map(|s| match s {
1027 IndexSummary::Candidate(s) => Some(s.version()),
1028 _ => None,
1029 })
1030 .collect::<Vec<_>>();
1031 let found = match vers.len() {
1032 0 => "".to_string(),
1033 1 => format!("version `{}`", vers[0]),
1034 _ => {
1035 vers.sort();
1036 let strs: Vec<_> = vers.into_iter().map(|v| v.to_string()).collect();
1037 format!("versions `{}`", strs.join(", "))
1038 }
1039 };
1040 Err(if found.is_empty() {
1041 anyhow::anyhow!(
1042 "patch location `{}` does not contain packages matching `{}`\n\
1043 help: check `{}` patch definition for `{}` in `{}`",
1044 &original_patch.dep.source_id(),
1045 &original_patch.dep.package_name(),
1046 &original_patch.dep.package_name(),
1047 orig_patch_url,
1048 original_patch.loc
1049 )
1050 } else {
1051 anyhow::anyhow!(
1052 "patch `{}` version mismatch\n\
1053 note: patch location contains {}, but patch definition requires `{}`\n\
1054 help: check patch location `{}`\n\
1055 help: check `{}` patch definition for `{}` in `{}`",
1056 &original_patch.dep.package_name(),
1057 found,
1058 &original_patch.dep.version_req(),
1059 &original_patch.dep.source_id(),
1060 &original_patch.dep.package_name(),
1061 orig_patch_url,
1062 original_patch.loc
1063 )
1064 })
1065}