Skip to content

Commit

Permalink
cmd/fix: support go versions with patch release
Browse files Browse the repository at this point in the history
Support go version with patch release(e.g. 1.21.0)
and release candidates(e.g. 1.21rc1)
when parsing the go version in the fix command
by using new "go/version" package.

Fixes #62584

Change-Id: I0ec16137c7a396c68039d374c770c4021fb54b4e
GitHub-Last-Rev: 76bced5
GitHub-Pull-Request: #62586
Reviewed-on: https://go-review.googlesource.com/c/go/+/527342
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Russ Cox <rsc@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Alex Bozhenko <alexbozhenko@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
  • Loading branch information
alexbozhenko authored and gopherbot committed Feb 22, 2024
1 parent 5d4e8f5 commit 7fd62ba
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 32 deletions.
5 changes: 3 additions & 2 deletions src/cmd/fix/buildtag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ package main

import (
"go/ast"
"go/version"
"strings"
)

func init() {
register(buildtagFix)
}

const buildtagGoVersionCutoff = 1_18
const buildtagGoVersionCutoff = "go1.18"

var buildtagFix = fix{
name: "buildtag",
Expand All @@ -23,7 +24,7 @@ var buildtagFix = fix{
}

func buildtag(f *ast.File) bool {
if goVersion < buildtagGoVersionCutoff {
if version.Compare(*goVersion, buildtagGoVersionCutoff) < 0 {
return false
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/fix/buildtag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func init() {
var buildtagTests = []testCase{
{
Name: "buildtag.oldGo",
Version: 1_10,
Version: "go1.10",
In: `//go:build yes
// +build yes
Expand All @@ -20,7 +20,7 @@ package main
},
{
Name: "buildtag.new",
Version: 1_99,
Version: "go1.99",
In: `//go:build yes
// +build yes
Expand Down
29 changes: 6 additions & 23 deletions src/cmd/fix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
"go/parser"
"go/scanner"
"go/token"
"go/version"
"internal/diff"
"io"
"io/fs"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)

Expand All @@ -37,10 +37,8 @@ var forceRewrites = flag.String("force", "",
var allowed, force map[string]bool

var (
doDiff = flag.Bool("diff", false, "display diffs instead of rewriting files")
goVersionStr = flag.String("go", "", "go language version for files")

goVersion int // 115 for go1.15
doDiff = flag.Bool("diff", false, "display diffs instead of rewriting files")
goVersion = flag.String("go", "", "go language version for files")
)

// enable for debugging fix failures
Expand Down Expand Up @@ -68,24 +66,9 @@ func main() {
flag.Usage = usage
flag.Parse()

if *goVersionStr != "" {
if !strings.HasPrefix(*goVersionStr, "go") {
report(fmt.Errorf("invalid -go=%s", *goVersionStr))
os.Exit(exitCode)
}
majorStr := (*goVersionStr)[len("go"):]
minorStr := "0"
if before, after, found := strings.Cut(majorStr, "."); found {
majorStr, minorStr = before, after
}
major, err1 := strconv.Atoi(majorStr)
minor, err2 := strconv.Atoi(minorStr)
if err1 != nil || err2 != nil || major < 0 || major >= 100 || minor < 0 || minor >= 100 {
report(fmt.Errorf("invalid -go=%s", *goVersionStr))
os.Exit(exitCode)
}

goVersion = major*100 + minor
if !version.IsValid(*goVersion) {
report(fmt.Errorf("invalid -go=%s", *goVersion))
os.Exit(exitCode)
}

sort.Sort(byDate(fixes))
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/fix/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type testCase struct {
Name string
Fn func(*ast.File) bool
Version int
Version string
In string
Out string
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestRewrite(t *testing.T) {
for _, tt := range testCases {
tt := tt
t.Run(tt.Name, func(t *testing.T) {
if tt.Version == 0 {
if tt.Version == "" {
if testing.Verbose() {
// Don't run in parallel: cmd/fix sometimes writes directly to stderr,
// and since -v prints which test is currently running we want that
Expand All @@ -105,10 +105,10 @@ func TestRewrite(t *testing.T) {
t.Parallel()
}
} else {
old := goVersion
goVersion = tt.Version
old := *goVersion
*goVersion = tt.Version
defer func() {
goVersion = old
*goVersion = old
}()
}

Expand Down

0 comments on commit 7fd62ba

Please sign in to comment.