Skip to content

Commit

Permalink
gopls/internal/lsp/debug: remove hard-coded version
Browse files Browse the repository at this point in the history
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 <noreply+kokoro@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
findleyr committed Aug 24, 2023
1 parent 0a044c0 commit c28af0a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 71 deletions.
2 changes: 1 addition & 1 deletion gopls/internal/lsp/cmd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/cmd/test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
29 changes: 0 additions & 29 deletions gopls/internal/lsp/debug/buildinfo_go1.12.go

This file was deleted.

19 changes: 0 additions & 19 deletions gopls/internal/lsp/debug/buildinfo_go1.18.go

This file was deleted.

38 changes: 20 additions & 18 deletions gopls/internal/lsp/debug/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,38 @@ 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
}

// VersionInfo returns the build info for the gopls process. If it was not
// 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(),
},
}
}

Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions gopls/internal/lsp/debug/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
}

0 comments on commit c28af0a

Please sign in to comment.