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
6 changes: 5 additions & 1 deletion pkgs/mcpx/Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkgs/mcpx/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
6 changes: 5 additions & 1 deletion pkgs/mcpx/cmd/mcpx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
67 changes: 47 additions & 20 deletions pkgs/mcpx/internal/buildcheck/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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)
}
}
24 changes: 14 additions & 10 deletions pkgs/mcpx/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,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";
Expand Down Expand Up @@ -83,10 +85,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"
Expand Down
Loading