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
64 changes: 64 additions & 0 deletions pkgs/fleet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,70 @@ default.nix the four variant packages (see below)
smoke.sh per-variant smoke suite (post-build)
```

## Versioning — derived per-app versions (#8566; policy #5790)

A fleet binary's version is **derived, never hand-maintained**. The
workspace `version = "0.1.0"` in `Cargo.toml` is only the release-line
base cargo requires; the version a deployed binary reports — the one
`fleet version`, `fleet capabilities` (JSON `build.derived_version`)
and every `--version` surface print as `Derived version:` — is baked
at build time by `crates/fleet/build.rs` from the derivation in
`crates/fleet/src/derived_version.rs`, through the same channel that
reports the source rev (`FLEET_SOURCE_REV`): a packaged build with no
git history receives it as `$FLEET_DERIVED_VERSION`, computed by the
release tooling where the history exists.

Derivation (nearest `<app>/vX.Y.Z` tag on the build commit's history;
the app is this workspace's short name, `fleet`):

| build state | reported version |
| --- | --- |
| exactly on the tag | `X.Y.Z` |
| on `main`, N commits after the tag | `X.Y.(Z+1)-dev.N+g<shorthash>` |
| on a feature branch, N commits after | `X.Y.(Z+1)-dev.N+g<shorthash>.<branch-slug>` |
| no `<app>/v*` tag yet | `0.1.0-dev.N+g<shorthash>` (bare `0.1.0-dev` if no history is available) |

The `+g<hash>` suffix is what guarantees no two distinct commits ever
report the same version string; the patch-bump + `-dev.N` form is what
makes the string sort after the tag it descends from and before its
own release.

### Tag and release procedure (ruled on #5790, 2026-11-06)

1. **Per-app tags.** A release is cut by tagging the exact tested
commit `<short-app-name>/vX.Y.Z` — for this workspace,
`fleet/vX.Y.Z`. The short app name is the tag prefix for ALL apps
(one policy, no per-app mixing); app names used in tags and
deploys must be unique across apps — if a short name would
collide, fix the app name, not the policy.
2. **First tag and 0.x discipline.** Each app's first tag is
`0.1.0`. Versions stay in 0.x: minor for feature releases, patch
for fixes, and **no major-version bumps** until a separate 1.0
decision.
3. **Releases are tag builds only.** Build the release artifact
from the tagged commit and verify the artifact itself reports
the clean version: `./result/bin/fleet version` must show
`Derived version: X.Y.Z`. A packaged (nix) release build takes
the version through the package's `derivedVersion` argument —
the build source carries no `.git`, so the release tooling
computes the version where the history exists and passes it in;
a packaged build without one reports the rev-derived prerelease
form `0.1.0-dev+g<rev>`, never a clean release string. Builds
deployed from `main` are
**prereleases** (their `-dev.N` strings say so) — deploying one
never makes it a release.
4. **Repo tags.** At every release point (not every commit, not
every deploy), also cut `repo/v<YYYYMMDD>.<minor>.<patch>` —
the date is the major segment; within a date, a release point
including any app feature increments minor and resets patch, a
fix-only or repeat point increments patch; minor/patch may
reset to `0.0` on a new date. A repo **prerelease** tag on a
deploy from main is also fine and does not make the deploy a
release.
5. **Deploy truth.** What a target runs is read from the deployed
binary's own `Derived version:` line, never from paperwork
(that read is the #5760 deployed-state check's input).

## Variants

Capability surface = compiled features ∩ invocation-name profile ∩
Expand Down
194 changes: 178 additions & 16 deletions pkgs/fleet/crates/fleet/build.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,192 @@
//! Bake the source revision into the binary for `fleet version`.
//! Bake the source revision and the derived version into the
//! binary for `fleet version`.
//!
//! Resolution: `$FLEET_SOURCE_REV` (the nix build passes the flake's
//! rev) → `git rev-parse` in the repo root (developer builds) →
//! "unknown". The nix sandbox has no `.git`, so without the env var a
//! packaged build honestly reports "unknown" rather than inventing one.
//! Source rev resolution: `$FLEET_SOURCE_REV` (the nix build passes
//! the flake's rev) → `git rev-parse` in the repo root (developer
//! builds) → "unknown". The nix sandbox has no `.git`, so without
//! the env var a packaged build honestly reports "unknown" rather
//! than inventing one.
//!
//! Derived version resolution (#8566; policy #5790): the version is
//! derived, never hand-maintained —
//! `$FLEET_DERIVED_VERSION` (release tooling / packaged builds,
//! computed where the git history exists, exactly the way
//! `$FLEET_SOURCE_REV` already flows) → `git describe --tags
//! --match '<app>/v*'` transformed by the shared derivation in
//! `src/derived_version.rs` → the pre-first-tag fallback
//! `0.1.0-dev` for a build with no history at all. See the
//! "Versioning" section of pkgs/fleet/README.md for the tag and
//! release procedure this implements.

use std::process::Command;

#[path = "src/derived_version.rs"]
mod derived_version;

use derived_version::{ReleaseVersion, branch_class, derive, parse_app_tag, parse_describe};

/// The app's short name — its tag prefix and deploy name (#5790:
/// one short-name policy for all apps).
const APP_NAME: &str = "fleet";

fn git(root: &std::path::Path, args: &[&str]) -> Option<String> {
Command::new("git")
.args(args)
.current_dir(root)
.output()
.ok()
.filter(|o| o.status.success())
.and_then(|o| String::from_utf8(o.stdout).ok())
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
}

