From b118a09f9b437e69795b36f796058ef1eb8bb1ba Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Fri, 30 Oct 2026 00:17:36 -0500 Subject: [PATCH] feat(mcpx): single-source the version in pkgs/mcpx/VERSION (#1881 slice 2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Decision 0005 §3, slice 2 of queue task 3114: new pkgs/mcpx/VERSION (0.1.0) is the single source. package.nix derives version = lib.removeSuffix \n (builtins.readFile ./VERSION) for both the derivation attribute and the -X main.version= ldflags stamp. cmd/mcpx/main.go fallback and the Containerfile ARG VERSION default become the 0.0.0-dev sentinel, so an unstamped build announces itself instead of impersonating a release; the release workflow (slice 4/6) passes VERSION contents as the Containerfile build arg. internal/buildcheck TestVersionHasOneSourceOfTruth rewritten for the new scheme: VERSION is canonical, package.nix must derive + interpolate and must not contain the literal, both defaults must equal the sentinel. Laptop: buildcheck PASS, go build ./... PASS, package.nix parses. Note: the task resume brief paraphrased the main.go half as matching VERSION; the merged decision §3 explicitly makes the fallback a sentinel instead, and this follows the decision. --- pkgs/mcpx/Containerfile | 6 +- pkgs/mcpx/VERSION | 1 + pkgs/mcpx/cmd/mcpx/main.go | 6 +- pkgs/mcpx/internal/buildcheck/version_test.go | 67 +++++++++++++------ pkgs/mcpx/package.nix | 24 ++++--- 5 files changed, 72 insertions(+), 32 deletions(-) create mode 100644 pkgs/mcpx/VERSION diff --git a/pkgs/mcpx/Containerfile b/pkgs/mcpx/Containerfile index 58f64053c2..95fbfe485a 100644 --- a/pkgs/mcpx/Containerfile +++ b/pkgs/mcpx/Containerfile @@ -37,7 +37,11 @@ FROM ${GO_IMAGE} AS build # and the resolver stops honouring nsswitch.conf. Neither matters inside this # image; both matter to somebody. docs/container.md has the measurements. ARG CGO_ENABLED=1 -ARG VERSION=0.1.0 +# The version's single source is pkgs/mcpx/VERSION (decision 0005 §3); +# the release workflow passes its contents as --build-arg VERSION=... +# The default here is the dev sentinel, matching cmd/mcpx/main.go, so +# a plain `docker build` does not impersonate a release. +ARG VERSION=0.0.0-dev ENV CGO_ENABLED=${CGO_ENABLED} WORKDIR /src diff --git a/pkgs/mcpx/VERSION b/pkgs/mcpx/VERSION new file mode 100644 index 0000000000..6e8bf73aa5 --- /dev/null +++ b/pkgs/mcpx/VERSION @@ -0,0 +1 @@ +0.1.0 diff --git a/pkgs/mcpx/cmd/mcpx/main.go b/pkgs/mcpx/cmd/mcpx/main.go index a9a810d667..3713b3d7d9 100644 --- a/pkgs/mcpx/cmd/mcpx/main.go +++ b/pkgs/mcpx/cmd/mcpx/main.go @@ -17,7 +17,11 @@ import ( ) // version is overridden at build time with -ldflags "-X main.version=...". -var version = "0.1.0" +// The default is a sentinel, not a release number (decision 0005 §3): +// the release version's single source is pkgs/mcpx/VERSION, and a +// binary built without ldflags must announce that it is not a release +// instead of impersonating one. +var version = "0.0.0-dev" // splitList accepts comma or space separated values. func splitList(s string) []string { diff --git a/pkgs/mcpx/internal/buildcheck/version_test.go b/pkgs/mcpx/internal/buildcheck/version_test.go index 08aa9c6809..876c95abc6 100644 --- a/pkgs/mcpx/internal/buildcheck/version_test.go +++ b/pkgs/mcpx/internal/buildcheck/version_test.go @@ -9,30 +9,51 @@ import ( ) var ( - nixVersionLine = regexp.MustCompile(`(?m)^\s*version = "([^"]+)";\s*$`) - ldVersionStamp = regexp.MustCompile(`-X main\.version=([^"\s]+)`) - mainVersionVar = regexp.MustCompile(`(?m)^var version = "([^"]+)"`) + nixVersionDerivation = regexp.MustCompile(`version = lib\.removeSuffix "\\n" \(builtins\.readFile \./VERSION\);`) + ldVersionStamp = regexp.MustCompile(`-X main\.version=([^"\s]+)`) + mainVersionVar = regexp.MustCompile(`(?m)^var version = "([^"]+)"`) + containerVersionArg = regexp.MustCompile(`(?m)^ARG VERSION=(\S+)$`) ) -// TestVersionHasOneSourceOfTruth is the guard for #1895: the version literal -// in package.nix, the ldflags build stamp and the default in cmd/mcpx/main.go -// are three sites for one value, so a disagreement must fail the build rather -// than ship a binary whose --version is not the package's version. -func TestVersionHasOneSourceOfTruth(t *testing.T) { - nix := readPackageNix(t) +// devSentinel is the one version literal allowed outside VERSION +// (decision 0005 §3): a build that was not stamped announces itself +// instead of impersonating a release. +const devSentinel = "0.0.0-dev" - nixMatch := nixVersionLine.FindStringSubmatch(nix) - if nixMatch == nil { - t.Fatal("package.nix has no `version = \"...\";` line") +// TestVersionHasOneSourceOfTruth is the guard for #1895, updated for +// decision 0005 §3 (#1881 slice 2): pkgs/mcpx/VERSION is the version's +// single source. package.nix derives its version from that file and +// interpolates it into the ldflags stamp; cmd/mcpx/main.go's default +// and the Containerfile's ARG default are the 0.0.0-dev sentinel, so +// no second copy of the release literal exists to drift. +func TestVersionHasOneSourceOfTruth(t *testing.T) { + vb, err := os.ReadFile(filepath.Join(moduleRoot, "VERSION")) + if err != nil { + t.Fatalf("VERSION file (the version's single source, decision 0005 §3): %v", err) + } + version := strings.TrimSpace(string(vb)) + if version == "" || strings.ContainsAny(version, " \t\r\n") { + t.Fatalf("VERSION file content %q is not a single version token", string(vb)) + } + if version == devSentinel { + t.Fatalf("VERSION file holds the dev sentinel; it must name a release version") } - nixVersion := nixMatch[1] + nix := readPackageNix(t) + if !nixVersionDerivation.MatchString(nix) { + t.Error("package.nix does not derive its version from ./VERSION via lib.removeSuffix/builtins.readFile (decision 0005 §3)") + } stamps := ldVersionStamp.FindAllStringSubmatch(nix, -1) if len(stamps) != 1 { t.Fatalf("package.nix has %d `-X main.version=` stamps; want exactly one", len(stamps)) } if got := stamps[0][1]; got != "${version}" { - t.Errorf("package.nix ldflags stamp is %q; it must interpolate ${version} so the literal is written once (package.nix version = %q)", got, nixVersion) + t.Errorf("package.nix ldflags stamp is %q; it must interpolate ${version} so VERSION is written once", got) + } + // A hard-coded copy of the release literal in the derivation would + // reintroduce the drift this test exists to catch. + if strings.Contains(nix, `"`+version+`"`) { + t.Errorf("package.nix contains the version literal %q; VERSION is the only place it may be written", version) } mainSrc, err := os.ReadFile(filepath.Join(moduleRoot, "cmd", "mcpx", "main.go")) @@ -43,13 +64,19 @@ func TestVersionHasOneSourceOfTruth(t *testing.T) { if mainMatch == nil { t.Fatal("cmd/mcpx/main.go has no `var version = \"...\"` default") } - if mainMatch[1] != nixVersion { - t.Errorf("version disagreement: package.nix says %q, cmd/mcpx/main.go default says %q", nixVersion, mainMatch[1]) + if mainMatch[1] != devSentinel { + t.Errorf("cmd/mcpx/main.go default is %q; want the %q sentinel (decision 0005 §3) — release binaries are stamped from VERSION via ldflags", mainMatch[1], devSentinel) } - // A second hard-coded literal in the derivation would reintroduce the - // drift this test exists to catch. - if strings.Count(nix, `"`+nixVersion+`"`) != 1 { - t.Errorf("package.nix mentions %q more than once besides the version line; keep the literal in one place", nixVersion) + cf, err := os.ReadFile(filepath.Join(moduleRoot, "Containerfile")) + if err != nil { + t.Fatal(err) + } + cfMatch := containerVersionArg.FindStringSubmatch(string(cf)) + if cfMatch == nil { + t.Fatal("Containerfile has no `ARG VERSION=` line") + } + if cfMatch[1] != devSentinel { + t.Errorf("Containerfile ARG VERSION default is %q; want the %q sentinel — the release workflow passes VERSION's contents as a build arg (decision 0005 §3)", cfMatch[1], devSentinel) } } diff --git a/pkgs/mcpx/package.nix b/pkgs/mcpx/package.nix index 8584f17a90..3c7cdc6340 100644 --- a/pkgs/mcpx/package.nix +++ b/pkgs/mcpx/package.nix @@ -19,12 +19,14 @@ installShellFiles, }: let - # The mcpx version, written once here (#1895): the derivation attribute - # below and the ldflags build stamp both take this binding, because the - # argument to buildGo127Module is a non-recursive attribute set and its - # own attributes are not in scope for interpolation inside it. - # internal/buildcheck keeps cmd/mcpx/main.go default in agreement. - version = "0.1.0"; + # The mcpx version's single source is the VERSION file beside this + # one (decision 0005 §3, #1881; #1895's one-literal rule, moved): + # the derivation attribute below and the ldflags build stamp both + # take this binding, because the argument to buildGo127Module is a + # non-recursive attribute set and its own attributes are not in + # scope for interpolation inside it. internal/buildcheck guards + # the scheme. + version = lib.removeSuffix "\n" (builtins.readFile ./VERSION); in buildGo127Module { pname = "mcpx"; @@ -79,10 +81,12 @@ buildGo127Module { export GOFLAGS="$GOFLAGS -buildvcs=false" ''; - # The version is written once, above, and interpolated here, so the build - # stamp cannot silently disagree with `package.nix`'s version. The default in - # cmd/mcpx/main.go is the third site and is kept honest by - # internal/buildcheck's version agreement test (#1895). + # The version comes from VERSION (above) and is interpolated here, so + # the build stamp cannot silently disagree with the package version. + # The default in cmd/mcpx/main.go is deliberately NOT this version: + # it is the 0.0.0-dev sentinel (decision 0005 §3), so a binary built + # without ldflags announces that fact instead of impersonating a + # release. internal/buildcheck guards both halves of that scheme. ldflags = [ "-s" "-w"