Skip to content

Commit

Permalink
tools: add tazel (#42605)
Browse files Browse the repository at this point in the history
close #42608
  • Loading branch information
hawkingrei authored Mar 28, 2023
1 parent 7442a66 commit 22839a3
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 21 deletions.
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load("@bazel_gazelle//:def.bzl", "gazelle")
# gazelle:exclude tidb-binlog/proto/proto
# gazelle:exclude plugin/conn_ip_example
# gazelle:exclude build/linter/staticcheck
# gazelle:resolve go github.com/bazelbuild/buildtools/build @com_github_bazelbuild_buildtools//build:go_default_library
gazelle(name = "gazelle")

package(default_visibility = ["//visibility:public"])
Expand Down
15 changes: 15 additions & 0 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ def go_deps():
sum = "h1:Q2feRPMlcfVcqz3pF87PJzkm5lZrL+x6BDtzhODzNJM=",
version = "v11.2.8+incompatible",
)
go_repository(
name = "com_github_bazelbuild_buildtools",
build_file_proto_mode = "disable",
importpath = "github.com/bazelbuild/buildtools",
sum = "h1:XmPu4mXICgdGnC5dXGjUGbwUD/kUmS0l5Aop3LaevBM=",
version = "v0.0.0-20230317132445-9c3c1fc0106e",
)

go_repository(
name = "com_github_benbjohnson_clock",
Expand Down Expand Up @@ -5828,6 +5835,14 @@ def go_deps():
sum = "h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=",
version = "v1.3.0",
)
go_repository(
name = "net_starlark_go",
build_file_proto_mode = "disable",
importpath = "go.starlark.net",
sum = "h1:xwwDQW5We85NaTk2APgoN9202w/l0DVGp+GZMfsrh7s=",
version = "v0.0.0-20210223155950-e043a3d3c984",
)

