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 <noreply@anthropic.com>
This commit is contained in:
Rongsong Shen 2026-08-20 12:02:10 +00:00
parent bc5c669b94
commit 18ae0cc394
No known key found for this signature in database
GPG key ID: 5BCDAAE0123E9FE2
2 changed files with 15 additions and 3 deletions

View file

@ -49,8 +49,9 @@
- _Depends: 3.1_ - _Depends: 3.1_
- [ ] 4. Core: Go binary version display - [ ] 4. Core: Go binary version display
- [ ] 4.1 go_build script injects ldflags from environment - [x] 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 - `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` - 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_ - _Requirements: 1.1, 1.2, 1.4, 4.1, 4.2_
- _Boundary: go_build script_ - _Boundary: go_build script_

View file

@ -99,4 +99,15 @@ done
export GOPROXY=https://goproxy.cn 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")