1 # Overriding Dependencies
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:
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
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
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.
19 These scenarios can be solved with the [`[patch]` manifest
20 section](#the-patch-section).
22 This chapter walks through a few different use cases, and includes details
23 on the different ways to override a dependency.
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)
32 * [The `[patch]` section](#the-patch-section)
33 * [The `[replace]` section](#the-replace-section)
34 * [`paths` overrides](#paths-overrides)
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
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:
46 [`uuid` crate]: https://crates.io/crates/uuid
57 First thing we'll do is to clone the [`uuid` repository][uuid-repository]
61 $ git clone https://github.com/uuid-rs/uuid.git
64 Next we'll edit the manifest of `my-library` to contain:
68 uuid = { path = "../path/to/uuid" }
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.
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.
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!
89 In any case, typically all you need to do now is:
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
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`.
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:
111 uuid = { git = 'https://github.com/uuid-rs/uuid.git' }
114 [uuid-repository]: https://github.com/uuid-rs/uuid
116 ## Working with an unpublished minor version
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.
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
138 uuid = { git = 'https://github.com/uuid-rs/uuid.git' }
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
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
151 It's also worth noting that `[patch]` applies *transitively*. Let's say you use
152 `my-library` in a larger package, such as:
160 my-library = { git = 'https://example.com/git/my-library' }
164 uuid = { git = 'https://github.com/uuid-rs/uuid.git' }
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
174 ### Overriding repository URL
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:
181 [patch."https://github.com/your/repository"]
182 my-library = { path = "../my-library/path" }
187 ## Prepublishing a breaking change
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:
200 uuid = { git = "https://github.com/uuid-rs/uuid.git", branch = "2.0.0" }
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:
214 my-library = { git = 'https://example.com/git/my-library' }
218 uuid = { git = 'https://github.com/uuid-rs/uuid.git', branch = '2.0.0' }
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.
227 ## Using `[patch]` with multiple versions
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:
237 serde = { git = 'https://github.com/serde-rs/serde.git' }
238 serde2 = { git = 'https://github.com/example/serde.git', package = 'serde', branch = 'v2' }
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`.
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
251 ## The `[patch]` section
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:
259 foo = { git = 'https://github.com/example/foo.git' }
260 bar = { path = 'my/local/bar' }
263 git = 'https://github.com/example/baz.git'
265 [patch.'https://github.com/example/baz']
266 baz = { git = 'https://github.com/example/patched-baz.git', branch = 'my-branch' }
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
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.
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.
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.
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
298 ## The `[replace]` section
300 > **Note**: `[replace]` is deprecated. You should use the
301 > [`[patch]`](#the-patch-section) table instead.
303 This section of Cargo.toml can be used to override dependencies with other
304 copies. The syntax is similar to the `[dependencies]` section:
308 "foo:0.1.0" = { git = 'https://github.com/example/foo.git' }
309 "bar:1.0.2" = { path = 'my/local/bar' }
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).
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
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
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`:
335 paths = ["/path/to/uuid"]
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.
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
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.
357 [crates.io]: https://crates.io/
358 [multiple locations]: specifying-dependencies.md#multiple-locations
359 [dependencies]: specifying-dependencies.md