Skip to content

Commit a82b6ff

Browse files
committed
[ty] Fix server version
1 parent 426fa4b commit a82b6ff

File tree

8 files changed

+54
-23
lines changed

8 files changed

+54
-23
lines changed

crates/ruff/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ pub fn run(
131131
}: Args,
132132
) -> Result<ExitStatus> {
133133
{
134+
ruff_db::set_program_version(crate::version::version().to_string()).unwrap();
134135
let default_panic_hook = std::panic::take_hook();
135136
std::panic::set_hook(Box::new(move |info| {
136137
#[expect(clippy::print_stderr)]

crates/ruff_db/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ pub use web_time::{Instant, SystemTime, SystemTimeError};
2828
pub type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;
2929
pub type FxDashSet<K> = dashmap::DashSet<K, BuildHasherDefault<FxHasher>>;
3030

31+
static VERSION: std::sync::OnceLock<String> = std::sync::OnceLock::new();
32+
33+
/// Returns the version of the executing program if set.
34+
pub fn program_version() -> Option<&'static str> {
35+
VERSION.get().map(|version| version.as_str())
36+
}
37+
38+
/// Sets the version of the executing program.
39+
///
40+
/// ## Errors
41+
/// If the version has already been initialized (can only be set once).
42+
pub fn set_program_version(version: String) -> Result<(), String> {
43+
VERSION.set(version)
44+
}
45+
3146
/// Most basic database that gives access to files, the host system, source code, and parsed AST.
3247
#[salsa::db]
3348
pub trait Db: salsa::Database {

crates/ty/build.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@ use std::{
77
fn main() {
88
// The workspace root directory is not available without walking up the tree
99
// https://github.com/rust-lang/cargo/issues/3946
10-
let workspace_root = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
11-
.join("..")
10+
let ruff_workspace_root = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
1211
.join("..")
1312
.join("..");
13+
let ty_workspace_root = ruff_workspace_root.join("..");
14+
15+
version_info(&ty_workspace_root);
16+
17+
// If not in a git repository, do not attempt to retrieve commit information
18+
let git_dir = ty_workspace_root.join(".git");
19+
if git_dir.exists() {
20+
commit_info(&git_dir, &ty_workspace_root, false);
21+
} else {
22+
// Try if we're inside the ruff repository and, if so, use that commit hash.
23+
let git_dir = ruff_workspace_root.join(".git");
1424

15-
version_info(&workspace_root);
16-
commit_info(&workspace_root);
25+
if git_dir.exists() {
26+
commit_info(&git_dir, &ruff_workspace_root, true);
27+
}
28+
}
1729

1830
let target = std::env::var("TARGET").unwrap();
1931
println!("cargo::rustc-env=RUST_HOST_TARGET={target}");
@@ -45,14 +57,8 @@ fn version_info(workspace_root: &Path) {
4557
}
4658

4759
/// Retrieve commit information from the Git repository.
48-
fn commit_info(workspace_root: &Path) {
49-
// If not in a git repository, do not attempt to retrieve commit information
50-
let git_dir = workspace_root.join(".git");
51-
if !git_dir.exists() {
52-
return;
53-
}
54-
55-
if let Some(git_head_path) = git_head(&git_dir) {
60+
fn commit_info(git_dir: &Path, workspace_root: &Path, is_ruff: bool) {
61+
if let Some(git_head_path) = git_head(git_dir) {
5662
println!("cargo:rerun-if-changed={}", git_head_path.display());
5763

5864
let git_head_contents = fs::read_to_string(git_head_path);
@@ -96,7 +102,10 @@ fn commit_info(workspace_root: &Path) {
96102
let mut describe_parts = describe.split('-');
97103
let last_tag = describe_parts.next().unwrap();
98104

99-
println!("cargo::rustc-env=TY_LAST_TAG={last_tag}");
105+
println!(
106+
"cargo::rustc-env=TY_LAST_TAG={ruff}{last_tag}",
107+
ruff = if is_ruff { "ruff/" } else { "" }
108+
);
100109

101110
// If this is the tagged commit, this component will be missing
102111
println!(

crates/ty/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use ty_server::run_server;
3333

3434
pub fn run() -> anyhow::Result<ExitStatus> {
3535
setup_rayon();
36+
ruff_db::set_program_version(crate::version::version().to_string()).unwrap();
3637

3738
let args = wild::args_os();
3839
let args = argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX)

crates/ty_project/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,13 @@ where
667667
arch = std::env::consts::ARCH
668668
),
669669
));
670+
if let Some(version) = ruff_db::program_version() {
671+
diagnostic.sub(SubDiagnostic::new(
672+
Severity::Info,
673+
format!("Version: {version}"),
674+
));
675+
}
676+
670677
diagnostic.sub(SubDiagnostic::new(
671678
Severity::Info,
672679
format!(

crates/ty_server/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ pub(crate) const DIAGNOSTIC_NAME: &str = "ty";
1717
/// result type is needed.
1818
pub(crate) type Result<T> = anyhow::Result<T>;
1919

20-
pub(crate) fn version() -> &'static str {
21-
env!("CARGO_PKG_VERSION")
22-
}
23-
2420
pub fn run_server() -> anyhow::Result<()> {
2521
let four = NonZeroUsize::new(4).unwrap();
2622

crates/ty_server/src/server.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ impl Server {
5656
let position_encoding = Self::find_best_position_encoding(&client_capabilities);
5757
let server_capabilities = Self::server_capabilities(position_encoding);
5858

59-
let connection = connection.initialize_finish(
60-
id,
61-
&server_capabilities,
62-
crate::SERVER_NAME,
63-
crate::version(),
64-
)?;
59+
let version = ruff_db::program_version().unwrap_or("Unknown");
60+
61+
let connection =
62+
connection.initialize_finish(id, &server_capabilities, crate::SERVER_NAME, version)?;
6563

6664
// The number 32 was chosen arbitrarily. The main goal was to have enough capacity to queue
6765
// some responses before blocking.
@@ -73,6 +71,8 @@ impl Server {
7371
global_options.tracing.log_file.as_deref(),
7472
);
7573

74+
tracing::debug!("Version: {version}");
75+
7676
let mut workspace_for_url = |url: Url| {
7777
let Some(workspace_settings) = workspace_options.as_mut() else {
7878
return (url, ClientOptions::default());

crates/ty_wasm/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub fn version() -> String {
3636
pub fn run() {
3737
use log::Level;
3838

39+
ruff_db::set_program_version(version()).unwrap();
40+
3941
// When the `console_error_panic_hook` feature is enabled, we can call the
4042
// `set_panic_hook` function at least once during initialization, and then
4143
// we will get better error messages if our code ever panics.

0 commit comments

Comments
 (0)