Add version command to proc-macro-srv

This commit is contained in:
Lukas Wirth 2025-07-31 10:04:42 +02:00
parent 218e00d0bb
commit 87625cc166
4 changed files with 139 additions and 18 deletions

View file

@ -2,11 +2,13 @@
//! Driver for proc macro server
#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
#![cfg_attr(not(feature = "sysroot-abi"), allow(unused_crate_dependencies))]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout, clippy::print_stderr)]
#[cfg(feature = "in-rust-tree")]
extern crate rustc_driver as _;
mod version;
#[cfg(any(feature = "sysroot-abi", rust_analyzer))]
mod main_loop;
use clap::{Command, ValueEnum};
@ -25,12 +27,22 @@ fn main() -> std::io::Result<()> {
std::process::exit(122);
}
let matches = Command::new("proc-macro-srv")
.args(&[clap::Arg::new("format")
.long("format")
.action(clap::ArgAction::Set)
.default_value("json")
.value_parser(clap::builder::EnumValueParser::<ProtocolFormat>::new())])
.args(&[
clap::Arg::new("format")
.long("format")
.action(clap::ArgAction::Set)
.default_value("json")
.value_parser(clap::builder::EnumValueParser::<ProtocolFormat>::new()),
clap::Arg::new("version")
.long("version")
.action(clap::ArgAction::SetTrue)
.help("Prints the version of the proc-macro-srv"),
])
.get_matches();
if matches.get_flag("version") {
println!("rust-analyzer-proc-macro-srv {}", version::version());
return Ok(());
}
let &format =
matches.get_one::<ProtocolFormat>("format").expect("format value should always be present");
run(format)

View file

@ -0,0 +1,58 @@
//! Code for representing rust-analyzer's release version number.
#![expect(dead_code)]
use std::fmt;
/// Information about the git repository where rust-analyzer was built from.
pub(crate) struct CommitInfo {
pub(crate) short_commit_hash: &'static str,
pub(crate) commit_hash: &'static str,
pub(crate) commit_date: &'static str,
}
/// Cargo's version.
pub(crate) struct VersionInfo {
/// rust-analyzer's version, such as "1.57.0", "1.58.0-beta.1", "1.59.0-nightly", etc.
pub(crate) version: &'static str,
/// The release channel we were built for (stable/beta/nightly/dev).
///
/// `None` if not built via bootstrap.
pub(crate) release_channel: Option<&'static str>,
/// Information about the Git repository we may have been built from.
///
/// `None` if not built from a git repo.
pub(crate) commit_info: Option<CommitInfo>,
}
impl fmt::Display for VersionInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.version)?;
if let Some(ci) = &self.commit_info {
write!(f, " ({} {})", ci.short_commit_hash, ci.commit_date)?;
};
Ok(())
}
}
/// Returns information about cargo's version.
pub(crate) const fn version() -> VersionInfo {
let version = match option_env!("CFG_RELEASE") {
Some(x) => x,
None => "0.0.0",
};
let release_channel = option_env!("CFG_RELEASE_CHANNEL");
let commit_info = match (
option_env!("RA_COMMIT_SHORT_HASH"),
option_env!("RA_COMMIT_HASH"),
option_env!("RA_COMMIT_DATE"),
) {
(Some(short_commit_hash), Some(commit_hash), Some(commit_date)) => {
Some(CommitInfo { short_commit_hash, commit_hash, commit_date })
}
_ => None,
};
VersionInfo { version, release_channel, commit_info }
}