Skip to content

Commit 68630b3

Browse files
adonovangopherbot
authored andcommitted
[gopls-release-branch.0.15] 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]> (cherry picked from commit 9b64301) Reviewed-on: https://go-review.googlesource.com/c/tools/+/569879 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent f5357f5 commit 68630b3

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"
@@ -29,6 +30,7 @@ import (
2930
"golang.org/x/tools/gopls/internal/protocol"
3031
"golang.org/x/tools/gopls/internal/util/bug"
3132
"golang.org/x/tools/gopls/internal/util/safetoken"
33+
"golang.org/x/tools/gopls/internal/util/slices"
3234
"golang.org/x/tools/internal/analysisinternal"
3335
"golang.org/x/tools/internal/event"
3436
"golang.org/x/tools/internal/event/tag"
@@ -1628,10 +1630,19 @@ func (b *typeCheckBatch) typesConfig(ctx context.Context, inputs typeCheckInputs
16281630

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

0 commit comments

Comments
 (0)