Proposal for the follows attribute for dependencies in Cargo.toml to match the version of another dependency or vice versa.
Problem
Resolving shared dependencies in Cargo is implicit, but I propose a method for explicit resolution by the user with the ability to inherit versions between them, similar to how it works in nix flakes. This would allow root->A, which depends on B, to use the specified version of root->B so that root->A->B = root->B.
Consideration
Nix Flake Example
For those unfamiliar with how the follows attribute works in nix flakes:
The environment uses nixpkgs as a dependency from branch 23.11.
The environment uses rust-overlay as a dependency from branch master.
rust-overlay also has nixpkgs as a dependency, and inputs.nixpkgs.follows = "nixpkgs"; makes rust-overlay follow the same version of nixpkgs as described above.
No because Cargo eagerly picks the latest version for a version range, so this would pick a higher version than the other dependency. This only works if the version requirements are compatible.
There are a lot of related cases where re-exports don't work
There has been a related RFC for it already:
The original public/private dependency RFC tried to provide a solution for this but it caused problems. In the follow up RFC, I actually point out a future possibility related to the proposal here under Caller-declared relations.
[package]
name = "some-cli"
[dependencies]
clap = { from = ["clap_complete", "clap_mangen"] }
clap_complete = "3.4"
clap_mangen = "3.4"
There the idea is that you can say "for this dependency, select the source for it from these dependencies' public dependencies". Limiting this to public dependencies is an important distinction because dependencies are implementation details and can come and go.