|
1 | 1 | # Packages and Resolution
|
2 | 2 |
|
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