rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.66k stars 2.41k forks source link

Feature request: single source of truth for crate versions #2075

Closed Diggsey closed 1 year ago

Diggsey commented 8 years ago

Often you need to specify the crate version in several places:

Additionally, you sometimes have several crates within the same repository, whose versions you want to keep in lock-step. The crate version must then additionally be specified:

For more complex projects, it's easy to imagine that there's other information which may be desirable, eg. git commit hash, build date, and additional configuration options (think rust's channel system).

All in all, this can make publishing a new version somewhat tedious and error-prone. Honestly I don't know what a good and general solution is that covers all of these cases.

Currently the only way I can see of doing it would be to turn most of the project into template files which are passed through a pre-processor by the build script, but this is extremely intrusive, and wouldn't work for redundancy within Cargo.toml itself.

alexcrichton commented 8 years ago

For code at least cargo will pass the version as an environment variable (accessible via env!), but between Cargo.toml instances this can definitely be a problem.

rtaycher commented 8 years ago

For simple one crate binaries clap-rs reads the aforementioned env variable w/ crate_version! macro, and could be added to docopt and getopts.

mitsuhiko commented 7 years ago

With the new workspaces feature this is becoming a bit more relevant now I think. We just set up a larger Rust project into many independent sub crates which we want to release together. Right now I'm planning on making a script that bumps all versions in one go but not having to do that would be great.

epage commented 2 years ago

In the sub-crate's Cargo.toml

With https://github.com/rust-lang/rfcs/pull/2906

Workspace

[workspace]
version = "1.2.3"

Crate

[package]
name = "foo"
version.workspace = true

Within the [dependencies] section of the parent Cargo.toml

https://github.com/rust-lang/rfcs/pull/2906 provides a solution for this but it is a breaking change so in https://github.com/rust-lang/cargo/issues/8415 we are proposing to drop it.

Using something like cargo-release helps. When you bump the version of one crate it automatically updates all dependency specifications pointing to that crate. It also supports replacements so you can update versions in install instructions, etc.

For more complex projects, it's easy to imagine that there's other information which may be desirable, eg. git commit hash, build date, and additional configuration options (think rust's channel system).

I'd recommend checking out https://github.com/baoyachi/shadow-rs or https://github.com/rustyhorde/vergen

weihanglo commented 1 year ago

Since this is a quite old issue, I am going to close it. Cargo's workspace inheritance should help with that in most use cases.

For other feature requests and alternatives, read Ed's comment https://github.com/rust-lang/cargo/issues/2075#issuecomment-1105460973. If you want to integrate cargo-release or other build time information into Cargo, please file an issue with a clear scope. That would be easier to reason and get done. Thank you!