Skip to content

Commit e074ef8

Browse files
committed
gopls/internal: don't show a warning if the 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 <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]>
1 parent 7fba77c commit e074ef8

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

gopls/internal/lsp/general.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,21 @@ func OldestSupportedGoVersion() int {
251251
return GoVersionTable[len(GoVersionTable)-1].GoVersion + 1
252252
}
253253

254-
func versionMessage(oldestVersion int) (string, protocol.MessageType) {
254+
// versionMessage returns the warning/error message to display if the user is
255+
// on the given Go version, if any. The goVersion variable is the X in Go 1.X.
256+
//
257+
// If goVersion is invalid (< 0), it returns "", 0.
258+
func versionMessage(goVersion int) (string, protocol.MessageType) {
259+
if goVersion < 0 {
260+
return "", 0
261+
}
262+
255263
for _, v := range GoVersionTable {
256-
if oldestVersion <= v.GoVersion {
264+
if goVersion <= v.GoVersion {
257265
var msgBuilder strings.Builder
258266

259267
mType := protocol.Error
260-
fmt.Fprintf(&msgBuilder, "Found Go version 1.%d", oldestVersion)
268+
fmt.Fprintf(&msgBuilder, "Found Go version 1.%d", goVersion)
261269
if v.DeprecatedVersion != "" {
262270
// not deprecated yet, just a warning
263271
fmt.Fprintf(&msgBuilder, ", which will be unsupported by gopls %s. ", v.DeprecatedVersion)

gopls/internal/lsp/general_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,32 @@ import (
1313

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

2627
for _, test := range tests {
27-
gotMsg, gotType := versionMessage(test.goVer)
28+
gotMsg, gotType := versionMessage(test.goVersion)
2829

2930
if len(test.wantContains) == 0 && gotMsg != "" {
30-
t.Errorf("versionMessage(%d) = %q, want \"\"", test.goVer, gotMsg)
31+
t.Errorf("versionMessage(%d) = %q, want \"\"", test.goVersion, gotMsg)
3132
}
3233

3334
for _, want := range test.wantContains {
3435
if !strings.Contains(gotMsg, want) {
35-
t.Errorf("versionMessage(%d) = %q, want containing %q", test.goVer, gotMsg, want)
36+
t.Errorf("versionMessage(%d) = %q, want containing %q", test.goVersion, gotMsg, want)
3637
}
3738
}
3839

3940
if gotType != test.wantType {
40-
t.Errorf("versionMessage(%d) = returned message type %d, want %d", test.goVer, gotType, test.wantType)
41+
t.Errorf("versionMessage(%d) = returned message type %d, want %d", test.goVersion, gotType, test.wantType)
4142
}
4243
}
4344
}

0 commit comments

Comments
 (0)