Skip to content

Commit

Permalink
[gopls-release-branch.0.10] gopls/internal: don't show a warning if t…
Browse files Browse the repository at this point in the history
…he Go version is undetected

CL 445581 inadvertently removed suppression of the Go version error if
the Go version was undetected. Add it back, with a test.

Fixes golang/go#56465

Change-Id: I352369096280c8d3423a7345123ec9309359fb58
Reviewed-on: https://go-review.googlesource.com/c/tools/+/446175
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
(cherry picked from commit d62bb0ee81c0fdecd166cad1f91e3f61204b0640)
Reviewed-on: https://go-review.googlesource.com/c/tools/+/446235
  • Loading branch information
findleyr committed Oct 28, 2022
1 parent d12147c commit 09a89ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 11 additions & 3 deletions gopls/internal/lsp/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,21 @@ func OldestSupportedGoVersion() int {
return GoVersionTable[len(GoVersionTable)-1].GoVersion + 1
}

func versionMessage(oldestVersion int) (string, protocol.MessageType) {
// versionMessage returns the warning/error message to display if the user is
// on the given Go version, if any. The goVersion variable is the X in Go 1.X.
//
// If goVersion is invalid (< 0), it returns "", 0.
func versionMessage(goVersion int) (string, protocol.MessageType) {
if goVersion < 0 {
return "", 0
}

for _, v := range GoVersionTable {
if oldestVersion <= v.GoVersion {
if goVersion <= v.GoVersion {
var msgBuilder strings.Builder

mType := protocol.Error
fmt.Fprintf(&msgBuilder, "Found Go version 1.%d", oldestVersion)
fmt.Fprintf(&msgBuilder, "Found Go version 1.%d", goVersion)
if v.DeprecatedVersion != "" {
// not deprecated yet, just a warning
fmt.Fprintf(&msgBuilder, ", which will be unsupported by gopls %s. ", v.DeprecatedVersion)
Expand Down
11 changes: 6 additions & 5 deletions gopls/internal/lsp/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,32 @@ import (

func TestVersionMessage(t *testing.T) {
tests := []struct {
goVer int
goVersion int
wantContains []string // string fragments that we expect to see
wantType protocol.MessageType
}{
{-1, nil, 0},
{12, []string{"1.12", "not supported", "upgrade to Go 1.16", "install gopls v0.7.5"}, protocol.Error},
{13, []string{"1.13", "will be unsupported by gopls v0.11.0", "upgrade to Go 1.16", "install gopls v0.9.5"}, protocol.Warning},
{15, []string{"1.15", "will be unsupported by gopls v0.11.0", "upgrade to Go 1.16", "install gopls v0.9.5"}, protocol.Warning},
{16, nil, 0},
}

for _, test := range tests {
gotMsg, gotType := versionMessage(test.goVer)
gotMsg, gotType := versionMessage(test.goVersion)

if len(test.wantContains) == 0 && gotMsg != "" {
t.Errorf("versionMessage(%d) = %q, want \"\"", test.goVer, gotMsg)
t.Errorf("versionMessage(%d) = %q, want \"\"", test.goVersion, gotMsg)
}

for _, want := range test.wantContains {
if !strings.Contains(gotMsg, want) {
t.Errorf("versionMessage(%d) = %q, want containing %q", test.goVer, gotMsg, want)
t.Errorf("versionMessage(%d) = %q, want containing %q", test.goVersion, gotMsg, want)
}
}

if gotType != test.wantType {
t.Errorf("versionMessage(%d) = returned message type %d, want %d", test.goVer, gotType, test.wantType)
t.Errorf("versionMessage(%d) = returned message type %d, want %d", test.goVersion, gotType, test.wantType)
}
}
}

0 comments on commit 09a89ee

Please sign in to comment.