summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Vogel <lukedirtwalker@gmail.com>2022-12-19 22:23:45 +0100
committerGitHub <noreply@github.com>2022-12-19 14:23:45 -0700
commitc3b5b1811c119b896c5f70e3275ca835e7df5460 (patch)
tree9e2742745c689fc175018c450c000dfb3d54ef75
parent4fe5e64e461a9c22a8f2a275a1c715f25a6ca381 (diff)
cmd: Avoid panic when printing version without build info (#5210)
* version: don't panic if read build info doesn't work If `debug.ReadBuildInfo()` doesn't return the build information we should not try to access it. Especially if users only want to build with the `CustomVersion` we should not assume access to `debug.ReadBuildInfo()`. The build environment where this isn't available for me is when building with bazel. * exit early
-rw-r--r--caddy.go22
1 files changed, 15 insertions, 7 deletions
diff --git a/caddy.go b/caddy.go
index 1ecad94..5d0152a 100644
--- a/caddy.go
+++ b/caddy.go
@@ -864,13 +864,21 @@ func Version() (simple, full string) {
// bi.Main... hopefully.
var module *debug.Module
bi, ok := debug.ReadBuildInfo()
- if ok {
- // find the Caddy module in the dependency list
- for _, dep := range bi.Deps {
- if dep.Path == ImportPath {
- module = dep
- break
- }
+ if !ok {
+ if CustomVersion != "" {
+ full = CustomVersion
+ simple = CustomVersion
+ return
+ }
+ full = "unknown"
+ simple = "unknown"
+ return
+ }
+ // find the Caddy module in the dependency list
+ for _, dep := range bi.Deps {
+ if dep.Path == ImportPath {
+ module = dep
+ break
}
}
if module != nil {