Skip to content
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

*: enable gofmt #35721

Merged
merged 2 commits into from
Jun 24, 2022
Merged
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
8 changes: 8 additions & 0 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,14 @@ def go_deps():
sum = "h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=",
version = "v0.0.4",
)
go_repository(
name = "com_github_golangci_gofmt",
build_file_proto_mode = "disable",
importpath = "github.com/golangci/gofmt",
sum = "h1:iR3fYXUjHCR97qWS8ch1y9zPNsgXThGwjKPrYfqMPks=",
version = "v0.0.0-20190930125516-244bba706f1a",
)

go_repository(
name = "com_github_golangci_prealloc",
build_file_proto_mode = "disable",
Expand Down
12 changes: 11 additions & 1 deletion br/pkg/errors/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "errors",
Expand All @@ -7,3 +7,13 @@ go_library(
visibility = ["//visibility:public"],
deps = ["@com_github_pingcap_errors//:errors"],
)

go_test(
name = "errors_test",
srcs = ["errors_test.go"],
deps = [
":errors",
"@com_github_pingcap_errors//:errors",
"@com_github_stretchr_testify//require",
],
)
1 change: 1 addition & 0 deletions br/pkg/pdutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
"//util/codec",
"@com_github_coreos_go_semver//semver",
"@com_github_docker_go_units//:go-units",
"@com_github_google_uuid//:uuid",
"@com_github_opentracing_opentracing_go//:opentracing-go",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
Expand Down
1 change: 1 addition & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ nogo(
"@org_golang_x_tools//go/analysis/passes/unusedresult:go_default_library",
"//build/linter/durationcheck:durationcheck",
"//build/linter/exportloopref:exportloopref",
"//build/linter/gofmt:gofmt",
"//build/linter/ineffassign:ineffassign",
"//build/linter/prealloc:prealloc",
] + staticcheck_analyzers(STATICHECK_ANALYZERS),
Expand Down
12 changes: 12 additions & 0 deletions build/linter/gofmt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "gofmt",
srcs = ["analyzer.go"],
importpath = "github.com/pingcap/tidb/build/linter/gofmt",
visibility = ["//visibility:public"],
deps = [
"@com_github_golangci_gofmt//gofmt",
"@org_golang_x_tools//go/analysis",
],
)
65 changes: 65 additions & 0 deletions build/linter/gofmt/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gofmt

import (
"fmt"
"strings"

"github.com/golangci/gofmt/gofmt"
"golang.org/x/tools/go/analysis"
)

// Analyzer is the analyzer struct of gofmt.
var Analyzer = &analysis.Analyzer{
Name: "gofmt",
Doc: "gofmt checks whether code was gofmt-ed" +
"this tool runs with -s option to check for code simplification",
Run: run,
}

var needSimplify bool

func init() {
Analyzer.Flags.BoolVar(&needSimplify, "need-simplify", true, "run gofmt with -s for code simplification")
}

func run(pass *analysis.Pass) (any, error) {
fileNames := make([]string, 0, 10)
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
if pos.Filename != "" && !strings.HasSuffix(pos.Filename, "failpoint_binding__.go") {
fileNames = append(fileNames, pos.Filename)
}
}

for _, f := range fileNames {
diff, err := gofmt.Run(f, needSimplify)
if err != nil {
return nil, fmt.Errorf("could not run gofmt: %w (%s)", err, f)
}

if diff == nil {
continue
}

pass.Report(analysis.Diagnostic{
Pos: 1,
Message: fmt.Sprintf("\n%s", diff),
})
}

return nil, nil
}
10 changes: 10 additions & 0 deletions build/nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@
".*_generated\\.go$": "ignore generated code"
}
},
"gofmt": {
"exclude_files": {
"/external/": "no need to vet third party code",
".*_generated\\.go$": "ignore generated code",
"/cgo/": "ignore cgo code",
"/rules_go_work-*": "ignore generated code",
".*test_/testmain\\.go$": "ignore generated code",
".*failpoint_binding__.go$": "ignore generated code"
}
},
"httpresponse": {
"exclude_files": {
"/external/": "no need to vet third party code",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ require (
require (
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1581
github.com/charithe/durationcheck v0.0.9
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
github.com/gordonklaus/ineffassign v0.0.0-20210914165742-4cc7213b9bc8
github.com/kyoh86/exportloopref v0.1.8
honnef.co/go/tools v0.0.1-2020.1.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ github.com/golang/snappy v0.0.2-0.20190904063534-ff6b7dc882cf/go.mod h1:/XxbfmMg
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a h1:iR3fYXUjHCR97qWS8ch1y9zPNsgXThGwjKPrYfqMPks=
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a/go.mod h1:9qCChq59u/eW8im404Q2WWTrnBUQKjpNYKMbU4M7EFU=
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21 h1:leSNB7iYzLYSSx3J/s5sVf4Drkc68W2wm4Ixh/mr0us=
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21/go.mod h1:tf5+bzsHdTM0bsB7+8mt0GUMvjCgwLpTapNZHU8AajI=
github.com/gomodule/redigo v1.7.1-0.20190724094224-574c33c3df38/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
Expand Down