From 18ae0cc394ade9be8bc8a88b5920d9d3cc4e7ae8 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Thu, 20 Aug 2026 12:02:10 +0000 Subject: [PATCH] feat(build-time-versioning): go_build injects commit/build-time via ldflags Reads XCE_GIT_COMMIT/XCE_BUILD_TIME from the environment (empty-safe via [ -z ... ], not just unset-safe -- a classic ${VAR:-default} gotcha avoided), falls back to "unknown", and appends -ldflags "-X main.GitCommit=... -X main.BuildTime=..." to the go build invocation alongside the existing -gcflags. Confirmed via live testing: -X on a main package var that doesn't exist yet is silently ignored by the Go linker (not a build error), so this task has no real ordering dependency on tasks 4.4/4.5 (which add the consuming vars to apiserver/xcecli). Task 4.1/13 of build-time-versioning. Co-Authored-By: Claude Sonnet 5 --- .kiro/specs/build-time-versioning/tasks.md | 5 +++-- src/tools/go_build | 13 ++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.kiro/specs/build-time-versioning/tasks.md b/.kiro/specs/build-time-versioning/tasks.md index 77e828d0..656556ea 100644 --- a/.kiro/specs/build-time-versioning/tasks.md +++ b/.kiro/specs/build-time-versioning/tasks.md @@ -49,8 +49,9 @@ - _Depends: 3.1_ - [ ] 4. Core: Go binary version display -- [ ] 4.1 go_build script injects ldflags from environment - - `src/tools/go_build` reads `XCE_GIT_COMMIT`/`XCE_BUILD_TIME` from its environment (both optional, empty-safe) and appends `-ldflags "-X main.GitCommit=... -X main.BuildTime=..."` to its `go build` invocation, falling back to `unknown` for any unset value +- [x] 4.1 go_build script injects ldflags from environment + - `src/tools/go_build` reads `XCE_GIT_COMMIT`/`XCE_BUILD_TIME` via `[ -z ... ]` (correctly empty-safe, not just unset-safe) and appends `-ldflags "-X main.GitCommit=... -X main.BuildTime=..."` to its `go build` invocation, falling back to `unknown` + - Confirmed (research finding, independently reproduced by review): `-X` on a `main` package var that doesn't exist yet is silently ignored by the Go linker, not a build error -- this task has no real ordering dependency on 4.4/4.5 - Observable: invoking `go_build` with neither env var set still produces a working binary whose injected vars read `unknown` - _Requirements: 1.1, 1.2, 1.4, 4.1, 4.2_ - _Boundary: go_build script_ diff --git a/src/tools/go_build b/src/tools/go_build index c6c011b9..7edaaaed 100755 --- a/src/tools/go_build +++ b/src/tools/go_build @@ -99,4 +99,15 @@ done export GOPROXY=https://goproxy.cn -(cd $_sdir; go build -gcflags "all=-N -l" -o "$_dfile" "$_sfile") +_git_commit="$XCE_GIT_COMMIT" +_build_time="$XCE_BUILD_TIME" + +if [ -z "$_git_commit" ]; then + _git_commit=unknown +fi + +if [ -z "$_build_time" ]; then + _build_time=unknown +fi + +(cd $_sdir; go build -gcflags "all=-N -l" -ldflags "-X main.GitCommit=$_git_commit -X main.BuildTime=$_build_time" -o "$_dfile" "$_sfile")