From 5ac1074c541f6e2dff4e05459764cf9f943168d3 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 4 Nov 2021 16:20:51 -0700 Subject: [PATCH] main: Report version information for "interesting" dependencies We have a few dependencies that are such a significant part of Terraform's behavior that they will often be the root cause of or the solution to a bug reported against Terraform. As a small quality-of-life improvement to help with diagnosing those, we'll now report the selected versions for each of these so-called "interesting" dependencies as part of our initial trace log output during Terraform startup. The goal here is that when someone opens a bug report, and includes the trace log as our bug report template requests, we'll be able to see at a glance which versions of these dependencies were involved, instead of having to manually cross-reference in the go.mod file of the reported main Terraform CLI version. This does slightly grow the general overhead of the logs, but as long as we keep this set of interesting dependencies relatively small it shouldn't present any significant problem in typical usage. --- main.go | 3 +++ version/dependencies.go | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 version/dependencies.go diff --git a/main.go b/main.go index 38c7141cc..1ffd8a343 100644 --- a/main.go +++ b/main.go @@ -79,6 +79,9 @@ func realMain() int { log.Printf( "[INFO] Terraform version: %s %s", Version, VersionPrerelease) + for _, depMod := range version.InterestingDependencies() { + log.Printf("[DEBUG] using %s %s", depMod.Path, depMod.Version) + } log.Printf("[INFO] Go runtime version: %s", runtime.Version()) log.Printf("[INFO] CLI args: %#v", os.Args) diff --git a/version/dependencies.go b/version/dependencies.go new file mode 100644 index 000000000..155d7eafc --- /dev/null +++ b/version/dependencies.go @@ -0,0 +1,44 @@ +package version + +import "runtime/debug" + +// See the docs for InterestingDependencyVersions to understand what +// "interesting" is intended to mean here. We should keep this set relatively +// small to avoid bloating the logs too much. +var interestingDependencies = map[string]struct{}{ + "github.com/hashicorp/hcl/v2": {}, + "github.com/zclconf/go-cty": {}, + "github.com/hashicorp/go-tfe": {}, + "github.com/hashicorp/terraform-config-inspect": {}, + "github.com/hashicorp/terraform-svchost": {}, +} + +// InterestingDependencies returns the compiled-in module version info for +// a small number of dependencies that Terraform uses broadly and which we +// tend to upgrade relatively often as part of improvements to Terraform. +// +// The set of dependencies this reports might change over time if our +// opinions change about what's "interesting". This is here only to create +// a small number of extra annotations in a debug log to help us more easily +// cross-reference bug reports with dependency changelogs. +func InterestingDependencies() []*debug.Module { + info, ok := debug.ReadBuildInfo() + if !ok { + // Weird to not be built in module mode, but not a big deal. + return nil + } + + ret := make([]*debug.Module, 0, len(interestingDependencies)) + + for _, mod := range info.Deps { + if _, ok := interestingDependencies[mod.Path]; !ok { + continue + } + if mod.Replace != nil { + mod = mod.Replace + } + ret = append(ret, mod) + } + + return ret +}