Skip to content

Allow Cargo patches without modifying Cargo.lock #17473

Description

@coffeeispower

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 = false

When 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lockfileArea: Cargo.lock issuesA-patchArea: [patch] table overrideC-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions