Skip to content

Commit 9b64301

Browse files
committed
gopls/internal/cache: avoid go/types panic on version "go1.2.3"
This change fixes a gopls panic caused by giving go/[email protected] a Go version string with three components, e.g. go1.2.3. Unfortunately this is hard to write a test for, since it requires building gopls with go1.20 and running it with a go1.21 toolchain. Fixes golang/go#66195 Change-Id: I09257e6ded69568812b367ee80cafea30add93d3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/570135 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 93c0ca5 commit 9b64301

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

gopls/internal/cache/check.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/sha256"
1010
"fmt"
1111
"go/ast"
12+
"go/build"
1213
"go/parser"
1314
"go/token"
1415
"go/types"
@@ -30,6 +31,7 @@ import (
3031
"golang.org/x/tools/gopls/internal/protocol"
3132
"golang.org/x/tools/gopls/internal/util/bug"
3233
"golang.org/x/tools/gopls/internal/util/safetoken"
34+
"golang.org/x/tools/gopls/internal/util/slices"
3335
"golang.org/x/tools/internal/analysisinternal"
3436
"golang.org/x/tools/internal/event"
3537
"golang.org/x/tools/internal/event/tag"
@@ -1629,10 +1631,19 @@ func (b *typeCheckBatch) typesConfig(ctx context.Context, inputs typeCheckInputs
16291631

16301632
if inputs.goVersion != "" {
16311633
goVersion := "go" + inputs.goVersion
1634+
16321635
// types.NewChecker panics if GoVersion is invalid. An unparsable mod
16331636
// file should probably stop us before we get here, but double check
16341637
// just in case.
1635-
if goVersionRx.MatchString(goVersion) {
1638+
//
1639+
// Prior to go/[email protected] the precondition was stricter:
1640+
// no patch version. That's not a problem when also using go1.20 list,
1641+
// as it would reject go.mod files containing a patch version, but
1642+
// go/[email protected] will panic on go.mod versions that are returned
1643+
// by go1.21 list, hence the need for the extra check.
1644+
if goVersionRx.MatchString(goVersion) &&
1645+
(slices.Contains(build.Default.ReleaseTags, "go1.21") ||
1646+
strings.Count(goVersion, ".") < 2) { // no patch version
16361647
cfg.GoVersion = goVersion
16371648
}
16381649
}

0 commit comments

Comments
 (0)