Skip to content

Commit 15d0909

Browse files
committed
Auto merge of #11870 - epage:registry, r=Eh2406
docs(contrib): Move higher level resolver docs into doc comments This is a follow up to #11809. I chose `ops::resolve` for most of the old documentation as this because the docs cover the higher level details that include it, like `Cargo.lock` file, while `core::resolver` is more of the algorithm.
2 parents ed42a0d + bd5e1a5 commit 15d0909

File tree

5 files changed

+65
-97
lines changed

5 files changed

+65
-97
lines changed

src/cargo/core/resolver/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@
3737
//! everything to bail out immediately and return success, and only if *nothing*
3838
//! works do we actually return an error up the stack.
3939
//!
40+
//! Resolution is currently performed twice
41+
//! 1. With all features enabled (this is what gets saved to `Cargo.lock`)
42+
//! 2. With only the specific features the user selected on the command-line. Ideally this
43+
//! run will get removed in the future when transitioning to the new feature resolver.
44+
//!
45+
//! A new feature-specific resolver was added in 2020 which adds more sophisticated feature
46+
//! resolution. It is located in the [`features`] module. The original dependency resolver still
47+
//! performs feature unification, as it can help reduce the dependencies it has to consider during
48+
//! resolution (rather than assuming every optional dependency of every package is enabled).
49+
//! Checking if a feature is enabled must go through the new feature resolver.
50+
//!
4051
//! ## Performance
4152
//!
4253
//! Note that this is a relatively performance-critical portion of Cargo. The

src/cargo/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
//! This is the entry point for all the compilation commands. This is a
3232
//! good place to start if you want to follow how compilation starts and
3333
//! flows to completion.
34-
//! - [`core::resolver`]:
35-
//! This is the dependency and feature resolvers.
34+
//! - [`ops::resolve`]:
35+
//! Top-level API for dependency and feature resolver (e.g. [`ops::resolve_ws`])
36+
//! - [`core::resolver`]: The core algorithm
3637
//! - [`core::compiler`]:
3738
//! This is the code responsible for running `rustc` and `rustdoc`.
3839
//! - [`core::compiler::build_context`]:

src/cargo/ops/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mod common_for_install_and_uninstall;
5353
mod fix;
5454
pub(crate) mod lockfile;
5555
pub(crate) mod registry;
56-
mod resolve;
56+
pub(crate) mod resolve;
5757
pub mod tree;
5858
mod vendor;
5959

src/cargo/ops/resolve.rs

