Skip to content

Commit f2e5ab6

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/cache: set goVersion when there is no module version
When there is no module version to indicate the correct language semantics for a package, we should fall back on the view Go version. This fixes golang/go#75000, where a suggestion to modernize a script broke the user's build. Fixes golang/go#75000 Change-Id: I57f537117941490c13b09e13bcd6f7a60fa77c0b Reviewed-on: https://go-review.googlesource.com/c/tools/+/695935 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Findley <rfindley@google.com>
1 parent 2ef7d42 commit f2e5ab6

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

gopls/internal/cache/check.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,9 @@ func (s *Snapshot) typeCheckInputs(ctx context.Context, mp *metadata.Package) (*
14941494
goVersion := ""
14951495
if mp.Module != nil && mp.Module.GoVersion != "" {
14961496
goVersion = mp.Module.GoVersion
1497+
} else {
1498+
// Fall back on the go version implied by the ambient Go command.
1499+
goVersion = fmt.Sprintf("1.%d", s.View().GoVersion())
14971500
}
14981501

14991502
return &typeCheckInputs{

gopls/internal/test/integration/diagnostics/analysis_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"golang.org/x/tools/gopls/internal/cache"
1212
"golang.org/x/tools/gopls/internal/protocol"
1313
. "golang.org/x/tools/gopls/internal/test/integration"
14+
"golang.org/x/tools/internal/testenv"
1415
)
1516

1617
// Test for the timeformat analyzer, following golang/vscode-go#2406.
@@ -154,3 +155,45 @@ var Y interface{}
154155
)
155156
})
156157
}
158+
159+
func TestModernizationConsistency_Issue75000(t *testing.T) {
160+
testenv.SkipAfterGoCommand1Point(t, 24)
161+
testenv.NeedsGoCommand1Point(t, 22) // uses range-over-int
162+
163+
// This test checks that we don't offer modernization suggestions when the
164+
// ambient Go version is older than the modernized APIs.
165+
//
166+
// The code is from golang/go#75000, where gopls suggested to use
167+
// `waitgroup.Go` even though the user was on 1.24.
168+
169+
const src = `
170+
-- main.go --
171+
package main
172+
173+
import (
174+
"fmt"
175+
"sync"
176+
)
177+
178+
func doit(i int) {
179+
fmt.Println("i = ", i)
180+
}
181+
182+
func main() {
183+
var wg sync.WaitGroup
184+
for i := range 5 {
185+
wg.Add(1)
186+
go func() {
187+
defer wg.Done()
188+
doit(i)
189+
}()
190+
}
191+
192+
wg.Wait()
193+
}
194+
`
195+
Run(t, src, func(t *testing.T, env *Env) {
196+
env.OpenFile("main.go")
197+
env.AfterChange(NoDiagnostics())
198+
})
199+
}

0 commit comments

Comments
 (0)