From 60781dfaa0123e369aecac0b8b8e0c0899255f45 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Mon, 15 Jun 2026 20:16:30 -0500 Subject: [PATCH 1/3] Add crate version and commit hash to ldk-server We will now log the version number and commit hash when starting up. Also will give the full commit hash when doing ldk-server --version to better help debugging things. --- ldk-server/build.rs | 58 +++++++++++++++++++++++++++++++++++ ldk-server/src/main.rs | 3 +- ldk-server/src/util/config.rs | 2 +- 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 ldk-server/build.rs diff --git a/ldk-server/build.rs b/ldk-server/build.rs new file mode 100644 index 00000000..ab31d8dd --- /dev/null +++ b/ldk-server/build.rs @@ -0,0 +1,58 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +use std::env; +use std::path::Path; +use std::process::Command; + +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-env-changed=GIT_HASH"); + + // Re-run the build script whenever the checked-out commit changes so the + // embedded hash never goes stale. HEAD lives in the (possibly worktree-local) + // git dir, while branch refs and packed-refs live in the common git dir. + if let Some(git_dir) = git_output(&["rev-parse", "--git-dir"]) { + watch_if_exists(&Path::new(&git_dir).join("HEAD")); + } + if let Some(common_dir) = git_output(&["rev-parse", "--git-common-dir"]) { + let common_dir = Path::new(&common_dir); + watch_if_exists(&common_dir.join("packed-refs")); + // If HEAD points at a ref, watch that ref file too (it changes on commit). + if let Some(ref_path) = git_output(&["symbolic-ref", "-q", "HEAD"]) { + watch_if_exists(&common_dir.join(ref_path)); + } + } + + if env::var("GIT_HASH").is_err() { + let git_hash = git_output(&["rev-parse", "HEAD"]).unwrap_or_else(|| "unknown".to_string()); + println!("cargo:rustc-env=GIT_HASH={git_hash}"); + } +} + +/// Runs `git` with the given args, returning the trimmed stdout on success or +/// `None` if git is unavailable, exits non-zero, or produces no output. +fn git_output(args: &[&str]) -> Option { + let output = Command::new("git").args(args).output().ok()?; + if !output.status.success() { + return None; + } + let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string(); + if stdout.is_empty() { + None + } else { + Some(stdout) + } +} + +fn watch_if_exists(path: &Path) { + if path.exists() { + println!("cargo:rerun-if-changed={}", path.display()); + } +} diff --git a/ldk-server/src/main.rs b/ldk-server/src/main.rs index 1d12cd21..6d2dc9a6 100644 --- a/ldk-server/src/main.rs +++ b/ldk-server/src/main.rs @@ -58,6 +58,7 @@ use crate::util::tls::get_or_generate_tls_config; use crate::util::{systemd, write_new}; const API_KEY_FILE: &str = "api_key"; +const FULL_VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("GIT_HASH"), ")"); pub fn get_default_data_dir() -> Option { #[cfg(target_os = "macos")] @@ -241,7 +242,7 @@ fn main() { let (event_sender, _) = broadcast::channel::(1024); let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false); - info!("Starting up..."); + info!("Starting ldk-server version {FULL_VERSION}"); match node.start() { Ok(()) => {}, Err(e) => { diff --git a/ldk-server/src/util/config.rs b/ldk-server/src/util/config.rs index 0bf0d115..1a342fe1 100644 --- a/ldk-server/src/util/config.rs +++ b/ldk-server/src/util/config.rs @@ -742,7 +742,7 @@ impl TryFrom<&LSPSClientTomlConfig> for LSPSClientConfig { #[derive(Parser, Debug)] #[command( - version, + version = crate::FULL_VERSION, about = "LDK Server Configuration", long_about = None, override_usage = "ldk-server [config_path]" From 5d6e9066c211e6ad17c87e5174e5efb553ad0d54 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Mon, 15 Jun 2026 20:16:30 -0500 Subject: [PATCH 2/3] Add commit hash to ldk-server-cli --version --- ldk-server-cli/build.rs | 58 ++++++++++++++++++++++++++++++++++++++ ldk-server-cli/src/main.rs | 4 ++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 ldk-server-cli/build.rs diff --git a/ldk-server-cli/build.rs b/ldk-server-cli/build.rs new file mode 100644 index 00000000..ab31d8dd --- /dev/null +++ b/ldk-server-cli/build.rs @@ -0,0 +1,58 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +use std::env; +use std::path::Path; +use std::process::Command; + +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-env-changed=GIT_HASH"); + + // Re-run the build script whenever the checked-out commit changes so the + // embedded hash never goes stale. HEAD lives in the (possibly worktree-local) + // git dir, while branch refs and packed-refs live in the common git dir. + if let Some(git_dir) = git_output(&["rev-parse", "--git-dir"]) { + watch_if_exists(&Path::new(&git_dir).join("HEAD")); + } + if let Some(common_dir) = git_output(&["rev-parse", "--git-common-dir"]) { + let common_dir = Path::new(&common_dir); + watch_if_exists(&common_dir.join("packed-refs")); + // If HEAD points at a ref, watch that ref file too (it changes on commit). + if let Some(ref_path) = git_output(&["symbolic-ref", "-q", "HEAD"]) { + watch_if_exists(&common_dir.join(ref_path)); + } + } + + if env::var("GIT_HASH").is_err() { + let git_hash = git_output(&["rev-parse", "HEAD"]).unwrap_or_else(|| "unknown".to_string()); + println!("cargo:rustc-env=GIT_HASH={git_hash}"); + } +} + +/// Runs `git` with the given args, returning the trimmed stdout on success or +/// `None` if git is unavailable, exits non-zero, or produces no output. +fn git_output(args: &[&str]) -> Option { + let output = Command::new("git").args(args).output().ok()?; + if !output.status.success() { + return None; + } + let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string(); + if stdout.is_empty() { + None + } else { + Some(stdout) + } +} + +fn watch_if_exists(path: &Path) { + if path.exists() { + println!("cargo:rerun-if-changed={}", path.display()); + } +} diff --git a/ldk-server-cli/src/main.rs b/ldk-server-cli/src/main.rs index 3aa6f1c8..918f57a3 100644 --- a/ldk-server-cli/src/main.rs +++ b/ldk-server-cli/src/main.rs @@ -60,6 +60,8 @@ use types::{ mod types; +const FULL_VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("GIT_HASH"), ")"); + const DEFAULT_DIR: &str = if cfg!(target_os = "macos") { "~/Library/Application Support/ldk-server" } else if cfg!(target_os = "windows") { @@ -71,7 +73,7 @@ const DEFAULT_DIR: &str = if cfg!(target_os = "macos") { #[derive(Parser, Debug)] #[command( name = "ldk-server-cli", - version, + version = FULL_VERSION, about = "CLI for interacting with an LDK Server node", override_usage = "ldk-server-cli [OPTIONS] " )] From 8e8e2cadcfc4835703cf80c4db0c0a86749a001a Mon Sep 17 00:00:00 2001 From: Leo Nash Date: Thu, 18 Jun 2026 23:11:54 +0000 Subject: [PATCH 3/3] Fix build git hash refresh Prefer the repository HEAD over any inherited GIT_HASH so exported shell values cannot stale the compiled version output. Register git metadata paths with Cargo even when they do not yet exist, so builds rerun when packed refs or branch refs are created after the first build. AI-Assisted-By: OpenAI Codex --- ldk-server-cli/build.rs | 22 +++++++++++----------- ldk-server/build.rs | 22 +++++++++++----------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/ldk-server-cli/build.rs b/ldk-server-cli/build.rs index ab31d8dd..935f95ba 100644 --- a/ldk-server-cli/build.rs +++ b/ldk-server-cli/build.rs @@ -19,21 +19,23 @@ fn main() { // embedded hash never goes stale. HEAD lives in the (possibly worktree-local) // git dir, while branch refs and packed-refs live in the common git dir. if let Some(git_dir) = git_output(&["rev-parse", "--git-dir"]) { - watch_if_exists(&Path::new(&git_dir).join("HEAD")); + watch_path(&Path::new(&git_dir).join("HEAD")); } if let Some(common_dir) = git_output(&["rev-parse", "--git-common-dir"]) { let common_dir = Path::new(&common_dir); - watch_if_exists(&common_dir.join("packed-refs")); + watch_path(&common_dir.join("packed-refs")); // If HEAD points at a ref, watch that ref file too (it changes on commit). + // Watch it even when it does not exist yet: packed refs become loose + // files on the next commit, and Cargo can detect that creation. if let Some(ref_path) = git_output(&["symbolic-ref", "-q", "HEAD"]) { - watch_if_exists(&common_dir.join(ref_path)); + watch_path(&common_dir.join(ref_path)); } } - if env::var("GIT_HASH").is_err() { - let git_hash = git_output(&["rev-parse", "HEAD"]).unwrap_or_else(|| "unknown".to_string()); - println!("cargo:rustc-env=GIT_HASH={git_hash}"); - } + let git_hash = git_output(&["rev-parse", "HEAD"]) + .or_else(|| env::var("GIT_HASH").ok()) + .unwrap_or_else(|| "unknown".to_string()); + println!("cargo:rustc-env=GIT_HASH={git_hash}"); } /// Runs `git` with the given args, returning the trimmed stdout on success or @@ -51,8 +53,6 @@ fn git_output(args: &[&str]) -> Option { } } -fn watch_if_exists(path: &Path) { - if path.exists() { - println!("cargo:rerun-if-changed={}", path.display()); - } +fn watch_path(path: &Path) { + println!("cargo:rerun-if-changed={}", path.display()); } diff --git a/ldk-server/build.rs b/ldk-server/build.rs index ab31d8dd..935f95ba 100644 --- a/ldk-server/build.rs +++ b/ldk-server/build.rs @@ -19,21 +19,23 @@ fn main() { // embedded hash never goes stale. HEAD lives in the (possibly worktree-local) // git dir, while branch refs and packed-refs live in the common git dir. if let Some(git_dir) = git_output(&["rev-parse", "--git-dir"]) { - watch_if_exists(&Path::new(&git_dir).join("HEAD")); + watch_path(&Path::new(&git_dir).join("HEAD")); } if let Some(common_dir) = git_output(&["rev-parse", "--git-common-dir"]) { let common_dir = Path::new(&common_dir); - watch_if_exists(&common_dir.join("packed-refs")); + watch_path(&common_dir.join("packed-refs")); // If HEAD points at a ref, watch that ref file too (it changes on commit). + // Watch it even when it does not exist yet: packed refs become loose + // files on the next commit, and Cargo can detect that creation. if let Some(ref_path) = git_output(&["symbolic-ref", "-q", "HEAD"]) { - watch_if_exists(&common_dir.join(ref_path)); + watch_path(&common_dir.join(ref_path)); } } - if env::var("GIT_HASH").is_err() { - let git_hash = git_output(&["rev-parse", "HEAD"]).unwrap_or_else(|| "unknown".to_string()); - println!("cargo:rustc-env=GIT_HASH={git_hash}"); - } + let git_hash = git_output(&["rev-parse", "HEAD"]) + .or_else(|| env::var("GIT_HASH").ok()) + .unwrap_or_else(|| "unknown".to_string()); + println!("cargo:rustc-env=GIT_HASH={git_hash}"); } /// Runs `git` with the given args, returning the trimmed stdout on success or @@ -51,8 +53,6 @@ fn git_output(args: &[&str]) -> Option { } } -fn watch_if_exists(path: &Path) { - if path.exists() { - println!("cargo:rerun-if-changed={}", path.display()); - } +fn watch_path(path: &Path) { + println!("cargo:rerun-if-changed={}", path.display()); }