/// Fallback tag selection (PR #8630 Codex finding): `git describe`
/// considers every tag matching `fleet/v*`, including malformed
/// ones (`fleet/v0.4.3-rc.1`, `fleet/v01.2.3`) that parse_describe
/// rightly rejects — and a rejected NEAREST tag must not hide an
/// older valid release tag behind it. Scan the reachable tags,
/// keep only the ones that validate as `<app>/vX.Y.Z`, and pick
/// the nearest (smallest commit distance; ties go to the highest
/// version).
fn from_validated_tags(root: &std::path::Path, branch: Option<&str>) -> Option<String> {
let tags = git(
root,
&[
"tag",
"--list",
&format!("{APP_NAME}/v*"),
"--merged",
"HEAD",
],
)?;
let hash = git(root, &["rev-parse", "--short=7", "HEAD"]);
let mut best: Option<(u64, ReleaseVersion)> = None;
for line in tags.lines() {
let tag = line.trim();
let Some(version) = parse_app_tag(APP_NAME, tag) else {
continue;
};
let Some(distance) = git(root, &["rev-list", "--count", &format!("{tag}..HEAD")])
.and_then(|s| s.parse::<u64>().ok())
else {
continue;
};
let wins = match best {
None => true,
Some((bd, bv)) => {
distance < bd
|| (distance == bd
&& (version.major, version.minor, version.patch)
> (bv.major, bv.minor, bv.patch))
}
};
if wins {
best = Some((distance, version));
}
}
let (distance, version) = best?;
let class = branch_class(branch, distance == 0);
Some(derive(Some(version), distance, hash.as_deref(), &class))
}

fn derived_version(root: &std::path::Path) -> String {
if let Some(v) = std::env::var("FLEET_DERIVED_VERSION")
.ok()
.filter(|s| !s.is_empty())
{
return v;
Comment thread
dezren39 marked this conversation as resolved.
Resolved
}
let branch = git(root, &["branch", "--show-current"]);
if let Some(described) = git(
root,
&[
"describe",
"--tags",
"--long",
"--match",
&format!("{APP_NAME}/v*"),
],
)
.and_then(|out| parse_describe(APP_NAME, &out))
Comment thread
dezren39 marked this conversation as resolved.
Resolved
{
let class = branch_class(branch.as_deref(), described.distance == 0);
return derive(
Some(described.tag),
described.distance,
described.hash.as_deref(),
&class,
);
}
if let Some(v) = from_validated_tags(root, branch.as_deref()) {
return v;
}
// No `<app>/v*` tag yet: the 0.1.0-dev fallback family, with the
// repo-wide commit count as the (monotonic) distance and the
// HEAD hash as the uniqueness suffix, when git can prove them.
let hash = git(root, &["rev-parse", "--short=7", "HEAD"]);
let distance = git(root, &["rev-list", "--count", "HEAD"])
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(0);
let class = branch_class(branch.as_deref(), false);
derive(None, distance, hash.as_deref(), &class)
}

/// Register the git state the bake reads as build-script inputs
/// (PR #8630 Codex finding): without this, cutting a tag on an
/// already-built tree changes no env var and no source file, and
/// cargo keeps the stale baked version. Tag refs live in the
/// common git dir (shared across worktrees); HEAD is per-git-dir.
fn rerun_on_git_refs(root: &std::path::Path) {
let resolve = |args: &[&str]| -> Option<std::path::PathBuf> {
let dir = git(root, args)?;
let base = if std::path::Path::new(&dir).is_absolute() {
std::path::PathBuf::from(dir)
} else {
root.join(dir)
};
Some(base)
};
let mut watched: Vec<std::path::PathBuf> = Vec::new();
if let Some(common) = resolve(&["rev-parse", "--git-common-dir"]) {
watched.push(common.join("refs/tags"));
watched.push(common.join("refs/heads"));
watched.push(common.join("packed-refs"));
// The current branch's own ref (PR #8630 Codex round 2):
// on an attached HEAD, an ordinary commit updates the
// loose branch ref under the COMMON dir — HEAD itself is
// just the symref naming it and never changes content —
// so watch the resolved ref as well as its directory
// (the directory watch covers its first loose creation
// when it previously lived only in packed-refs).
if let Some(branch_ref) = git(root, &["symbolic-ref", "-q", "HEAD"]) {
watched.push(common.join(branch_ref));
}
}
if let Some(gitdir) = resolve(&["rev-parse", "--git-dir"]) {
watched.push(gitdir.join("HEAD"));
}
for path in watched {
if path.exists() {
println!("cargo:rerun-if-changed={}", path.display());
}
}
}

fn main() {
println!("cargo:rerun-if-env-changed=FLEET_SOURCE_REV");
println!("cargo:rerun-if-env-changed=FLEET_DERIVED_VERSION");
Comment thread
dezren39 marked this conversation as resolved.
Resolved
let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
let root = std::path::Path::new(&manifest).join("../../..");
rerun_on_git_refs(&root);
let rev = std::env::var("FLEET_SOURCE_REV")
.ok()
.filter(|s| !s.is_empty())
.unwrap_or_else(|| {
let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
let root = std::path::Path::new(&manifest).join("../../..");
Command::new("git")
.args(["rev-parse", "--short=12", "HEAD"])
.current_dir(root)
.output()
.ok()
.filter(|o| o.status.success())
.and_then(|o| String::from_utf8(o.stdout).ok())
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
git(&root, &["rev-parse", "--short=12", "HEAD"])
.unwrap_or_else(|| "unknown".to_string())
});
println!("cargo:rustc-env=FLEET_SOURCE_REV={rev}");
let derived = derived_version(&root);
println!("cargo:rustc-env=FLEET_DERIVED_VERSION={derived}");
}
Loading
Loading