From 5168db69d964c7c949d644775315463f3bab9279 Mon Sep 17 00:00:00 2001 From: clawbot <35+clawbot@noreply.example.org> Date: Fri, 2 Oct 2026 03:21:51 +0200 Subject: [PATCH] Read the architecture at run time instead of a Buildarch ldflag (closes #259) The Makefile no longer passes the architecture to the linker. The Buildarch variable in main and the field and setter in globals are gone; the startup log line reports runtime.GOARCH as `arch`. CONVENTIONS.md drops Buildarch from its main, globals, logger and Makefile examples, as the sneak/prompts conventions do. Model: opus-5-5 --- CONVENTIONS.md | 32 ++++++++++++-------------------- Makefile | 3 +-- TODO.md | 5 +++++ cmd/upaasd/main.go | 6 ++---- internal/globals/globals.go | 25 +++++++------------------ internal/logger/logger.go | 3 ++- internal/logger/logger_test.go | 34 ++++++++++++++++++++++++++++++++++ 7 files changed, 63 insertions(+), 45 deletions(-) create mode 100644 internal/logger/logger_test.go diff --git a/CONVENTIONS.md b/CONVENTIONS.md index 53bc824..945a092 100644 --- a/CONVENTIONS.md +++ b/CONVENTIONS.md @@ -115,15 +115,13 @@ import ( ) var ( - Appname string = "CHANGEME" - Version string - Buildarch string + Appname string = "CHANGEME" + Version string ) func main() { globals.Appname = Appname globals.Version = Version - globals.Buildarch = Buildarch fx.New( fx.Provide( @@ -823,7 +821,7 @@ func (l *Logger) Identify() { l.log.Info("starting", "appname", l.params.Globals.Appname, "version", l.params.Globals.Version, - "buildarch", l.params.Globals.Buildarch, + "arch", runtime.GOARCH, ) } ``` @@ -943,23 +941,20 @@ import "go.uber.org/fx" // Package-level variables (set from main) var ( - Appname string - Version string - Buildarch string + Appname string + Version string ) // Struct for DI type Globals struct { - Appname string - Version string - Buildarch string + Appname string + Version string } func New(lc fx.Lifecycle) (*Globals, error) { n := &Globals{ - Appname: Appname, - Buildarch: Buildarch, - Version: Version, + Appname: Appname, + Version: Version, } return n, nil } @@ -970,15 +965,13 @@ func New(lc fx.Lifecycle) (*Globals, error) { ```go // cmd/httpd/main.go var ( - Appname string = "CHANGEME" // Default, overridden by build - Version string // Set at build time - Buildarch string // Set at build time + Appname string = "CHANGEME" // Default, overridden by build + Version string // Set at build time ) func main() { globals.Appname = Appname globals.Version = Version - globals.Buildarch = Buildarch // ... } ``` @@ -989,10 +982,9 @@ Use ldflags to inject version information at build time: ```makefile VERSION := $(shell git describe --tags --always) -BUILDARCH := $(shell go env GOARCH) build: - go build -ldflags "-X main.Version=$(VERSION) -X main.Buildarch=$(BUILDARCH)" ./cmd/httpd + go build -ldflags "-X main.Version=$(VERSION)" ./cmd/httpd ``` --- diff --git a/Makefile b/Makefile index 6a669f4..b945fee 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,7 @@ BINARY := upaasd VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev") -BUILDARCH := $(shell go env GOARCH) -LDFLAGS := -X main.Version=$(VERSION) -X main.Buildarch=$(BUILDARCH) +LDFLAGS := -X main.Version=$(VERSION) all: check build diff --git a/TODO.md b/TODO.md index bf60f7f..d6bca11 100644 --- a/TODO.md +++ b/TODO.md @@ -20,6 +20,11 @@ regress. # Completed Steps +- 2026-10-02: The build no longer passes the CPU architecture in: upaas reads it + from Go's `runtime.GOARCH` when it runs, and the startup log line reports it + as `arch`. `CONVENTIONS.md` follows the updated conventions in `sneak/prompts` + (#259). + - 2026-10-01: Built images are tagged `upaas-:`, git's short form of the commit built, instead of the deployment number. A redeploy of a commit gives its tag to the new image; the old one is kept while the app runs diff --git a/cmd/upaasd/main.go b/cmd/upaasd/main.go index 29a4c29..066fb05 100644 --- a/cmd/upaasd/main.go +++ b/cmd/upaasd/main.go @@ -25,15 +25,13 @@ import ( // Build-time variables injected by linker flags (-ldflags). // These must be exported package-level variables for the build system. var ( - Appname = "upaas" //nolint:gochecknoglobals // build-time variable - Version string //nolint:gochecknoglobals // build-time variable - Buildarch string //nolint:gochecknoglobals // build-time variable + Appname = "upaas" //nolint:gochecknoglobals // build-time variable + Version string //nolint:gochecknoglobals // build-time variable ) func main() { globals.SetAppname(Appname) globals.SetVersion(Version) - globals.SetBuildarch(Buildarch) fx.New( fx.Provide( diff --git a/internal/globals/globals.go b/internal/globals/globals.go index 8c11187..b38b447 100644 --- a/internal/globals/globals.go +++ b/internal/globals/globals.go @@ -12,17 +12,15 @@ import ( // //nolint:gochecknoglobals // Required for ldflags injection at build time var ( - mu sync.RWMutex - appname string - version string - buildarch string + mu sync.RWMutex + appname string + version string ) // Globals holds build-time variables for dependency injection. type Globals struct { - Appname string - Version string - Buildarch string + Appname string + Version string } // New creates a new Globals instance from package-level variables. @@ -31,9 +29,8 @@ func New(_ fx.Lifecycle) (*Globals, error) { defer mu.RUnlock() return &Globals{ - Appname: appname, - Version: version, - Buildarch: buildarch, + Appname: appname, + Version: version, }, nil } @@ -52,11 +49,3 @@ func SetVersion(ver string) { version = ver } - -// SetBuildarch sets the build architecture (used for testing and main init). -func SetBuildarch(arch string) { - mu.Lock() - defer mu.Unlock() - - buildarch = arch -} diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 4d671a6..9106b3f 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -4,6 +4,7 @@ package logger import ( "log/slog" "os" + "runtime" "go.uber.org/fx" @@ -81,6 +82,6 @@ func (l *Logger) Identify() { l.log.Info("starting", "appname", l.params.Globals.Appname, "version", l.params.Globals.Version, - "buildarch", l.params.Globals.Buildarch, + "arch", runtime.GOARCH, ) } diff --git a/internal/logger/logger_test.go b/internal/logger/logger_test.go new file mode 100644 index 0000000..9e90962 --- /dev/null +++ b/internal/logger/logger_test.go @@ -0,0 +1,34 @@ +package logger //nolint:testpackage // sets the unexported log and params fields + +import ( + "bytes" + "encoding/json" + "log/slog" + "runtime" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "sneak.berlin/go/upaas/internal/globals" +) + +func TestIdentifyLogsArchitectureFromRuntime(t *testing.T) { + t.Parallel() + + var buf bytes.Buffer + + l := &Logger{ + log: slog.New(slog.NewJSONHandler(&buf, nil)), + params: Params{ + Globals: &globals.Globals{Appname: "upaas-test", Version: "test"}, + }, + } + + l.Identify() + + var line map[string]any + + require.NoError(t, json.Unmarshal(buf.Bytes(), &line)) + assert.Equal(t, runtime.GOARCH, line["arch"]) +}