Skip to content
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
1 change: 1 addition & 0 deletions crates/ruff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub fn run(
}: Args,
) -> Result<ExitStatus> {
{
ruff_db::set_program_version(crate::version::version().to_string()).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

Why is this required? Isn't this the entrypoint for the Ruff CLI?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is more a precaution that we don't introduce a panic in ruff when we add a new path accessing version (Both the diagnostics and ruff analzye use the ruff_db crate).

let default_panic_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
#[expect(clippy::print_stderr)]
Expand Down
15 changes: 15 additions & 0 deletions crates/ruff_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ pub use web_time::{Instant, SystemTime, SystemTimeError};
pub type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;
pub type FxDashSet<K> = dashmap::DashSet<K, BuildHasherDefault<FxHasher>>;

static VERSION: std::sync::OnceLock<String> = std::sync::OnceLock::new();

/// Returns the version of the executing program if set.
pub fn program_version() -> Option<&'static str> {
VERSION.get().map(|version| version.as_str())
}

/// Sets the version of the executing program.
///
/// ## Errors
/// If the version has already been initialized (can only be set once).
pub fn set_program_version(version: String) -> Result<(), String> {
VERSION.set(version)
}

/// Most basic database that gives access to files, the host system, source code, and parsed AST.
#[salsa::db]
pub trait Db: salsa::Database {
Expand Down
35 changes: 22 additions & 13 deletions crates/ty/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ use std::{
fn main() {
// The workspace root directory is not available without walking up the tree
// https://github.com/rust-lang/cargo/issues/3946
let workspace_root = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("..")
let ruff_workspace_root = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("..")
.join("..");
let ty_workspace_root = ruff_workspace_root.join("..");

version_info(&ty_workspace_root);

// If not in a git repository, do not attempt to retrieve commit information
let git_dir = ty_workspace_root.join(".git");
if git_dir.exists() {
commit_info(&git_dir, &ty_workspace_root, false);
} else {
// Try if we're inside the ruff repository and, if so, use that commit hash.
let git_dir = ruff_workspace_root.join(".git");

version_info(&workspace_root);
commit_info(&workspace_root);
if git_dir.exists() {
commit_info(&git_dir, &ruff_workspace_root, true);
}
}

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

/// Retrieve commit information from the Git repository.
fn commit_info(workspace_root: &Path) {
// If not in a git repository, do not attempt to retrieve commit information
let git_dir = workspace_root.join(".git");
if !git_dir.exists() {
return;
}

if let Some(git_head_path) = git_head(&git_dir) {
fn commit_info(git_dir: &Path, workspace_root: &Path, is_ruff: bool) {
if let Some(git_head_path) = git_head(git_dir) {
println!("cargo:rerun-if-changed={}", git_head_path.display());

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

println!("cargo::rustc-env=TY_LAST_TAG={last_tag}");
println!(
"cargo::rustc-env=TY_LAST_TAG={ruff}{last_tag}",
ruff = if is_ruff { "ruff/" } else { "" }
);

// If this is the tagged commit, this component will be missing
println!(
Expand Down
1 change: 1 addition & 0 deletions crates/ty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use ty_server::run_server;

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

let args = wild::args_os();
let args = argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX)
Expand Down
7 changes: 7 additions & 0 deletions crates/ty_project/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,13 @@ where
arch = std::env::consts::ARCH
),
));
if let Some(version) = ruff_db::program_version() {
diagnostic.sub(SubDiagnostic::new(
Severity::Info,
format!("Version: {version}"),
));
}

diagnostic.sub(SubDiagnostic::new(
Severity::Info,
format!(
Expand Down
4 changes: 0 additions & 4 deletions crates/ty_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ pub(crate) const DIAGNOSTIC_NAME: &str = "ty";
/// result type is needed.
pub(crate) type Result<T> = anyhow::Result<T>;

pub(crate) fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}

pub fn run_server() -> anyhow::Result<()> {
let four = NonZeroUsize::new(4).unwrap();

Expand Down
12 changes: 6 additions & 6 deletions crates/ty_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ impl Server {
let position_encoding = Self::find_best_position_encoding(&client_capabilities);
let server_capabilities = Self::server_capabilities(position_encoding);

let connection = connection.initialize_finish(
id,
&server_capabilities,
crate::SERVER_NAME,
crate::version(),
)?;
let version = ruff_db::program_version().unwrap_or("Unknown");

let connection =
connection.initialize_finish(id, &server_capabilities, crate::SERVER_NAME, version)?;

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

tracing::debug!("Version: {version}");

let mut workspace_for_url = |url: Url| {
let Some(workspace_settings) = workspace_options.as_mut() else {
return (url, ClientOptions::default());
Expand Down
2 changes: 2 additions & 0 deletions crates/ty_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub fn version() -> String {
pub fn run() {
use log::Level;

ruff_db::set_program_version(version()).unwrap();

// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
Expand Down
Loading