]> git.proxmox.com Git - rustc.git/blob - src/tools/cargo/src/doc/src/reference/overriding-dependencies.md
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / cargo / src / doc / src / reference / overriding-dependencies.md
1 # Overriding Dependencies
2
3 The desire to override a dependency can arise through a number of scenarios.
4 Most of them, however, boil down to the ability to work with a crate before
5 it's been published to [crates.io]. For example:
6
7 * A crate you're working on is also used in a much larger application you're
8 working on, and you'd like to test a bug fix to the library inside of the
9 larger application.
10 * An upstream crate you don't work on has a new feature or a bug fix on the
11 master branch of its git repository which you'd like to test out.
12 * You're about to publish a new major version of your crate, but you'd like to
13 do integration testing across an entire package to ensure the new major
14 version works.
15 * You've submitted a fix to an upstream crate for a bug you found, but you'd
16 like to immediately have your application start depending on the fixed
17 version of the crate to avoid blocking on the bug fix getting merged.
18
19 These scenarios can be solved with the [`[patch]` manifest
20 section](#the-patch-section).
21
22 This chapter walks through a few different use cases, and includes details
23 on the different ways to override a dependency.
24
25 * Example use cases
26 * [Testing a bugfix](#testing-a-bugfix)
27 * [Working with an unpublished minor version](#working-with-an-unpublished-minor-version)
28 * [Overriding repository URL](#overriding-repository-url)
29 * [Prepublishing a breaking change](#prepublishing-a-breaking-change)
30 * [Using `[patch]` with multiple versions](#using-patch-with-multiple-versions)
31 * Reference
32 * [The `[patch]` section](#the-patch-section)
33 * [The `[replace]` section](#the-replace-section)
34 * [`paths` overrides](#paths-overrides)
35
36 > **Note**: See also specifying a dependency with [multiple locations], which
37 > can be used to override the source for a single dependency declaration in a
38 > local package.
39
40 ## Testing a bugfix
41
42 Let's say you're working with the [`uuid` crate] but while you're working on it
43 you discover a bug. You are, however, quite enterprising so you decide to also
44 try to fix the bug! Originally your manifest will look like:
45
46 [`uuid` crate]: https://crates.io/crates/uuid
47
48 ```toml
49 [package]
50 name = "my-library"
51 version = "0.1.0"
52
53 [dependencies]
54 uuid = "1.0"
55 ```
56
57 First thing we'll do is to clone the [`uuid` repository][uuid-repository]
58 locally via:
59
60 ```console
61 $ git clone https://github.com/uuid-rs/uuid.git
62 ```
63
64 Next we'll edit the manifest of `my-library` to contain:
65
66 ```toml
67 [patch.crates-io]
68 uuid = { path = "../path/to/uuid" }
69 ```
70
71 Here we declare that we're *patching* the source `crates-io` with a new
72 dependency. This will effectively add the local checked out version of `uuid` to
73 the crates.io registry for our local package.
74
75 Next up we need to ensure that our lock file is updated to use this new version
76 of `uuid` so our package uses the locally checked out copy instead of one from
77 crates.io. The way `[patch]` works is that it'll load the dependency at
78 `../path/to/uuid` and then whenever crates.io is queried for versions of `uuid`
79 it'll *also* return the local version.
80
81 This means that the version number of the local checkout is significant and will
82 affect whether the patch is used. Our manifest declared `uuid = "1.0"` which
83 means we'll only resolve to `>= 1.0.0, < 2.0.0`, and Cargo's greedy resolution
84 algorithm also means that we'll resolve to the maximum version within that
85 range. Typically this doesn't matter as the version of the git repository will
86 already be greater or match the maximum version published on crates.io, but it's
87 important to keep this in mind!
88
89 In any case, typically all you need to do now is:
90
91 ```console
92 $ cargo build
93 Compiling uuid v1.0.0 (.../uuid)
94 Compiling my-library v0.1.0 (.../my-library)
95 Finished dev [unoptimized + debuginfo] target(s) in 0.32 secs
96 ```
97
98 And that's it! You're now building with the local version of `uuid` (note the
99 path in parentheses in the build output). If you don't see the local path version getting
100 built then you may need to run `cargo update uuid --precise $version` where
101 `$version` is the version of the locally checked out copy of `uuid`.
102
103 Once you've fixed the bug you originally found the next thing you'll want to do
104 is to likely submit that as a pull request to the `uuid` crate itself. Once
105 you've done this then you can also update the `[patch]` section. The listing
106 inside of `[patch]` is just like the `[dependencies]` section, so once your pull
107 request is merged you could change your `path` dependency to:
108
109 ```toml
110 [patch.crates-io]
111 uuid = { git = 'https://github.com/uuid-rs/uuid.git' }
112 ```
113
114 [uuid-repository]: https://github.com/uuid-rs/uuid
115
116 ## Working with an unpublished minor version
117
118 Let's now shift gears a bit from bug fixes to adding features. While working on
119 `my-library` you discover that a whole new feature is needed in the `uuid`
120 crate. You've implemented this feature, tested it locally above with `[patch]`,
121 and submitted a pull request. Let's go over how you continue to use and test it
122 before it's actually published.
123
124 Let's also say that the current version of `uuid` on crates.io is `1.0.0`, but
125 since then the master branch of the git repository has updated to `1.0.1`. This
126 branch includes your new feature you submitted previously. To use this
127 repository we'll edit our `Cargo.toml` to look like
128
129 ```toml
130 [package]
131 name = "my-library"
132 version = "0.1.0"
133
134 [dependencies]
135 uuid = "1.0.1"
136
137 [patch.crates-io]
138 uuid = { git = 'https://github.com/uuid-rs/uuid.git' }
139 ```
140
141 Note that our local dependency on `uuid` has been updated to `1.0.1` as it's
142 what we'll actually require once the crate is published. This version doesn't
143 exist on crates.io, though, so we provide it with the `[patch]` section of the
144 manifest.
145
146 Now when our library is built it'll fetch `uuid` from the git repository and
147 resolve to 1.0.1 inside the repository instead of trying to download a version
148 from crates.io. Once 1.0.1 is published on crates.io the `[patch]` section can
149 be deleted.
150
151 It's also worth noting that `[patch]` applies *transitively*. Let's say you use
152 `my-library` in a larger package, such as:
153
154 ```toml
155 [package]
156 name = "my-binary"
157 version = "0.1.0"
158
159 [dependencies]
160 my-library = { git = 'https://example.com/git/my-library' }
161 uuid = "1.0"
162
163 [patch.crates-io]
164 uuid = { git = 'https://github.com/uuid-rs/uuid.git' }
165 ```
166
167 Remember that `[patch]` is applicable *transitively* but can only be defined at
168 the *top level* so we consumers of `my-library` have to repeat the `[patch]` section
169 if necessary. Here, though, the new `uuid` crate applies to *both* our dependency on
170 `uuid` and the `my-library -> uuid` dependency. The `uuid` crate will be resolved to
171 one version for this entire crate graph, 1.0.1, and it'll be pulled from the git
172 repository.
173
174 ### Overriding repository URL
175
176 In case the dependency you want to override isn't loaded from `crates.io`,
177 you'll have to change a bit how you use `[patch]`. For example, if the
178 dependency is a git dependency, you can override it to a local path with:
179
180 ```toml
181 [patch."https://github.com/your/repository"]
182 my-library = { path = "../my-library/path" }
183 ```
184
185 And that's it!
186
187 ## Prepublishing a breaking change
188
189 Let's take a look at working with a new major version of a crate, typically
190 accompanied with breaking changes. Sticking with our previous crates, this
191 means that we're going to be creating version 2.0.0 of the `uuid` crate. After
192 we've submitted all changes upstream we can update our manifest for
193 `my-library` to look like:
194
195 ```toml
196 [dependencies]
197 uuid = "2.0"
198
199 [patch.crates-io]
200 uuid = { git = "https://github.com/uuid-rs/uuid.git", branch = "2.0.0" }
201 ```
202
203 And that's it! Like with the previous example the 2.0.0 version doesn't actually
204 exist on crates.io but we can still put it in through a git dependency through
205 the usage of the `[patch]` section. As a thought exercise let's take another
206 look at the `my-binary` manifest from above again as well:
207
208 ```toml
209 [package]
210 name = "my-binary"
211 version = "0.1.0"
212
213 [dependencies]
214 my-library = { git = 'https://example.com/git/my-library' }
215 uuid = "1.0"
216
217 [patch.crates-io]
218 uuid = { git = 'https://github.com/uuid-rs/uuid.git', branch = '2.0.0' }
219 ```
220
221 Note that this will actually resolve to two versions of the `uuid` crate. The
222 `my-binary` crate will continue to use the 1.x.y series of the `uuid` crate but
223 the `my-library` crate will use the `2.0.0` version of `uuid`. This will allow you
224 to gradually roll out breaking changes to a crate through a dependency graph
225 without being forced to update everything all at once.
226
227 ## Using `[patch]` with multiple versions
228
229 You can patch in multiple versions of the same crate with the `package` key
230 used to rename dependencies. For example let's say that the `serde` crate has
231 a bugfix that we'd like to use to its `1.*` series but we'd also like to
232 prototype using a `2.0.0` version of serde we have in our git repository. To
233 configure this we'd do:
234
235 ```toml
236 [patch.crates-io]
237 serde = { git = 'https://github.com/serde-rs/serde.git' }
238 serde2 = { git = 'https://github.com/example/serde.git', package = 'serde', branch = 'v2' }
239 ```
240
241 The first `serde = ...` directive indicates that serde `1.*` should be used
242 from the git repository (pulling in the bugfix we need) and the second `serde2
243 = ...` directive indicates that the `serde` package should also be pulled from
244 the `v2` branch of `https://github.com/example/serde`. We're assuming here
245 that `Cargo.toml` on that branch mentions version `2.0.0`.
246
247 Note that when using the `package` key the `serde2` identifier here is actually
248 ignored. We simply need a unique name which doesn't conflict with other patched
249 crates.
250
251 ## The `[patch]` section
252
253 The `[patch]` section of `Cargo.toml` can be used to override dependencies
254 with other copies. The syntax is similar to the
255 [`[dependencies]`][dependencies] section:
256
257 ```toml
258 [patch.crates-io]
259 foo = { git = 'https://github.com/example/foo.git' }
260 bar = { path = 'my/local/bar' }
261
262 [dependencies.baz]
263 git = 'https://github.com/example/baz.git'
264
265 [patch.'https://github.com/example/baz']
266 baz = { git = 'https://github.com/example/patched-baz.git', branch = 'my-branch' }
267 ```
268
269 > **Note**: The `[patch]` table can also be specified as a [configuration
270 > option](config.md), such as in a `.cargo/config.toml` file or a CLI option
271 > like `--config 'patch.crates-io.rand.path="rand"'`. This can be useful for
272 > local-only changes that you don't want to commit, or temporarily testing a
273 > patch.
274
275 The `[patch]` table is made of dependency-like sub-tables. Each key after
276 `[patch]` is a URL of the source that is being patched, or the name of a
277 registry. The name `crates-io` may be used to override the default registry
278 [crates.io]. The first `[patch]` in the example above demonstrates overriding
279 [crates.io], and the second `[patch]` demonstrates overriding a git source.
280
281 Each entry in these tables is a normal dependency specification, the same as
282 found in the `[dependencies]` section of the manifest. The dependencies listed
283 in the `[patch]` section are resolved and used to patch the source at the
284 URL specified. The above manifest snippet patches the `crates-io` source (e.g.
285 crates.io itself) with the `foo` crate and `bar` crate. It also
286 patches the `https://github.com/example/baz` source with a `my-branch` that
287 comes from elsewhere.
288
289 Sources can be patched with versions of crates that do not exist, and they can
290 also be patched with versions of crates that already exist. If a source is
291 patched with a crate version that already exists in the source, then the
292 source's original crate is replaced.
293
294 Cargo only looks at the patch settings in the `Cargo.toml` manifest at the
295 root of the workspace. Patch settings defined in dependencies will be
296 ignored.
297
298 ## The `[replace]` section
299
300 > **Note**: `[replace]` is deprecated. You should use the
301 > [`[patch]`](#the-patch-section) table instead.
302
303 This section of Cargo.toml can be used to override dependencies with other
304 copies. The syntax is similar to the `[dependencies]` section:
305
306 ```toml
307 [replace]
308 "foo:0.1.0" = { git = 'https://github.com/example/foo.git' }
309 "bar:1.0.2" = { path = 'my/local/bar' }
310 ```
311
312 Each key in the `[replace]` table is a [package ID
313 specification](pkgid-spec.md), which allows arbitrarily choosing a node in the
314 dependency graph to override (the 3-part version number is required). The
315 value of each key is the same as the `[dependencies]` syntax for specifying
316 dependencies, except that you can't specify features. Note that when a crate
317 is overridden the copy it's overridden with must have both the same name and
318 version, but it can come from a different source (e.g., git or a local path).
319
320 Cargo only looks at the replace settings in the `Cargo.toml` manifest at the
321 root of the workspace. Replace settings defined in dependencies will be
322 ignored.
323
324 ## `paths` overrides
325
326 Sometimes you're only temporarily working on a crate and you don't want to have
327 to modify `Cargo.toml` like with the `[patch]` section above. For this use
328 case Cargo offers a much more limited version of overrides called **path
329 overrides**.
330
331 Path overrides are specified through [`.cargo/config.toml`](config.md) instead of
332 `Cargo.toml`. Inside of `.cargo/config.toml` you'll specify a key called `paths`:
333
334 ```toml
335 paths = ["/path/to/uuid"]
336 ```
337
338 This array should be filled with directories that contain a `Cargo.toml`. In
339 this instance, we’re just adding `uuid`, so it will be the only one that’s
340 overridden. This path can be either absolute or relative to the directory that
341 contains the `.cargo` folder.
342
343 Path overrides are more restricted than the `[patch]` section, however, in
344 that they cannot change the structure of the dependency graph. When a
345 path replacement is used then the previous set of dependencies
346 must all match exactly to the new `Cargo.toml` specification. For example this
347 means that path overrides cannot be used to test out adding a dependency to a
348 crate, instead `[patch]` must be used in that situation. As a result usage of a
349 path override is typically isolated to quick bug fixes rather than larger
350 changes.
351
352 > **Note**: using a local configuration to override paths will only work for
353 > crates that have been published to [crates.io]. You cannot use this feature
354 > to tell Cargo how to find local unpublished crates.
355
356
357 [crates.io]: https://crates.io/
358 [multiple locations]: specifying-dependencies.md#multiple-locations
359 [dependencies]: specifying-dependencies.md