From 71e27b844b3bb99c8402a6c35302c57a74768333 Mon Sep 17 00:00:00 2001 From: Lukas Vogel Date: Mon, 19 Dec 2022 22:23:45 +0100 Subject: [PATCH] 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 --- caddy.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/caddy.go b/caddy.go index 1ecad94e..5d0152a7 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 {