+49-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
11
//! High-level APIs for executing the resolver.
22
//!
3-
//! This module provides functions for running the resolver given a workspace.
3+
//! This module provides functions for running the resolver given a workspace, including loading
4+
//! the `Cargo.lock` file and checkinf if it needs updating.
5+
//!
46
//! There are roughly 3 main functions:
57
//!
6-
//! - `resolve_ws`: A simple, high-level function with no options.
7-
//! - `resolve_ws_with_opts`: A medium-level function with options like
8+
//! - [`resolve_ws`]: A simple, high-level function with no options.
9+
//! - [`resolve_ws_with_opts`]: A medium-level function with options like
810
//! user-provided features. This is the most appropriate function to use in
911
//! most cases.
10-
//! - `resolve_with_previous`: A low-level function for running the resolver,
12+
//! - [`resolve_with_previous`]: A low-level function for running the resolver,
1113
//! providing the most power and flexibility.
14+
//!
15+
//! ### Data Structures
16+
//!
17+
//! - [`Workspace`]:
18+
//! Usually created by [`crate::util::command_prelude::ArgMatchesExt::workspace`] which discovers the root of the
19+
//! workspace, and loads all the workspace members as a [`Package`] object
20+
//! - [`Package`]
21+
//! Corresponds with `Cargo.toml` manifest (deserialized as [`Manifest`]) and its associated files.
22+
//! - [`Target`]s are crates such as the library, binaries, integration test, or examples.
23+
//! They are what is actually compiled by `rustc`.
24+
//! Each `Target` defines a crate root, like `src/lib.rs` or `examples/foo.rs`.
25+
//! - [`PackageId`] --- A unique identifier for a package.
26+
//! - [`PackageRegistry`]:
27+
//! The primary interface for how the dependency
28+
//! resolver finds packages. It contains the `SourceMap`, and handles things
29+
//! like the `[patch]` table. The dependency resolver
30+
//! sends a query to the `PackageRegistry` to "get me all packages that match
31+
//! this dependency declaration". The `Registry` trait provides a generic interface
32+
//! to the `PackageRegistry`, but this is only used for providing an alternate
33+
//! implementation of the `PackageRegistry` for testing.
34+
//! - [`SourceMap`]: Map of all available sources.
35+
//! - [`Source`]: An abstraction for something that can fetch packages (a remote
36+
//! registry, a git repo, the local filesystem, etc.). Check out the [source
37+
//! implementations] for all the details about registries, indexes, git
38+
//! dependencies, etc.
39+
//! * [`SourceId`]: A unique identifier for a source.
40+
//! - [`Summary`]: A of a [`Manifest`], and is essentially
41+
//! the information that can be found in a registry index. Queries against the
42+
//! `PackageRegistry` yields a `Summary`. The resolver uses the summary
43+
//! information to build the dependency graph.
44+
//! - [`PackageSet`] --- Contains all of the `Package` objects. This works with the
45+
//! [`Downloads`] struct to coordinate downloading packages. It has a reference
46+
//! to the `SourceMap` to get the `Source` objects which tell the `Downloads`
47+
//! struct which URLs to fetch.
48+
//!
49+
//! [`Package`]: crate::core::package
50+
//! [`Target`]: crate::core::Target
51+
//! [`Manifest`]: crate::core::Manifest
52+
//! [`Source`]: crate::core::Source
53+
//! [`SourceMap`]: crate::core::SourceMap
54+
//! [`PackageRegistry`]: crate::core::registry::PackageRegistry
55+
//! [source implementations]: crate::sources
56+
//! [`Downloads`]: crate::core::package::Downloads
1257
1358
use crate::core::compiler::{CompileKind, RustcTargetData};
1459
use crate::core::registry::{LockedPatchDependency, PackageRegistry};
+1-90
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,3 @@
11
# Packages and Resolution
22

