(c28bc04) Report the version and the commit it was built from

worklane --version exited 2: the Cli derive set name and about but never
version, so clap generated no flag. Nothing could say what a given binary
was, which matters here because the opencode tools shell out to whatever
worklane is on PATH, so an agent can be driving a stale build with no way
to notice.

The crate version alone would not have answered it. Cargo.toml is pinned
at 0.1.0 and nobody bumps it per install, so a stale install and a fresh
build both claim the same thing. A build script stamps the short sha
alongside it, giving "worklane 0.1.0 (df97b06)", and falls back to
"unknown" where there is no git directory, as when installing from a
published crate.

propagate_version lets subcommands answer too. This overrides the ticket's
own plan step, which said to skip the sha if it needed a build script.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ansho Rei
2026-08-20 14:48:37 +02:00
co-authored by Claude Opus 5
parent df97b06275
commit 32a43b5201
3 changed files with 95 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
//! Stamps the build with the commit it came from.
//!
//! The crate version alone cannot tell a stale install from a fresh build: it is
//! pinned at 0.1.0 and nobody bumps it per install, so both binaries claim the
//! same thing. The short sha is what makes `worklane --version` able to answer
//! "is this the binary with the fix in it".
//!
//! Absent a git directory, as when installing from a published crate, the sha is
//! "unknown" rather than a build failure.
use std::process::Command;
fn main() {
// Without these, an incremental build keeps stamping the sha it first saw.
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/refs");
println!("cargo:rustc-env=WORKLANE_GIT_SHA={}", git_sha());
}
fn git_sha() -> String {
let output = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output();
let sha = match output {
Ok(output) if output.status.success() => String::from_utf8_lossy(&output.stdout)
.trim()
.to_string(),
_ => String::new(),
};
if sha.is_empty() {
"unknown".to_string()
} else {
sha
}
}
+9
View File
@@ -1,8 +1,17 @@
use clap::Parser;
/// What `--version` prints: the crate version and the commit it was built from.
///
/// The sha is the part that matters. Two binaries built weeks apart both call
/// themselves 0.1.0, so without it there is no way to tell whether the copy on
/// PATH is the one carrying a given fix.
const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("WORKLANE_GIT_SHA"), ")");
#[derive(Parser)]
#[command(name = "worklane")]
#[command(about = "A CLI tool for managing project status")]
#[command(version = VERSION)]
#[command(propagate_version = true)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
+48
View File
@@ -0,0 +1,48 @@
use assert_cmd::Command;
const BIN_NAME: &str = env!("CARGO_BIN_EXE_worklane");
fn version_output(arg: &str) -> String {
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
let assert = cmd.arg(arg).assert().success();
String::from_utf8(assert.get_output().stdout.clone()).unwrap()
}
#[test]
fn long_flag_reports_the_crate_version() {
let stdout = version_output("--version");
assert!(stdout.starts_with("worklane "), "was {stdout:?}");
assert!(stdout.contains(env!("CARGO_PKG_VERSION")), "was {stdout:?}");
}
#[test]
fn short_flag_matches_the_long_one() {
assert_eq!(version_output("-V"), version_output("--version"));
}
/// The crate version alone cannot tell two builds apart, so the build stamp is
/// the load-bearing half.
#[test]
fn the_output_identifies_the_build() {
let stdout = version_output("--version");
let stamp = stdout
.split_once('(')
.and_then(|(_, rest)| rest.split_once(')'))
.map(|(stamp, _)| stamp.to_string())
.unwrap_or_default();
assert!(!stamp.is_empty(), "no build stamp in {stdout:?}");
// "unknown" is the documented fallback when there is no git directory.
assert!(
stamp == "unknown" || stamp.chars().all(|c| c.is_ascii_hexdigit()),
"stamp was {stamp:?}"
);
}
#[test]
fn subcommands_accept_the_flag_too() {
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
let assert = cmd.args(["mv", "--version"]).assert().success();
let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
assert!(stdout.contains(env!("CARGO_PKG_VERSION")), "was {stdout:?}");
}