go_repository(
name = "org_golang_google_api",
build_file_proto_mode = "disable_global",
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

include Makefile.common

.PHONY: all clean test server dev benchkv benchraw check checklist parser tidy ddltest build_br build_lightning build_lightning-ctl build_dumpling ut bazel_build bazel_prepare bazel_test check-file-perm check-bazel-prepare bazel_lint
.PHONY: all clean test server dev benchkv benchraw check checklist parser tidy ddltest build_br build_lightning build_lightning-ctl build_dumpling ut bazel_build bazel_prepare bazel_test check-file-perm check-bazel-prepare bazel_lint tazel

default: server buildsucc

Expand Down Expand Up @@ -402,6 +402,9 @@ bazel_ci_prepare:
bazel_prepare:
bazel run //:gazelle
bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro DEPS.bzl%go_deps -build_file_proto_mode=disable
bazel run \
--run_under="cd $(CURDIR) && " \
//tools/tazel:tazel

check-bazel-prepare:
@echo "make bazel_prepare"
Expand Down
2 changes: 2 additions & 0 deletions br/pkg/restore/ingestrec/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ go_library(

go_test(
name = "ingestrec_test",
timeout = "short",
srcs = ["ingest_recorder_test.go"],
flaky = True,
deps = [
":ingestrec",
"//parser/model",
Expand Down
40 changes: 20 additions & 20 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,29 @@ nogo(
"@org_golang_x_tools//go/analysis/passes/unreachable:go_default_library",
"@org_golang_x_tools//go/analysis/passes/unsafeptr:go_default_library",
"@org_golang_x_tools//go/analysis/passes/unusedresult:go_default_library",
"//build/linter/asciicheck:asciicheck",
"//build/linter/bodyclose:bodyclose",
"//build/linter/durationcheck:durationcheck",
"//build/linter/exportloopref:exportloopref",
"//build/linter/forcetypeassert:forcetypeassert",
"//build/linter/gofmt:gofmt",
"//build/linter/gci:gci",
"//build/linter/gosec:gosec",
"//build/linter/ineffassign:ineffassign",
"//build/linter/makezero:makezero",
"//build/linter/misspell:misspell",
"//build/linter/noloopclosure:noloopclosure",
"//build/linter/prealloc:prealloc",
"//build/linter/predeclared:predeclared",
"//build/linter/unconvert:unconvert",
"//build/linter/rowserrcheck:rowserrcheck",
"//build/linter/asciicheck",
"//build/linter/bodyclose",
"//build/linter/durationcheck",
"//build/linter/exportloopref",
"//build/linter/forcetypeassert",
"//build/linter/gofmt",
"//build/linter/gci",
"//build/linter/gosec",
"//build/linter/ineffassign",
"//build/linter/makezero",
"//build/linter/misspell",
"//build/linter/noloopclosure",
"//build/linter/prealloc",
"//build/linter/predeclared",
"//build/linter/unconvert",
"//build/linter/rowserrcheck",
] + staticcheck_analyzers(STATICHECK_ANALYZERS) +
select({
"//build:with_nogo": [
"//build/linter/filepermission:filepermission",
"//build/linter/allrevive:allrevive",
"//build/linter/errcheck:errcheck",
"//build/linter/revive:revive",
"//build/linter/allrevive",
"//build/linter/errcheck",
"//build/linter/filepermission",
"//build/linter/revive",
],
"//conditions:default": [],
}),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/apache/skywalking-eyes v0.4.0
github.com/ashanbrown/makezero v1.1.1
github.com/aws/aws-sdk-go v1.44.48
github.com/bazelbuild/buildtools v0.0.0-20230317132445-9c3c1fc0106e
github.com/blacktear23/go-proxyprotocol v1.0.5
github.com/carlmjohnson/flagext v0.21.0
github.com/charithe/durationcheck v0.0.10
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ github.com/aws/aws-sdk-go v1.30.19/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZve
github.com/aws/aws-sdk-go v1.44.48 h1:jLDC9RsNoYMLFlKpB8LdqUnoDdC2yvkS4QbuyPQJ8+M=
github.com/aws/aws-sdk-go v1.44.48/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/bazelbuild/buildtools v0.0.0-20230317132445-9c3c1fc0106e h1:XmPu4mXICgdGnC5dXGjUGbwUD/kUmS0l5Aop3LaevBM=
github.com/bazelbuild/buildtools v0.0.0-20230317132445-9c3c1fc0106e/go.mod h1:689QdV3hBP7Vo9dJMmzhoYIyo/9iMhEmHkJcnaPRCbo=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
Expand Down Expand Up @@ -1043,6 +1045,7 @@ go.opentelemetry.io/otel/trace v0.20.0 h1:1DL6EXUdcg95gukhuRRvLDO/4X5THh/5dIV52l
go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw=
go.opentelemetry.io/proto/otlp v0.7.0 h1:rwOQPCuKAKmwGKq2aVNnYIibI6wnV7EvzgfTCzcdGg8=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.starlark.net v0.0.0-20210223155950-e043a3d3c984/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
Expand Down
1 change: 1 addition & 0 deletions keyspace/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(

go_test(
name = "keyspace_test",
timeout = "short",
srcs = ["keyspace_test.go"],
embed = [":keyspace"],
flaky = True,
Expand Down
1 change: 1 addition & 0 deletions tests/realtikvtest/flashbacktest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "flashbacktest_test",
timeout = "short",
srcs = [
"flashback_test.go",
"main_test.go",
Expand Down
23 changes: 23 additions & 0 deletions tools/tazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "tazel_lib",
srcs = [
"main.go",
"util.go",
],
importpath = "github.com/pingcap/tidb/tools/tazel",
visibility = ["//visibility:private"],
deps = [
"//util/set",
"@com_github_bazelbuild_buildtools//build:go_default_library",
"@com_github_pingcap_log//:log",
"@org_uber_go_zap//:zap",
],
)

go_binary(
name = "tazel",
embed = [":tazel_lib"],
visibility = ["//visibility:public"],
)
65 changes: 65 additions & 0 deletions tools/tazel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2023 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 main

import (
"errors"
"io/fs"
"os"
"path/filepath"

"github.com/bazelbuild/buildtools/build"
"github.com/pingcap/log"
"go.uber.org/zap"
)

func main() {
if _, err := os.Stat("WORKSPACE"); errors.Is(err, os.ErrNotExist) {
log.Fatal("It should run from the project root")
}
err := filepath.Walk(".", func(path string, d fs.FileInfo, _ error) error {
if d.IsDir() || d.Name() != "BUILD.bazel" || skipTazel(path) {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
log.Fatal("fail to read file", zap.Error(err), zap.String("path", path))
}
buildfile, err := build.ParseBuild(d.Name(), data)
if err != nil {
log.Fatal("fail to parser BUILD.bazel", zap.Error(err), zap.String("path", path))
}
gotest := buildfile.Rules("go_test")
toWrite := false
if len(gotest) != 0 {
if gotest[0].AttrString("timeout") == "" {
gotest[0].SetAttr("timeout", &build.StringExpr{Value: "short"})
toWrite = true
}
if !skipFlaky(path) && gotest[0].AttrLiteral("flaky") == "" {
gotest[0].SetAttr("flaky", &build.LiteralExpr{Token: "True"})
toWrite = true
}
}
if toWrite {
log.Info("write file", zap.String("path", path))
write(path, buildfile)
}
return nil
})
if err != nil {
log.Fatal("fail to filepath.Walk", zap.Error(err))
}
}
40 changes: 40 additions & 0 deletions tools/tazel/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2023 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 main

import (
"os"

"github.com/bazelbuild/buildtools/build"
"github.com/pingcap/tidb/util/set"
)

func write(path string, f *build.File) error {
build.Rewrite(f)
out := build.Format(f)
return os.WriteFile(path, out, 0644)
}

func skipFlaky(path string) bool {
var pmap = set.NewStringSet()
pmap.Insert("tests/realtikvtest/addindextest/BUILD.bazel")
return pmap.Exist(path)
}

func skipTazel(path string) bool {
var pmap = set.NewStringSet()
pmap.Insert("build/BUILD.bazel")
return pmap.Exist(path)
}

0 comments on commit 22839a3

Please sign in to comment.