Skip to content

Commit 676d610

Browse files
alexbozhenkocherrymui
authored andcommitted
[release-branch.go1.22] cmd/fix: support go versions with patch release
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. For #62584. Fixes #68825. 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 <[email protected]> Reviewed-by: Russ Cox <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Alex Bozhenko <[email protected]> Reviewed-by: Bryan Mills <[email protected]> (cherry picked from commit 7fd62ba) Reviewed-on: https://go-review.googlesource.com/c/go/+/603981 Reviewed-by: Cherry Mui <[email protected]> TryBot-Bypass: Cherry Mui <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Kirill Kolyshkin <[email protected]>
1 parent 0a525a3 commit 676d610

File tree

4 files changed

+16
-32
lines changed

4 files changed

+16
-32
lines changed

src/cmd/fix/buildtag.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ package main
66

77
import (
88
"go/ast"
9+
"go/version"
910
"strings"
1011
)
1112

1213
func init() {
1314
register(buildtagFix)
1415
}
1516

16-
const buildtagGoVersionCutoff = 1_18
17+
const buildtagGoVersionCutoff = "go1.18"
1718

1819
var buildtagFix = fix{
1920
name: "buildtag",
@@ -23,7 +24,7 @@ var buildtagFix = fix{
2324
}
2425

2526
func buildtag(f *ast.File) bool {
26-
if goVersion < buildtagGoVersionCutoff {
27+
if version.Compare(*goVersion, buildtagGoVersionCutoff) < 0 {
2728
return false
2829
}
2930

src/cmd/fix/buildtag_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func init() {
1111
var buildtagTests = []testCase{
1212
{
1313
Name: "buildtag.oldGo",
14-
Version: 1_10,
14+
Version: "go1.10",
1515
In: `//go:build yes
1616
// +build yes
1717
@@ -20,7 +20,7 @@ package main
2020
},
2121
{
2222
Name: "buildtag.new",
23-
Version: 1_99,
23+
Version: "go1.99",
2424
In: `//go:build yes
2525
// +build yes
2626

src/cmd/fix/main.go

+6-23
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
"go/parser"
1414
"go/scanner"
1515
"go/token"
16+
"go/version"
1617
"internal/diff"
1718
"io"
1819
"io/fs"
1920
"os"
2021
"path/filepath"
2122
"sort"
22-
"strconv"
2323
"strings"
2424
)
2525

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

3939
var (
40-
doDiff = flag.Bool("diff", false, "display diffs instead of rewriting files")
41-
goVersionStr = flag.String("go", "", "go language version for files")
42-
43-
goVersion int // 115 for go1.15
40+
doDiff = flag.Bool("diff", false, "display diffs instead of rewriting files")
41+
goVersion = flag.String("go", "", "go language version for files")
4442
)
4543

4644
// enable for debugging fix failures
@@ -68,24 +66,9 @@ func main() {
6866
flag.Usage = usage
6967
flag.Parse()
7068

71-
if *goVersionStr != "" {
72-
if !strings.HasPrefix(*goVersionStr, "go") {
73-
report(fmt.Errorf("invalid -go=%s", *goVersionStr))
74-
os.Exit(exitCode)
75-
}
76-
majorStr := (*goVersionStr)[len("go"):]
77-
minorStr := "0"
78-
if before, after, found := strings.Cut(majorStr, "."); found {
79-
majorStr, minorStr = before, after
80-
}
81-
major, err1 := strconv.Atoi(majorStr)
82-
minor, err2 := strconv.Atoi(minorStr)
83-
if err1 != nil || err2 != nil || major < 0 || major >= 100 || minor < 0 || minor >= 100 {
84-
report(fmt.Errorf("invalid -go=%s", *goVersionStr))
85-
os.Exit(exitCode)
86-
}
87-
88-
goVersion = major*100 + minor
69+
if !version.IsValid(*goVersion) {
70+
report(fmt.Errorf("invalid -go=%s", *goVersion))
71+
os.Exit(exitCode)
8972
}
9073

9174
sort.Sort(byDate(fixes))

src/cmd/fix/main_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
type testCase struct {
1818
Name string
1919
Fn func(*ast.File) bool
20-
Version int
20+
Version string
2121
In string
2222
Out string
2323
}
@@ -96,7 +96,7 @@ func TestRewrite(t *testing.T) {
9696
for _, tt := range testCases {
9797
tt := tt
9898
t.Run(tt.Name, func(t *testing.T) {
99-
if tt.Version == 0 {
99+
if tt.Version == "" {
100100
if testing.Verbose() {
101101
// Don't run in parallel: cmd/fix sometimes writes directly to stderr,
102102
// and since -v prints which test is currently running we want that
@@ -105,10 +105,10 @@ func TestRewrite(t *testing.T) {
105105
t.Parallel()
106106
}
107107
} else {
108-
old := goVersion
109-
goVersion = tt.Version
108+
old := *goVersion
109+
*goVersion = tt.Version
110110
defer func() {
111-
goVersion = old
111+
*goVersion = old
112112
}()
113113
}
114114

0 commit comments

Comments
 (0)