Skip to content

Commit 127d8c9

Browse files
committed
cmd: Support setting version string via ldflags
When xcaddy is built directly from source (e.g. distro packaging), Go's debug.ReadBuildInfo() reports the main module version as "(devel)" rather than the actual release version. The upstream build process works around this by building from a separate wrapper module that imports xcaddy as a versioned dependency, but packagers typically build directly from the source tarball. Add a CustomVersion variable that can be set via -ldflags -X at build time, allowing packagers to inject the correct version string.
1 parent 14e0e39 commit 127d8c9

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

cmd/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ import (
3333
"github.com/caddyserver/xcaddy/internal/utils"
3434
)
3535

36+
// CustomVersion is an optional string that overrides xcaddy's
37+
// reported version. It can be helpful when downstream packagers
38+
// need to manually set xcaddy's version, since building as the
39+
// main module causes debug.ReadBuildInfo() to report "(devel)".
40+
//
41+
// Set this variable during `go build` with `-ldflags`:
42+
//
43+
// -ldflags '-X github.com/caddyserver/xcaddy/cmd.CustomVersion=v0.4.4'
44+
//
45+
// for example.
46+
var CustomVersion string
47+
3648
var (
3749
caddyVersion = os.Getenv("CADDY_VERSION")
3850
raceDetector = os.Getenv("XCADDY_RACE_DETECTOR") == "1"
@@ -207,6 +219,9 @@ func splitWith(arg string) (module, version, replace string, err error) {
207219

208220
// xcaddyVersion returns a detailed version string, if available.
209221
func xcaddyVersion() string {
222+
if CustomVersion != "" {
223+
return CustomVersion
224+
}
210225
mod := goModule()
211226
ver := mod.Version
212227
if mod.Sum != "" {

0 commit comments

Comments
 (0)