bin+lib cargo-patch

Cargo Subcommand for patching dependencies using patch files

29 releases

0.3.2 Jul 30, 2023
0.3.0 Jan 8, 2023
0.2.9 Nov 21, 2022
0.2.3 Jul 24, 2022
0.1.7 Jun 6, 2020

#1483 in Command line utilities

Download history 47/week @ 2024-06-10 30/week @ 2024-06-17 134/week @ 2024-06-24 164/week @ 2024-07-01 195/week @ 2024-07-08 219/week @ 2024-07-15 222/week @ 2024-07-22 147/week @ 2024-07-29 161/week @ 2024-08-05 124/week @ 2024-08-12 96/week @ 2024-08-19 154/week @ 2024-08-26 112/week @ 2024-09-02 176/week @ 2024-09-09 166/week @ 2024-09-16 197/week @ 2024-09-23

687 downloads per month

MIT license

29KB
514 lines

cargo-patch

Cargo-Patch is a Cargo Subcommand which allows patching dependencies using patch files.

Installation

Simply run:

cargo install cargo-patch

This is not necessary when patching via build.rs file

Usage

To patch a dependency one has to add the following to Cargo.toml:

[package.metadata.patch.serde]
version = "1.0"
patches = [
    "test.patch"
]

It specifies which dependency to patch (in this case serde) and one or more patchfiles to apply. Running:

cargo patch

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

[patch.crates-io]
serde = { path = './target/patch/serde-1.0.110' }

Instead of running cargo patch its also possible to add a build.rs file like this:

fn main() {
    println!("cargo:rerun-if-changed=Cargo.toml");
    println!("cargo:rerun-if-changed=patches/");
    cargo_patch::patch().expect("Failed while patching");
}

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

[package.metadata.patch.serde]
version = "1.0"
patches = [
    { path = "generatedByGithub.patch", source = "GithubPrDiff" },
    { path = "generatedByGithub2.patch", source = "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.

Dependencies

~53–88MB
~1.5M SLoC