Skip to content

Commit

Permalink
Bring the version command output in line with other rust tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jun 2, 2022
1 parent 4f5c7aa commit 84477e0
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 65 deletions.
74 changes: 29 additions & 45 deletions crates/rust-analyzer/build.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
//! Construct version in the `commit-hash date chanel` format

use std::{env, path::PathBuf, process::Command};
use std::{
env,
path::{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");
}
}

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 +34,24 @@ 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,),
));
fn set_commit_info() {
if !Path::new(".git").exists() {
return;
}
let stdout = String::from_utf8(output.stdout)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
Ok(stdout.trim().to_string())
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::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::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::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
26 changes: 14 additions & 12 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 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
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");
let _e = sh.push_env("CARGO_PROFILE_RELEASE_LTO", "thin");

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

0 comments on commit 84477e0

Please sign in to comment.