Skip to content

Commit

Permalink
gopls/internal/lsp/cache: allow versions of form "go1.2.3"
Browse files Browse the repository at this point in the history
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 <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
adonovan committed Jan 19, 2024
1 parent d0930db commit cd7b510
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gopls/internal/lsp/cache/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -1565,8 +1565,8 @@ func (b *typeCheckBatch) checkPackage(ctx context.Context, ph *packageHandle) (*
return &Package{ph.mp, ph.loadDiagnostics, pkg}, nil
}

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

func (b *typeCheckBatch) typesConfig(ctx context.Context, inputs typeCheckInputs, onError func(e error)) *types.Config {
cfg := &types.Config{
Expand Down
30 changes: 30 additions & 0 deletions gopls/internal/lsp/cache/constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,33 @@ func TestIsStandaloneFile(t *testing.T) {
})
}
}

func TestVersionRegexp(t *testing.T) {
// good
for _, s := range []string{
"go1",
"go1.2",
"go1.2.3",
"go1.0.33",
} {
if !goVersionRx.MatchString(s) {
t.Errorf("Valid Go version %q does not match the regexp", s)
}
}

// bad
for _, s := range []string{
"go", // missing numbers
"go0", // Go starts at 1
"go01", // leading zero
"go1.π", // non-decimal
"go1.-1", // negative
"go1.02.3", // leading zero
"go1.2.3.4", // too many segments
"go1.2.3-pre", // textual suffix
} {
if goVersionRx.MatchString(s) {
t.Errorf("Invalid Go version %q unexpectedly matches the regexp", s)
}
}
}

0 comments on commit cd7b510

Please sign in to comment.