Skip to content

Commit a58725a

Browse files
committed
Use version from rustbuild.
1 parent f5cb79e commit a58725a

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed

src/bin/cargo/cli.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,7 @@ pub fn get_version_string(is_verbose: bool) -> String {
164164
let version = cargo::version();
165165
let mut version_string = format!("cargo {}\n", version);
166166
if is_verbose {
167-
version_string.push_str(&format!(
168-
"release: {}.{}.{}\n",
169-
version.major, version.minor, version.patch
170-
));
167+
version_string.push_str(&format!("release: {}\n", version.version,));
171168
if let Some(ref cfg) = version.cfg_info {
172169
if let Some(ref ci) = cfg.commit_info {
173170
version_string.push_str(&format!("commit-hash: {}\n", ci.commit_hash));

src/cargo/version.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,16 @@ pub struct CfgInfo {
1919

2020
/// Cargo's version.
2121
pub struct VersionInfo {
22-
pub major: u8,
23-
pub minor: u8,
24-
pub patch: u8,
22+
/// Cargo's version, such as "1.57.0", "1.58.0-beta.1", "1.59.0-nightly", etc.
23+
pub version: String,
2524
/// Information that's only available when we were built with
2625
/// rustbuild, rather than Cargo itself.
2726
pub cfg_info: Option<CfgInfo>,
2827
}
2928

3029
impl fmt::Display for VersionInfo {
3130
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32-
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)?;
33-
if let Some(channel) = self.cfg_info.as_ref().map(|ci| &ci.release_channel) {
34-
if channel != "stable" {
35-
write!(f, "-{}", channel)?;
36-
}
37-
};
31+
write!(f, "{}", self.version)?;
3832

3933
if let Some(ref cfg) = self.cfg_info {
4034
if let Some(ref ci) = cfg.commit_info {
@@ -53,23 +47,28 @@ pub fn version() -> VersionInfo {
5347
};
5448
}
5549

56-
// So this is pretty horrible...
57-
// There are two versions at play here:
58-
// - version of cargo-the-binary, which you see when you type `cargo --version`
59-
// - version of cargo-the-library, which you download from crates.io for use
60-
// in your packages.
61-
//
62-
// We want to make the `binary` version the same as the corresponding Rust/rustc release.
63-
// At the same time, we want to keep the library version at `0.x`, because Cargo as
64-
// a library is (and probably will always be) unstable.
65-
//
66-
// Historically, Cargo used the same version number for both the binary and the library.
67-
// Specifically, rustc 1.x.z was paired with cargo 0.x+1.w.
68-
// We continue to use this scheme for the library, but transform it to 1.x.w for the purposes
69-
// of `cargo --version`.
70-
let major = 1;
71-
let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap() - 1;
72-
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u8>().unwrap();
50+
// This is the version set in rustbuild, which we use to match rustc.
51+
let version = option_env_str!("CFG_RELEASE").unwrap_or_else(|| {
52+
// If cargo is not being built by rustbuild, then we just use the
53+
// version from cargo's own `Cargo.toml`.
54+
//
55+
// There are two versions at play here:
56+
// - version of cargo-the-binary, which you see when you type `cargo --version`
57+
// - version of cargo-the-library, which you download from crates.io for use
58+
// in your packages.
59+
//
60+
// The library is permanently unstable, so it always has a 0 major
61+
// version. However, the CLI now reports a stable 1.x version
62+
// (starting in 1.26) which stays in sync with rustc's version.
63+
//
64+
// Coincidentally, the minor version for cargo-the-library is always
65+
// +1 of rustc's minor version (that is, `rustc 1.11.0` corresponds to
66+
// `cargo `0.12.0`). The versions always get bumped in lockstep, so
67+
// this should continue to hold.
68+
let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap() - 1;
69+
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u8>().unwrap();
70+
format!("1.{}.{}", minor, patch)
71+
});
7372

7473
match option_env!("CFG_RELEASE_CHANNEL") {
7574
// We have environment variables set up from configure/make.
@@ -80,9 +79,7 @@ pub fn version() -> VersionInfo {
8079
commit_date: option_env_str!("CFG_COMMIT_DATE").unwrap(),
8180
});
8281
VersionInfo {
83-
major,
84-
minor,
85-
patch,
82+
version,
8683
cfg_info: Some(CfgInfo {
8784
release_channel: option_env_str!("CFG_RELEASE_CHANNEL").unwrap(),
8885
commit_info,
@@ -91,9 +88,7 @@ pub fn version() -> VersionInfo {
9188
}
9289
// We are being compiled by Cargo itself.
9390
None => VersionInfo {
94-
major,
95-
minor,
96-
patch,
91+
version,
9792
cfg_info: None,
9893
},
9994
}

0 commit comments

Comments
 (0)