How to override a crate dependency?

Hi I am trying to write and compile programs for the ps3. The program I wrote merely prints "Hello World" using the crate libc-print, which depends on the crate libc. I git cloned libc and made a trivial patch. But when I run cargo build libc-print still build libc from crates.io.

I never figured out how to make libc-print depend on my local fork of libc. Instead I had to clone libc-print and modify its libc dependency. I would like to know how to override a dependency of a crate from the Cargo.toml file rather than cloning every time a crate depends on libc.

My current solution:

[package]
name = "hello_core"
version = "0.1.0"
edition = "2024"

[dependencies]
libc-print = "0.1.23"
panic-halt = "1.0.0"

[patch.crates-io]
libc-print = { path = "../rust-libc-print"}

Patches are transitive, so patching libc means that libc-print will have access to that patch also. The version of the patch does matter though. From the docs:

The way [patch] works is that it’ll load the dependency at ../path/to/uuid and then whenever crates.io is queried for versions of uuid it’ll also return the local version.

This means that the version number of the local checkout is significant and will affect whether the patch is used.

My guess is that there is a version mismatch between your patch and libc-print. E.g. maybe your local libc has 1.0.0-alpha.3 as its version, which is incompatible with libc-print's libc dependency declaration of ^0.2.148.

1 Like