Skip to content

Commit

Permalink
gopls: add a main.version variable to override the version at linktime
Browse files Browse the repository at this point in the history
As discussed in golang/go#63803, add a version variable to allow package
managers to set the gopls version at linktime, thereby avoiding the need
to patch source.

Fixes golang/go#63803

Change-Id: Ie6696283e1007577e82d96f01c96e16e89cfcb6f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/555457
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
findleyr authored and gopherbot committed Jan 19, 2024
1 parent 6673e7a commit c467be3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 22 deletions.
14 changes: 13 additions & 1 deletion gopls/internal/cmd/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"golang.org/x/tools/gopls/internal/hooks"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/util/bug"
"golang.org/x/tools/gopls/internal/version"
"golang.org/x/tools/internal/testenv"
"golang.org/x/tools/internal/tool"
"golang.org/x/tools/txtar"
Expand All @@ -55,7 +56,7 @@ func TestVersion(t *testing.T) {
tree := writeTree(t, "")

// There's not much we can robustly assert about the actual version.
want := debug.Version() // e.g. "master"
want := version.Version() // e.g. "master"

// basic
{
Expand All @@ -64,6 +65,13 @@ func TestVersion(t *testing.T) {
res.checkStdout(want)
}

// basic, with version override
{
res := goplsWithEnv(t, tree, []string{"TEST_GOPLS_VERSION=v1.2.3"}, "version")
res.checkExit(true)
res.checkStdout(`v1\.2\.3`)
}

// -json flag
{
res := gopls(t, tree, "version", "-json")
Expand Down Expand Up @@ -1034,6 +1042,10 @@ func goplsMain() {
bug.PanicOnBugs = true
}

if v := os.Getenv("TEST_GOPLS_VERSION"); v != "" {
version.VersionOverride = v
}

tool.Main(context.Background(), cmd.New(hooks.Options), os.Args[1:])
}

Expand Down
10 changes: 5 additions & 5 deletions gopls/internal/cmd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (
"sync"
"time"

"golang.org/x/tools/gopls/internal/debug"
"golang.org/x/tools/gopls/internal/filecache"
"golang.org/x/tools/gopls/internal/lsp/command"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/server"
"golang.org/x/tools/gopls/internal/settings"
goplsbug "golang.org/x/tools/gopls/internal/util/bug"
bugpkg "golang.org/x/tools/gopls/internal/util/bug"
versionpkg "golang.org/x/tools/gopls/internal/version"
"golang.org/x/tools/internal/event"
)

Expand Down 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: versionpkg.Version(),
GOPACKAGESDRIVER: os.Getenv("GOPACKAGESDRIVER"),
}

Expand Down Expand Up @@ -147,7 +147,7 @@ func (s *stats) Run(ctx context.Context, args ...string) error {
do("Gathering bug reports", func() error {
stats.CacheDir, stats.BugReports = filecache.BugReports()
if stats.BugReports == nil {
stats.BugReports = []goplsbug.Bug{} // non-nil for JSON
stats.BugReports = []bugpkg.Bug{} // non-nil for JSON
}
return nil
})
Expand Down Expand Up @@ -232,7 +232,7 @@ type GoplsStats struct {
GOPACKAGESDRIVER string
InitialWorkspaceLoadDuration string `anon:"ok"` // in time.Duration string form
CacheDir string
BugReports []goplsbug.Bug
BugReports []bugpkg.Bug
MemStats command.MemStatsResult `anon:"ok"`
WorkspaceStats command.WorkspaceStatsResult `anon:"ok"`
DirStats dirStats `anon:"ok"`
Expand Down
20 changes: 6 additions & 14 deletions gopls/internal/debug/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"runtime"
"runtime/debug"
"strings"

"golang.org/x/tools/gopls/internal/version"
)

type PrintMode int
Expand All @@ -25,16 +27,6 @@ const (
JSON
)

// Version is a manually-updated mechanism for tracking versions.
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 {
Expand All @@ -48,12 +40,12 @@ type ServerVersion struct {
func VersionInfo() *ServerVersion {
if info, ok := debug.ReadBuildInfo(); ok {
return &ServerVersion{
Version: Version(),
Version: version.Version(),
BuildInfo: info,
}
}
return &ServerVersion{
Version: Version(),
Version: version.Version(),
BuildInfo: &debug.BuildInfo{
Path: "gopls, built in GOPATH mode",
GoVersion: runtime.Version(),
Expand Down Expand Up @@ -124,11 +116,11 @@ 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())
printModuleInfo(w, info.Main, mode)
fmt.Fprintf(w, "%v %v\n", info.Path, version.Version())
if !verbose {
return
}
printModuleInfo(w, info.Main, mode)
for _, dep := range info.Deps {
printModuleInfo(w, *dep, mode)
}
Expand Down
6 changes: 4 additions & 2 deletions gopls/internal/debug/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"encoding/json"
"runtime"
"testing"

"golang.org/x/tools/gopls/internal/version"
)

func TestPrintVersionInfoJSON(t *testing.T) {
Expand All @@ -27,7 +29,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.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 +43,7 @@ func TestPrintVersionInfoPlainText(t *testing.T) {
res := buf.Bytes()

// Other fields of BuildInfo may not be available during test.
wantGoplsVersion, wantGoVersion := Version(), runtime.Version()
wantGoplsVersion, wantGoVersion := version.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)
}
Expand Down
29 changes: 29 additions & 0 deletions gopls/internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2019 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.

// Package version manages the gopls version.
//
// The VersionOverride variable may be used to set the gopls version at link
// time.
package version

import "runtime/debug"

var VersionOverride = ""

// Version returns the gopls version.
//
// By default, this is read from runtime/debug.ReadBuildInfo, but may be
// overridden by the [VersionOverride] variable.
func Version() string {
if VersionOverride != "" {
return VersionOverride
}
if info, ok := debug.ReadBuildInfo(); ok {
if info.Main.Version != "" {
return info.Main.Version
}
}
return "(unknown)"
}
5 changes: 5 additions & 0 deletions gopls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ import (
"golang.org/x/telemetry/counter"
"golang.org/x/tools/gopls/internal/cmd"
"golang.org/x/tools/gopls/internal/hooks"
versionpkg "golang.org/x/tools/gopls/internal/version"
"golang.org/x/tools/internal/tool"
)

var version = "" // if set by the linker, overrides the gopls version

func main() {
versionpkg.VersionOverride = version

counter.Open() // Enable telemetry counter writing.
ctx := context.Background()
tool.Main(ctx, cmd.New(hooks.Options), os.Args[1:])
Expand Down

0 comments on commit c467be3

Please sign in to comment.