Skip to content

Commit 84477e0

Browse files
committed
Bring the version command output in line with other rust tools
1 parent 4f5c7aa commit 84477e0

File tree

7 files changed

+54
-65
lines changed

7 files changed

+54
-65
lines changed

crates/rust-analyzer/build.rs

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
//! Construct version in the `commit-hash date chanel` format
22
3-
use std::{env, path::PathBuf, process::Command};
3+
use std::{
4+
env,
5+
path::{Path, PathBuf},
6+
process::Command,
7+
};
48

59
fn main() {
610
set_rerun();
7-
println!("cargo:rustc-env=REV={}", rev());
11+
set_commit_info();
12+
if option_env!("CFG_RELEASE").is_none() {
13+
println!("cargo:rustc-env=POKE_RA_DEVS");
14+
}
815
}
916

1017
fn set_rerun() {
11-
println!("cargo:rerun-if-env-changed=RUST_ANALYZER_REV");
18+
println!("cargo:rerun-if-env-changed=CFG_RELEASE");
1219

1320
let mut manifest_dir = PathBuf::from(
1421
env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
@@ -27,47 +34,24 @@ fn set_rerun() {
2734
println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
2835
}
2936

30-
fn rev() -> String {
31-
if let Ok(rev) = env::var("RUST_ANALYZER_REV") {
32-
return rev;
33-
}
34-
35-
if let Some(commit_hash) = commit_hash() {
36-
let mut buf = commit_hash;
37-
38-
if let Some(date) = build_date() {
39-
buf.push(' ');
40-
buf.push_str(&date);
41-
}
42-
43-
let channel = env::var("RUST_ANALYZER_CHANNEL").unwrap_or_else(|_| "dev".to_string());
44-
buf.push(' ');
45-
buf.push_str(&channel);
46-
47-
return buf;
48-
}
49-
50-
"???????".to_string()
51-
}
52-
53-
fn commit_hash() -> Option<String> {
54-
exec("git rev-parse --short HEAD").ok()
55-
}
56-
57-
fn build_date() -> Option<String> {
58-
exec("date -u +%Y-%m-%d").ok()
59-
}
60-
61-
fn exec(command: &str) -> std::io::Result<String> {
62-
let args = command.split_ascii_whitespace().collect::<Vec<_>>();
63-
let output = Command::new(args[0]).args(&args[1..]).output()?;
64-
if !output.status.success() {
65-
return Err(std::io::Error::new(
66-
std::io::ErrorKind::InvalidData,
67-
format!("command {:?} returned non-zero code", command,),
68-
));
37+
fn set_commit_info() {
38+
if !Path::new(".git").exists() {
39+
return;
6940
}
70-
let stdout = String::from_utf8(output.stdout)
71-
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
72-
Ok(stdout.trim().to_string())
41+
let output = match Command::new("git")
42+
.arg("log")
43+
.arg("-1")
44+
.arg("--date=short")
45+
.arg("--format=%H %h %cd")
46+
.output()
47+
{
48+
Ok(output) if output.status.success() => output,
49+
_ => return,
50+
};
51+
let stdout = String::from_utf8(output.stdout).unwrap();
52+
let mut parts = stdout.split_whitespace();
53+
let mut next = || parts.next().unwrap();
54+
println!("cargo:rustc-env=RA_COMMIT_HASH={}", next());
55+
println!("cargo:rustc-env=RA_COMMIT_SHORT_HASH={}", next());
56+
println!("cargo:rustc-env=RA_COMMIT_DATE={}", next())
7357
}

crates/rust-analyzer/src/bin/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn try_main() -> Result<()> {
7070
return Ok(());
7171
}
7272
if cmd.version {
73-
println!("rust-analyzer {}", env!("REV"));
73+
println!("rust-analyzer {}", rust_analyzer::version::version());
7474
return Ok(());
7575
}
7676
if cmd.help {
@@ -150,7 +150,7 @@ fn with_extra_thread(
150150
}
151151

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

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

@@ -192,7 +192,7 @@ fn run_server() -> Result<()> {
192192
capabilities: server_capabilities,
193193
server_info: Some(lsp_types::ServerInfo {
194194
name: String::from("rust-analyzer"),
195-
version: Some(String::from(env!("REV"))),
195+
version: Some(rust_analyzer::version::version().to_string()),
196196
}),
197197
offset_encoding: if supports_utf8(config.caps()) {
198198
Some("utf-8".to_string())

crates/rust-analyzer/src/cli/lsif.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::cli::{
2222
};
2323
use crate::line_index::{LineEndings, LineIndex, OffsetEncoding};
2424
use crate::to_proto;
25+
use crate::version::version;
2526

2627
/// Need to wrap Snapshot to provide `Clone` impl for `map_with`
2728
struct Snap<DB>(DB);
@@ -312,7 +313,7 @@ impl flags::Lsif {
312313
tool_info: Some(lsp_types::lsif::ToolInfo {
313314
name: "rust-analyzer".to_string(),
314315
args: vec![],
315-
version: Some(env!("REV").to_string()),
316+
version: Some(version().to_string()),
316317
}),
317318
}));
318319
for file in si.files {

crates/rust-analyzer/src/dispatch.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use serde::{de::DeserializeOwned, Serialize};
88
use crate::{
99
global_state::{GlobalState, GlobalStateSnapshot},
1010
main_loop::Task,
11+
version::version,
1112
LspError, Result,
1213
};
1314

@@ -144,7 +145,7 @@ impl<'a> RequestDispatcher<'a> {
144145
match res {
145146
Ok(params) => {
146147
let panic_context =
147-
format!("\nversion: {}\nrequest: {} {:#?}", env!("REV"), R::METHOD, params);
148+
format!("\nversion: {}\nrequest: {} {:#?}", version(), R::METHOD, params);
148149
Some((req, params, panic_context))
149150
}
150151
Err(err) => {
@@ -248,7 +249,7 @@ impl<'a> NotificationDispatcher<'a> {
248249
};
249250
let _pctx = stdx::panic_context::enter(format!(
250251
"\nversion: {}\nnotification: {}",
251-
env!("REV"),
252+
version(),
252253
N::METHOD
253254
));
254255
f(self.global_state, params)?;

crates/rust-analyzer/src/lib.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,28 @@ macro_rules! eprintln {
1616
($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
1717
}
1818

19-
mod global_state;
20-
mod reload;
21-
mod main_loop;
22-
mod dispatch;
23-
mod handlers;
2419
mod caps;
2520
mod cargo_target_spec;
26-
mod to_proto;
27-
mod from_proto;
28-
mod semantic_tokens;
29-
mod markdown;
3021
mod diagnostics;
22+
mod diff;
23+
mod dispatch;
24+
mod from_proto;
25+
mod global_state;
26+
mod handlers;
3127
mod line_index;
3228
mod lsp_utils;
33-
mod task_pool;
29+
mod main_loop;
30+
mod markdown;
3431
mod mem_docs;
35-
mod diff;
3632
mod op_queue;
37-
pub mod lsp_ext;
33+
mod reload;
34+
mod semantic_tokens;
35+
mod task_pool;
36+
mod to_proto;
37+
mod version;
38+
3839
pub mod config;
40+
pub mod lsp_ext;
3941

4042
#[cfg(test)]
4143
mod integrated_benchmarks;

crates/rust-analyzer/src/lsp_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl GlobalState {
7474
/// panicky is a good idea, let's see if we can keep our awesome bleeding
7575
/// edge users from being upset!
7676
pub(crate) fn poke_rust_analyzer_developer(&mut self, message: String) {
77-
let from_source_build = env!("REV").contains("dev");
77+
let from_source_build = option_env!("POKE_RA_DEVS").is_some();
7878
let profiling_enabled = std::env::var("RA_PROFILE").is_ok();
7979
if from_source_build || profiling_enabled {
8080
self.show_message(lsp_types::MessageType::ERROR, message)

xtask/src/dist.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ fn dist_client(
7272
}
7373

7474
fn dist_server(sh: &Shell, release_channel: &str, target: &Target) -> anyhow::Result<()> {
75-
let _e = sh.push_env("RUST_ANALYZER_CHANNEL", release_channel);
75+
let _e = sh.push_env("CFG_RELEASE_CHANNEL", release_channel);
76+
let _e = sh.push_env("CFG_RELEASE", "0.0.0");
7677
let _e = sh.push_env("CARGO_PROFILE_RELEASE_LTO", "thin");
7778

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

0 commit comments

Comments
 (0)