Allow Cargo patches without modifying `Cargo.lock`
Problem
I maintain a monorepo containing several Rust projects as subtrees. During development, I use [patch] entries to make those projects compile against local versions of shared dependencies. The same patches are also needed when building the system with Nix.
This works for compilation, but Cargo writes patch information into Cargo.lock. That creates unwanted changes from normal editor activity, including rust-analyzer or even just from running cargo normally, and makes the lockfile describe the local development setup instead of the dependency declared by the package, which adds unwanted changes in git and makes the packages impossible to be compiled separately without modifying the lock file all the time.
For example, a dependency declared from crates.io or GitHub should continue to point to that source in Cargo.lock, even when a local path patch is used during development. Unused patch entries should not be added either.
Proposed Solution
Add a boolean configuration option:
[resolver]
write-patches-to-lockfile = falseWhen disabled, Cargo should:
- Continue applying patches during dependency resolution and compilation.
- Avoid writing
[[patch.unused]]entries. - Ignore existing
[[patch.unused]]entries when reading the lockfile. - Preserve the original dependency source in
Cargo.lock, including crates.io or GitHub sources. - Preserve the existing lockfile contents when applying a patch does not otherwise change the dependency graph.
The default should remain true for compatibility with current Cargo behavior.
Notes
I have vibecoded an ad-hoc patch to borrow myself some time to develop the main project while this feature is not in standard cargo. It's available at https://github.com/ardos-os/cargo/tree/codex/lockfile-patch-sources If you're fine upstreaming that exact code I can submit a pull request or if you want to change something in the implementation, i'm available to make those adjustments.
Source: rust-lang/cargo