cargo-patch
Cargo-Patch
is a Cargo Subcommand which allows
patching dependencies using patch files.
Installation
Simply run:
This is not necessary when patching via build.rs
file
Usage
To patch a dependency one has to add the following
to Cargo.toml
:
[]
= "1.0"
= [
"test.patch"
]
It specifies which dependency to patch (in this case serde) and one or more patchfiles to apply. Running:
will download the serde package specified in the
dependency section to the target/patch
folder
and apply the given patches. To use the patched
version one has to override the dependency using
replace
like this
[]
= { = './target/patch/serde-1.0.110' }
Instead of running cargo patch
its also possible to add a build.rs
file like this:
To make it work, add the cargo-patch library to the build-dependencies
[build-dependencies]
cargo-patch = "0.3"
Note, however, that all your patches should be in a single folder called patches
or something similar. This is to make sure that the build script is executed again when something changes.
Patch format
You can either use diff or git to create patch files. Important is that file paths are relative and inside the dependency.
Using diff file generated by GitHub pull request
[]
= "1.0"
= [
{ = "generatedByGithub.patch", = "GithubPrDiff" },
{ = "generatedByGithub2.patch", = "GithubPrDiff" },
"test.patch",
"test2.patch"
]
Limitations
It's only possible to patch dependencies of binary crates as it is not possible for a subcommand to intercept the build process.