diff --git a/crates/ty/build.rs b/crates/ty/build.rs index bd2f8d1d30..98df7eae01 100644 --- a/crates/ty/build.rs +++ b/crates/ty/build.rs @@ -12,12 +12,39 @@ fn main() { .join("..") .join(".."); + version_info(&workspace_root); commit_info(&workspace_root); let target = std::env::var("TARGET").unwrap(); println!("cargo::rustc-env=RUST_HOST_TARGET={target}"); } +/// Retrieve the version from the `dist-workspace.toml` file and set `TY_VERSION`. +fn version_info(workspace_root: &Path) { + let dist_file = workspace_root.join("dist-workspace.toml"); + if !dist_file.exists() { + return; + } + + println!("cargo:rerun-if-changed={}", dist_file.display()); + + let dist_file = fs::read_to_string(dist_file); + if let Ok(dist_file) = dist_file { + let lines = dist_file.lines(); + for line in lines { + if line.starts_with("version =") { + let (_key, version) = line.split_once('=').unwrap(); + println!( + "cargo::rustc-env=TY_VERSION={}", + version.trim().trim_matches('"') + ); + break; + } + } + } +} + +/// 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"); diff --git a/crates/ty/src/version.rs b/crates/ty/src/version.rs index cc52949e0a..a851afc8c7 100644 --- a/crates/ty/src/version.rs +++ b/crates/ty/src/version.rs @@ -54,16 +54,20 @@ pub(crate) fn version() -> VersionInfo { last_tag: option_env_str!("TY_LAST_TAG"), }); - let version = commit_info - .as_ref() - .and_then(|info| { - info.last_tag.as_ref().map(|tag| { - tag.strip_prefix("v") - .map(std::string::ToString::to_string) - .unwrap_or(tag.clone()) + // The version is pulled from `dist-workspace.toml` and set by `build.rs` + let version = option_env_str!("TY_VERSION").unwrap_or_else(|| { + // If missing, using the last tag + commit_info + .as_ref() + .and_then(|info| { + info.last_tag.as_ref().map(|tag| { + tag.strip_prefix("v") + .map(std::string::ToString::to_string) + .unwrap_or(tag.clone()) + }) }) - }) - .unwrap_or("unknown".to_string()); + .unwrap_or("unknown".to_string()) + }); VersionInfo { version,