From c28af0abbd9a213421dca5a03f6de8c93830263a Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 24 Aug 2023 11:23:10 -0400 Subject: [PATCH] gopls/internal/lsp/debug: remove hard-coded version Just use main module information for producing the gopls version, saving us a CL during the release process. This means that we won't see a version if gopls is built in GOPATH mode, but we probably should support this installation mode (if it even works...). Change-Id: Ib964b2dd6b2cf202805507a47f04b5077f4d24c3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/522184 gopls-CI: kokoro Reviewed-by: Hyang-Ah Hana Kim Run-TryBot: Robert Findley TryBot-Result: Gopher Robot --- gopls/internal/lsp/cmd/stats.go | 2 +- .../internal/lsp/cmd/test/integration_test.go | 2 +- gopls/internal/lsp/debug/buildinfo_go1.12.go | 29 -------------- gopls/internal/lsp/debug/buildinfo_go1.18.go | 19 ---------- gopls/internal/lsp/debug/info.go | 38 ++++++++++--------- gopls/internal/lsp/debug/info_test.go | 7 ++-- 6 files changed, 26 insertions(+), 71 deletions(-) delete mode 100644 gopls/internal/lsp/debug/buildinfo_go1.12.go delete mode 100644 gopls/internal/lsp/debug/buildinfo_go1.18.go diff --git a/gopls/internal/lsp/cmd/stats.go b/gopls/internal/lsp/cmd/stats.go index 4986107134e..4e339f1c543 100644 --- a/gopls/internal/lsp/cmd/stats.go +++ b/gopls/internal/lsp/cmd/stats.go @@ -74,7 +74,7 @@ func (s *stats) Run(ctx context.Context, args ...string) error { GOARCH: runtime.GOARCH, GOPLSCACHE: os.Getenv("GOPLSCACHE"), GoVersion: runtime.Version(), - GoplsVersion: debug.Version, + GoplsVersion: debug.Version(), } opts := s.app.options diff --git a/gopls/internal/lsp/cmd/test/integration_test.go b/gopls/internal/lsp/cmd/test/integration_test.go index 5c694d070b8..b7b6d9bd86c 100644 --- a/gopls/internal/lsp/cmd/test/integration_test.go +++ b/gopls/internal/lsp/cmd/test/integration_test.go @@ -55,7 +55,7 @@ func TestVersion(t *testing.T) { tree := writeTree(t, "") // There's not much we can robustly assert about the actual version. - const want = debug.Version // e.g. "master" + want := debug.Version() // e.g. "master" // basic { diff --git a/gopls/internal/lsp/debug/buildinfo_go1.12.go b/gopls/internal/lsp/debug/buildinfo_go1.12.go deleted file mode 100644 index 2f360dbfc70..00000000000 --- a/gopls/internal/lsp/debug/buildinfo_go1.12.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2022 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build !go1.18 -// +build !go1.18 - -package debug - -import ( - "runtime" - "runtime/debug" -) - -type BuildInfo struct { - debug.BuildInfo - GoVersion string // Version of Go that produced this binary -} - -func readBuildInfo() (*BuildInfo, bool) { - rinfo, ok := debug.ReadBuildInfo() - if !ok { - return nil, false - } - return &BuildInfo{ - GoVersion: runtime.Version(), - BuildInfo: *rinfo, - }, true -} diff --git a/gopls/internal/lsp/debug/buildinfo_go1.18.go b/gopls/internal/lsp/debug/buildinfo_go1.18.go deleted file mode 100644 index 4121c4bc9cd..00000000000 --- a/gopls/internal/lsp/debug/buildinfo_go1.18.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2022 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.18 -// +build go1.18 - -package debug - -import ( - "runtime/debug" -) - -type BuildInfo debug.BuildInfo - -func readBuildInfo() (*BuildInfo, bool) { - info, ok := debug.ReadBuildInfo() - return (*BuildInfo)(info), ok -} diff --git a/gopls/internal/lsp/debug/info.go b/gopls/internal/lsp/debug/info.go index 5ce23fc2f59..34e6dd4e2b1 100644 --- a/gopls/internal/lsp/debug/info.go +++ b/gopls/internal/lsp/debug/info.go @@ -30,12 +30,19 @@ const ( ) // Version is a manually-updated mechanism for tracking versions. -const Version = "master" +func Version() string { + if info, ok := debug.ReadBuildInfo(); ok { + if info.Main.Version != "" { + return info.Main.Version + } + } + return "(unknown)" +} // ServerVersion is the format used by gopls to report its version to the // client. This format is structured so that the client can parse it easily. type ServerVersion struct { - *BuildInfo + *debug.BuildInfo Version string } @@ -43,23 +50,18 @@ type ServerVersion struct { // built in module mode, we return a GOPATH-specific message with the // hardcoded version. func VersionInfo() *ServerVersion { - if info, ok := readBuildInfo(); ok { - return getVersion(info) - } - buildInfo := &BuildInfo{} - // go1.17 or earlier, part of s.BuildInfo are embedded fields. - buildInfo.Path = "gopls, built in GOPATH mode" - buildInfo.GoVersion = runtime.Version() - return &ServerVersion{ - Version: Version, - BuildInfo: buildInfo, + if info, ok := debug.ReadBuildInfo(); ok { + return &ServerVersion{ + Version: Version(), + BuildInfo: info, + } } -} - -func getVersion(info *BuildInfo) *ServerVersion { return &ServerVersion{ - Version: Version, - BuildInfo: info, + Version: Version(), + BuildInfo: &debug.BuildInfo{ + Path: "gopls, built in GOPATH mode", + GoVersion: runtime.Version(), + }, } } @@ -125,7 +127,7 @@ func section(w io.Writer, mode PrintMode, title string, body func()) { } func printBuildInfo(w io.Writer, info *ServerVersion, verbose bool, mode PrintMode) { - fmt.Fprintf(w, "%v %v\n", info.Path, Version) + fmt.Fprintf(w, "%v %v\n", info.Path, Version()) printModuleInfo(w, info.Main, mode) if !verbose { return diff --git a/gopls/internal/lsp/debug/info_test.go b/gopls/internal/lsp/debug/info_test.go index 5a536284193..3bc9290c157 100644 --- a/gopls/internal/lsp/debug/info_test.go +++ b/gopls/internal/lsp/debug/info_test.go @@ -27,7 +27,7 @@ func TestPrintVersionInfoJSON(t *testing.T) { if g, w := got.GoVersion, runtime.Version(); g != w { t.Errorf("go version = %v, want %v", g, w) } - if g, w := got.Version, Version; g != w { + if g, w := got.Version, Version(); g != w { t.Errorf("gopls version = %v, want %v", g, w) } // Other fields of BuildInfo may not be available during test. @@ -41,7 +41,8 @@ func TestPrintVersionInfoPlainText(t *testing.T) { res := buf.Bytes() // Other fields of BuildInfo may not be available during test. - if !bytes.Contains(res, []byte(Version)) || !bytes.Contains(res, []byte(runtime.Version())) { - t.Errorf("plaintext output = %q,\nwant (version: %v, go: %v)", res, Version, runtime.Version()) + wantGoplsVersion, wantGoVersion := Version(), runtime.Version() + if !bytes.Contains(res, []byte(wantGoplsVersion)) || !bytes.Contains(res, []byte(wantGoVersion)) { + t.Errorf("plaintext output = %q,\nwant (version: %v, go: %v)", res, wantGoplsVersion, wantGoVersion) } }