3-
## Workspaces
4-
5-
The [`Workspace`] object is usually created very early by calling the
6-
[`workspace`][ws-method] helper method. This discovers the root of the
7-
workspace, and loads all the workspace members as a [`Package`] object. Each
8-
package corresponds to a single `Cargo.toml` (which is deserialized into a
9-
[`Manifest`]), and may define several [`Target`]s, such as the library,
10-
binaries, integration test or examples. Targets are crates (each target
11-
defines a crate root, like `src/lib.rs` or `examples/foo.rs`) and are what is
12-
actually compiled by `rustc`.
13-
14-
## Packages and Sources
15-
16-
There are several data structures that are important to understand how
17-
packages are found and loaded:
18-
19-
* [`Package`] --- A package, which is a `Cargo.toml` manifest and its associated
20-
source files.
21-
* [`PackageId`] --- A unique identifier for a package.
22-
* [`Source`] --- An abstraction for something that can fetch packages (a remote
23-
registry, a git repo, the local filesystem, etc.). Check out the [source
24-
implementations] for all the details about registries, indexes, git
25-
dependencies, etc.
26-
* [`SourceId`] --- A unique identifier for a source.
27-
* [`SourceMap`] --- Map of all available sources.
28-
* [`PackageRegistry`] --- This is the main interface for how the dependency
29-
resolver finds packages. It contains the `SourceMap`, and handles things
30-
like the `[patch]` table. The `Registry` trait provides a generic interface
31-
to the `PackageRegistry`, but this is only used for providing an alternate
32-
implementation of the `PackageRegistry` for testing. The dependency resolver
33-
sends a query to the `PackageRegistry` to "get me all packages that match
34-
this dependency declaration".
35-
* [`Summary`] --- A summary is a subset of a [`Manifest`], and is essentially
36-
the information that can be found in a registry index. Queries against the
37-
`PackageRegistry` yields a `Summary`. The resolver uses the summary
38-
information to build the dependency graph.
39-
* [`PackageSet`] --- Contains all of the `Package` objects. This works with the
40-
[`Downloads`] struct to coordinate downloading packages. It has a reference
41-
to the `SourceMap` to get the `Source` objects which tell the `Downloads`
42-
struct which URLs to fetch.
43-
44-
All of these come together in the [`ops::resolve`] module. This module
45-
contains the primary functions for performing resolution (described below). It
46-
also handles downloading of packages. It is essentially where all of the data
47-
structures above come together.
48-
49-
## Resolver
50-
51-
[`Resolve`] is the representation of a directed graph of package dependencies,
52-
which uses [`PackageId`]s for nodes. This is the data structure that is saved
53-
to the `Cargo.lock` file. If there is no lock file, Cargo constructs a resolve
54-
by finding a graph of packages which matches declared dependency specification
55-
according to SemVer.
56-
57-
[`ops::resolve`] is the front-end for creating a `Resolve`. It handles loading
58-
the `Cargo.lock` file, checking if it needs updating, etc.
59-
60-
Resolution is currently performed twice. It is performed once with all
61-
features enabled. This is the resolve that gets saved to `Cargo.lock`. It then
62-
runs again with only the specific features the user selected on the
63-
command-line. Ideally this second run will get removed in the future when
64-
transitioning to the new feature resolver.
65-
66-
### Feature resolver
67-
68-
A new feature-specific resolver was added in 2020 which adds more
69-
sophisticated feature resolution. It is located in the [`resolver::features`]
70-
module. The original dependency resolver still performs feature unification,
71-
as it can help reduce the dependencies it has to consider during resolution
72-
(rather than assuming every optional dependency of every package is enabled).
73-
Checking if a feature is enabled must go through the new feature resolver.
74-
75-
76-
[`Workspace`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/core/workspace.rs
77-
[ws-method]: https://github.com/rust-lang/cargo/blob/e4b65bdc80f2a293447f2f6a808fa7c84bf9a357/src/cargo/util/command_prelude.rs#L298-L318
78-
[`Package`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/core/package.rs
79-
[`Target`]: https://github.com/rust-lang/cargo/blob/e4b65bdc80f2a293447f2f6a808fa7c84bf9a357/src/cargo/core/manifest.rs#L181-L206
80-
[`Manifest`]: https://github.com/rust-lang/cargo/blob/e4b65bdc80f2a293447f2f6a808fa7c84bf9a357/src/cargo/core/manifest.rs#L27-L51
81-
[`Source`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/core/source/mod.rs
82-
[`SourceId`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/core/source/source_id.rs
83-
[`SourceMap`]: https://github.com/rust-lang/cargo/blob/e4b65bdc80f2a293447f2f6a808fa7c84bf9a357/src/cargo/core/source/mod.rs#L245-L249
84-
[`PackageRegistry`]: https://github.com/rust-lang/cargo/blob/e4b65bdc80f2a293447f2f6a808fa7c84bf9a357/src/cargo/core/registry.rs#L36-L81
85-
[`ops::resolve`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/resolve.rs
86-
[`resolver::features`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/core/resolver/features.rs#L259
87-
[source implementations]: https://github.com/rust-lang/cargo/tree/master/src/cargo/sources
88-
[`PackageId`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/core/package_id.rs
89-
[`Summary`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/core/summary.rs
90-
[`PackageSet`]: https://github.com/rust-lang/cargo/blob/e4b65bdc80f2a293447f2f6a808fa7c84bf9a357/src/cargo/core/package.rs#L283-L296
91-
[`Downloads`]: https://github.com/rust-lang/cargo/blob/e4b65bdc80f2a293447f2f6a808fa7c84bf9a357/src/cargo/core/package.rs#L298-L352
92-
[`Resolve`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/core/resolver/resolve.rs
3+
See [nightly docs](https://doc.rust-lang.org/nightly/nightly-rustc/cargo/index.html)

0 commit comments

Comments
 (0)