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