Dependency Kinds, Sources & [patch]
A dependency in Cargo has three independent properties: what kind it is (normal, dev, build), where it comes from (a registry, a path, a Git repository) and what it's called in your code. This lesson covers all three, plus [patch], the tool for overriding a dependency everywhere in the graph.
cargo add and cargo remove
You can edit Cargo.toml by hand, but cargo add (built into Cargo since 1.62; it began life in the cargo-edit plugin) picks the current version, writes a caret requirement with it, and lists the dependency's features:
cargo new app --vcs none -q
cd app
cargo add serde --features derive 2>&1 | grep -v Unpacking
cargo add --dev regex 2>&1 | grep -E 'Adding regex'
cargo add --build cc 2>&1 | grep -E 'Adding cc'
cargo add --target 'cfg(unix)' libc 2>&1 | grep -E 'Adding libc'
cargo add syn@2 --rename syn2 --no-default-features --features parsing 2>&1 | grep -E 'Adding syn'
cat Cargo.toml
echo "--- remove one again"
cargo remove --dev regex Adding serde v1.0.229 to dependencies
Features:
+ derive
+ serde_derive
+ std
- alloc
- rc
- unstable
Locking 7 packages to highest Rust 1.99.1 compatible versions
Adding regex v1.13.1 to dev-dependencies
Adding regex v1.13.1
Adding regex-automata v0.4.18
Adding regex-syntax v0.8.11
Adding cc v1.10.0 to build-dependencies
Adding cc v1.10.0
Adding libc v0.2.191 to dependencies for target `cfg(unix)`
Adding libc v0.2.191
Adding syn v2 to dependencies
Adding syn v2.0.119 (available: v3.0.9)
[package]
name = "app"
version = "0.1.0"
edition = "2024"
[dependencies]
serde = { version = "1.0.229", features = ["derive"] }
syn2 = { version = "2", package = "syn", default-features = false, features = ["parsing"] }
[dev-dependencies]
regex = "1.13.1"
[build-dependencies]
cc = "1.10.0"
[target."cfg(unix)".dependencies]
libc = "0.2.191"
--- remove one again
Removing regex from dev-dependencies
The +/- list shows which features are on. Everything cargo add wrote is visible in the manifest, so review it like any other change.
The three kinds
| Kind | Table | Available to | Affects users of your library? |
|---|---|---|---|
| normal | [dependencies] |
library, binaries, tests, examples, benches | yes, they compile it too |
| dev | [dev-dependencies] |
tests, examples, benches only | no |
| build | [build-dependencies] |
build.rs only, compiled for the host |
yes (it runs during their build) |
Dev-dependencies are the right place for test frameworks, benchmark harnesses and anything used only in examples. A common mistake is to put regex in [dependencies] because one test uses it: every downstream user then compiles regex for nothing.
Platform-specific dependencies use a cfg expression: [target.'cfg(unix)'.dependencies], [target.'cfg(windows)'.dependencies], or a full target triple. They are only compiled when building for a matching target.
Sources: registry, path, Git
[dependencies]
serde = "1.0.200" # crates.io (the default registry)
units = { version = "1", registry = "local" } # an alternative registry
geom = { path = "../geom" } # a local folder
geom2 = { path = "../geom", version = "0.3" } # path while developing, registry when published
fancy = { git = "https://github.com/example/fancy", tag = "v0.4.0" } # or branch = / rev =- A path dependency is built from source in place. If you publish a crate that has one, it must also carry a
version: crates.io drops thepathand uses the version. - A Git dependency is locked to a commit in
Cargo.lock. crates.io doesn't allow publishing crates with Git dependencies.
Renaming: two names, one crate (or two versions)
The package key lets the name in your code differ from the name on the registry. The --rename above wrote a syn2 = { version = "2", package = "syn", ... } entry. The classic use is a migration where code uses two major versions side by side, here syn 3 and syn 2 (the Rust parser behind most procedural macros):
cargo new app --vcs none -q
cd app
cat >> Cargo.toml <<'T'
syn = "3"
syn2 = { package = "syn", version = "2" }
T
cat > src/main.rs <<'RS'
fn main() {
for src in ["hello", "r#type", "1st"] {
let new = syn::parse_str::<syn::Ident>(src).is_ok();
let old = syn2::parse_str::<syn2::Ident>(src).is_ok();
println!("{src:7} identifier? syn 3: {new}, syn 2: {old}");
}
}
RS
cargo run -q 2>&1 | grep -v Unpacking
cargo tree --depth 1hello identifier? syn 3: true, syn 2: true
r#type identifier? syn 3: true, syn 2: true
1st identifier? syn 3: false, syn 2: false
app v0.1.0 (/home/learner/app)
├── syn v2.0.119
└── syn v3.0.9
Both versions are compiled and linked into one binary; Duplicate Versions explains when that happens on its own and what it costs.
[patch]: replace a crate everywhere
Sometimes you need a fix that isn't released yet, in a crate that sits deep in your graph. [patch.<source>] replaces a crate from that source with another source, for every dependent in the graph, as long as the version is compatible. Here a local fork of anyhow (version 1.0.105, newer than the registry's 1.0.104) replaces the registry crate:
cargo new --lib anyhow-fork --name anyhow --vcs none -q
sed -i 's/^version = .*/version = "1.0.105"/' anyhow-fork/Cargo.toml
echo 'pub fn origin() -> &'"'"'static str { "patched fork" }' > anyhow-fork/src/lib.rs
cargo new app --vcs none -q
cd app
cargo add anyhow -q
cargo generate-lockfile -q
cat >> Cargo.toml <<'T'
[patch.crates-io]
anyhow = { path = "../anyhow-fork" }
T
echo 'fn main() { println!("{}", anyhow::origin()); }' > src/main.rs
cargo tree 2>&1
echo "--- the lockfile still pins 1.0.104: update it"
cargo update -p anyhow 2>&1
cargo tree
cargo run -qwarning: patch `anyhow v1.0.105 (/home/learner/anyhow-fork)` was not used in the crate graph
help: Check that the patched package version and available features are compatible
with the dependency requirements. If the patch has a different version from
what is locked in the Cargo.lock file, run `cargo update` to use the new
version. This may also occur with an optional dependency that is not enabled.
Unpacking anyhow v1.0.104 (registry `/opt/crates`)
app v0.1.0 (/home/learner/app)
└── anyhow v1.0.104
--- the lockfile still pins 1.0.104: update it
Locking 1 package to highest Rust 1.99.1 compatible version
Adding anyhow v1.0.105 (/home/learner/anyhow-fork)
Removing anyhow v1.0.104
app v0.1.0 (/home/learner/app)
└── anyhow v1.0.105 (/home/learner/anyhow-fork)
patched fork
The first cargo tree shows the most common [patch] surprise: the patch was not used, because Cargo.lock already pinned 1.0.104, and Cargo said so in a warning. After cargo update -p anyhow the fork took over. Rules to remember:
- The patched crate must have the same name and a version that satisfies the requirements in the graph.
[patch]only works in the root manifest (the workspace root); patches in dependencies are ignored.- It's a temporary tool: remove the patch when the fix is released, so you're back on registry versions.
Test yourself
A test file uses regex, the library itself doesn't. Where does regex belong?
- [dependencies]
- [dev-dependencies]
- [build-dependencies]
- [patch.crates-io]
What does syn2 = { package = "syn", version = "2" } do?
- Installs a crate called syn2
- Depends on syn 2 but names it
syn2in code - Aliases syn 3
- Patches syn
You added [patch.crates-io] for anyhow, but cargo tree still shows the registry version and warns the patch was not used. Most likely fix?
- Move the patch into the dependency's manifest
- Run
cargo update -p anyhowso the lockfile can pick the patched version - Rename the fork
- Use --frozen