Skip to content

cmd/fix: support go versions with patch release #62586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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