Skip to content

Commit cd7b510

Browse files
committed
gopls/internal/lsp/cache: allow versions of form "go1.2.3"
Plus a unit test. Fixes golang/go#63472 Change-Id: I69466024eb876457d0d3f262cef3467b25879d93 Reviewed-on: https://go-review.googlesource.com/c/tools/+/557215 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent d0930db commit cd7b510

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

gopls/internal/lsp/cache/check.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,8 +1565,8 @@ func (b *typeCheckBatch) checkPackage(ctx context.Context, ph *packageHandle) (*
15651565
return &Package{ph.mp, ph.loadDiagnostics, pkg}, nil
15661566
}
15671567

1568-
// TODO(golang/go#63472): this looks wrong with the new Go version syntax.
1569-
var goVersionRx = regexp.MustCompile(`^go([1-9][0-9]*)\.(0|[1-9][0-9]*)$`)
1568+
// e.g. "go1" or "go1.2" or "go1.2.3"
1569+
var goVersionRx = regexp.MustCompile(`^go[1-9][0-9]*(?:\.(0|[1-9][0-9]*)){0,2}$`)
15701570

15711571
func (b *typeCheckBatch) typesConfig(ctx context.Context, inputs typeCheckInputs, onError func(e error)) *types.Config {
15721572
cfg := &types.Config{

gopls/internal/lsp/cache/constraints_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,33 @@ func TestIsStandaloneFile(t *testing.T) {
9494
})
9595
}
9696
}
97+
98+
func TestVersionRegexp(t *testing.T) {
99+
// good
100+
for _, s := range []string{
101+
"go1",
102+
"go1.2",
103+
"go1.2.3",
104+
"go1.0.33",
105+
} {
106+
if !goVersionRx.MatchString(s) {
107+
t.Errorf("Valid Go version %q does not match the regexp", s)
108+
}
109+
}
110+
111+
// bad
112+
for _, s := range []string{
113+
"go", // missing numbers
114+
"go0", // Go starts at 1
115+
"go01", // leading zero
116+
"go1.π", // non-decimal
117+
"go1.-1", // negative
118+
"go1.02.3", // leading zero
119+
"go1.2.3.4", // too many segments
120+
"go1.2.3-pre", // textual suffix
121+
} {
122+
if goVersionRx.MatchString(s) {
123+
t.Errorf("Invalid Go version %q unexpectedly matches the regexp", s)
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)