Skip to content

Bring the version command output in line with other rust tools #12449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 22 additions & 45 deletions crates/rust-analyzer/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use std::{env, path::PathBuf, process::Command};

fn main() {
set_rerun();
println!("cargo:rustc-env=REV={}", rev());
set_commit_info();
if option_env!("CFG_RELEASE").is_none() {
println!("cargo:rustc-env=POKE_RA_DEVS=1");
}
}

fn set_rerun() {
println!("cargo:rerun-if-env-changed=RUST_ANALYZER_REV");
println!("cargo:rerun-if-env-changed=CFG_RELEASE");

let mut manifest_dir = PathBuf::from(
env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
Expand All @@ -27,47 +30,21 @@ fn set_rerun() {
println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
}

fn rev() -> String {
if let Ok(rev) = env::var("RUST_ANALYZER_REV") {
return rev;
}

if let Some(commit_hash) = commit_hash() {
let mut buf = commit_hash;

if let Some(date) = build_date() {
buf.push(' ');
buf.push_str(&date);
}

let channel = env::var("RUST_ANALYZER_CHANNEL").unwrap_or_else(|_| "dev".to_string());
buf.push(' ');
buf.push_str(&channel);

return buf;
}

"???????".to_string()
}

fn commit_hash() -> Option<String> {
exec("git rev-parse --short HEAD").ok()
}

fn build_date() -> Option<String> {
exec("date -u +%Y-%m-%d").ok()
}

fn exec(command: &str) -> std::io::Result<String> {
let args = command.split_ascii_whitespace().collect::<Vec<_>>();
let output = Command::new(args[0]).args(&args[1..]).output()?;
if !output.status.success() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("command {:?} returned non-zero code", command,),
));
}
let stdout = String::from_utf8(output.stdout)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
Ok(stdout.trim().to_string())
fn set_commit_info() {
let output = match Command::new("git")
.arg("log")
.arg("-1")
.arg("--date=short")
.arg("--format=%H %h %cd")
.output()
{
Ok(output) if output.status.success() => output,
_ => return,
};
let stdout = String::from_utf8(output.stdout).unwrap();
let mut parts = stdout.split_whitespace();
let mut next = || parts.next().unwrap();
println!("cargo:rustc-env=RA_COMMIT_HASH={}", next());
println!("cargo:rustc-env=RA_COMMIT_SHORT_HASH={}", next());
println!("cargo:rustc-env=RA_COMMIT_DATE={}", next())
}
6 changes: 3 additions & 3 deletions crates/rust-analyzer/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn try_main() -> Result<()> {
return Ok(());
}
if cmd.version {
println!("rust-analyzer {}", env!("REV"));
println!("rust-analyzer {}", rust_analyzer::version());
return Ok(());
}
if cmd.help {
Expand Down Expand Up @@ -150,7 +150,7 @@ fn with_extra_thread(
}

fn run_server() -> Result<()> {
tracing::info!("server version {} will start", env!("REV"));
tracing::info!("server version {} will start", rust_analyzer::version());

let (connection, io_threads) = Connection::stdio();

Expand Down Expand Up @@ -192,7 +192,7 @@ fn run_server() -> Result<()> {
capabilities: server_capabilities,
server_info: Some(lsp_types::ServerInfo {
name: String::from("rust-analyzer"),
version: Some(String::from(env!("REV"))),
version: Some(rust_analyzer::version().to_string()),
}),
offset_encoding: if supports_utf8(config.caps()) {
Some("utf-8".to_string())
Expand Down
3 changes: 2 additions & 1 deletion crates/rust-analyzer/src/cli/lsif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::cli::{
};
use crate::line_index::{LineEndings, LineIndex, OffsetEncoding};
use crate::to_proto;
use crate::version::version;

/// Need to wrap Snapshot to provide `Clone` impl for `map_with`
struct Snap<DB>(DB);
Expand Down Expand Up @@ -312,7 +313,7 @@ impl flags::Lsif {
tool_info: Some(lsp_types::lsif::ToolInfo {
name: "rust-analyzer".to_string(),
args: vec![],
version: Some(env!("REV").to_string()),
version: Some(version().to_string()),
}),
}));
for file in si.files {
Expand Down
5 changes: 3 additions & 2 deletions crates/rust-analyzer/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::{de::DeserializeOwned, Serialize};
use crate::{
global_state::{GlobalState, GlobalStateSnapshot},
main_loop::Task,
version::version,
LspError, Result,
};

Expand Down Expand Up @@ -144,7 +145,7 @@ impl<'a> RequestDispatcher<'a> {
match res {
Ok(params) => {
let panic_context =
format!("\nversion: {}\nrequest: {} {:#?}", env!("REV"), R::METHOD, params);
format!("\nversion: {}\nrequest: {} {:#?}", version(), R::METHOD, params);
Some((req, params, panic_context))
}
Err(err) => {
Expand Down Expand Up @@ -248,7 +249,7 @@ impl<'a> NotificationDispatcher<'a> {
};
let _pctx = stdx::panic_context::enter(format!(
"\nversion: {}\nnotification: {}",
env!("REV"),
version(),
N::METHOD
));
f(self.global_state, params)?;
Expand Down
28 changes: 15 additions & 13 deletions crates/rust-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ macro_rules! eprintln {
($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
}

mod global_state;
mod reload;
mod main_loop;
mod dispatch;
mod handlers;
mod caps;
mod cargo_target_spec;
mod to_proto;
mod from_proto;
mod semantic_tokens;
mod markdown;
mod diagnostics;
mod diff;
mod dispatch;
mod from_proto;
mod global_state;
mod handlers;
mod line_index;
mod lsp_utils;
mod task_pool;
mod main_loop;
mod markdown;
mod mem_docs;
mod diff;
mod op_queue;
pub mod lsp_ext;
mod reload;
mod semantic_tokens;
mod task_pool;
mod to_proto;
mod version;

pub mod config;
pub mod lsp_ext;

#[cfg(test)]
mod integrated_benchmarks;
Expand All @@ -44,7 +46,7 @@ use std::fmt;

use serde::de::DeserializeOwned;

pub use crate::{caps::server_capabilities, main_loop::main_loop};
pub use crate::{caps::server_capabilities, main_loop::main_loop, version::version};

pub type Error = Box<dyn std::error::Error + Send + Sync>;
pub type Result<T, E = Error> = std::result::Result<T, E>;
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/lsp_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl GlobalState {
/// panicky is a good idea, let's see if we can keep our awesome bleeding
/// edge users from being upset!
pub(crate) fn poke_rust_analyzer_developer(&mut self, message: String) {
let from_source_build = env!("REV").contains("dev");
let from_source_build = option_env!("POKE_RA_DEVS").is_some();
let profiling_enabled = std::env::var("RA_PROFILE").is_ok();
if from_source_build || profiling_enabled {
self.show_message(lsp_types::MessageType::ERROR, message)
Expand Down
57 changes: 57 additions & 0 deletions crates/rust-analyzer/src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Code for representing rust-analyzer's release version number.

use std::fmt;

/// Information about the git repository where rust-analyzer was built from.
pub struct CommitInfo {
pub short_commit_hash: &'static str,
pub commit_hash: &'static str,
pub commit_date: &'static str,
}

/// Cargo's version.
pub struct VersionInfo {
/// rust-analyzer's version, such as "1.57.0", "1.58.0-beta.1", "1.59.0-nightly", etc.
pub version: &'static str,
/// The release channel we were built for (stable/beta/nightly/dev).
///
/// `None` if not built via rustbuild.
pub 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 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 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 }
}
3 changes: 2 additions & 1 deletion xtask/src/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ fn dist_client(
}

fn dist_server(sh: &Shell, release_channel: &str, target: &Target) -> anyhow::Result<()> {
let _e = sh.push_env("RUST_ANALYZER_CHANNEL", release_channel);
let _e = sh.push_env("CFG_RELEASE_CHANNEL", release_channel);
let _e = sh.push_env("CFG_RELEASE", "0.0.0");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't currently have a notion of server version when doing our own builds so I left this as 0.0.0 (if left unset we would poke people again). We could of course also set this to any string we want, not necessarily a version. It would probably make sense to make this differ from the usual 1.56.0-stable like versions of the rust toolchain since this build is independent from that.

let _e = sh.push_env("CARGO_PROFILE_RELEASE_LTO", "thin");

// Uncomment to enable debug info for releases. Note that:
Expand Down