Skip to content

Commit

Permalink
bazel: generate //go:generate stringer files within sandbox.
Browse files Browse the repository at this point in the history
First part of #57787 work.

As `gazelle` is not taking care by itself in any changes related to `//go:generate stringer` we are creating a
`genrule` to handle it.

From all the `//go:generate stringer` files, there are some that are having troubles during bazel build:
```
-pkg/util/encoding/encoding.go
-pkg/util/encoding/BUILD.bazel
"stringer: can't handle non-integer constant type Type"

-pkg/workload/schemachange/schemachange.go
-pkg/workload/schemachange/BUILD.bazel
"stringer: can't happen: constant is not an integer TxStatusInFailure"
```
I already added some TODO task over there to keep them in track

Release note: None
  • Loading branch information
alan-mas committed Jan 25, 2021
1 parent 8efa63d commit ef93819
Show file tree
Hide file tree
Showing 23 changed files with 341 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/STRINGER.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Common function to create //go:generate stringer files within bazel sandbox

def stringer(file, typ, name):
native.genrule(
name = name,
srcs = [
file,
],
outs = [typ.lower() + "_string.go"],
cmd = """
env PATH=`dirname $(location @go_sdk//:bin/go)` HOME=$(GENDIR) \
$(location @org_golang_x_tools//cmd/stringer:stringer) -output=$@ -type={} $<
""".format(typ),
tools = [
"@go_sdk//:bin/go",
"@org_golang_x_tools//cmd/stringer",
],
)
8 changes: 8 additions & 0 deletions pkg/base/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "base",
Expand All @@ -14,6 +15,7 @@ go_library(
"testclusterreplicationmode_string.go",
"testing_knobs.go",
"zone.go",
":gen-clusterreplication-stringer", # keep
],
importpath = "github.com/cockroachdb/cockroach/pkg/base",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -64,3 +66,9 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

stringer(
name = "gen-clusterreplication-stringer",
file = "test_server_args.go",
typ = "TestClusterReplicationMode",
)
8 changes: 8 additions & 0 deletions pkg/ccl/sqlproxyccl/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "sqlproxyccl",
Expand All @@ -12,6 +13,7 @@ go_library(
"metrics.go",
"proxy.go",
"server.go",
":gen-errorcode-stringer", # keep
],
importpath = "github.com/cockroachdb/cockroach/pkg/ccl/sqlproxyccl",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -58,3 +60,9 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

stringer(
name = "gen-errorcode-stringer",
file = "error.go",
typ = "ErrorCode",
)
8 changes: 8 additions & 0 deletions pkg/cli/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "cli",
Expand Down Expand Up @@ -51,6 +52,7 @@ go_library(
"tsdump.go",
"userfile.go",
"zip.go",
":gen-keytype-stringer", # keep
],
# keep
cdeps = [
Expand Down Expand Up @@ -336,3 +338,9 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

stringer(
name = "gen-keytype-stringer",
file = "flags_util.go",
typ = "keyType",
)
8 changes: 8 additions & 0 deletions pkg/clusterversion/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "clusterversion",
Expand All @@ -11,6 +12,7 @@ go_library(
"keyed_versions.go",
"setting.go",
"testutils.go",
":gen-key-stringer", # keep
],
embed = [":clusterversion_go_proto"],
importpath = "github.com/cockroachdb/cockroach/pkg/clusterversion",
Expand Down Expand Up @@ -61,3 +63,9 @@ go_proto_library(
"@com_github_gogo_protobuf//gogoproto",
],
)

stringer(
name = "gen-key-stringer",
file = "cockroach_versions.go",
typ = "Key",
)
8 changes: 8 additions & 0 deletions pkg/kv/kvclient/kvcoord/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "kvcoord",
Expand Down Expand Up @@ -28,6 +29,7 @@ go_library(
"txn_lock_gatekeeper.go",
"txn_metrics.go",
"txnstate_string.go",
":gen-txnstate-stringer", # keep
],
importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -149,3 +151,9 @@ go_test(
"@org_golang_x_sync//errgroup",
],
)

stringer(
name = "gen-txnstate-stringer",
file = "txn_coord_sender.go",
typ = "txnState",
)
8 changes: 8 additions & 0 deletions pkg/kv/kvserver/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "kvserver",
Expand Down Expand Up @@ -91,6 +92,7 @@ go_library(
"testing_knobs.go",
"track_raft_protos.go",
"ts_maintenance_queue.go",
":gen-refreshraftreason-stringer", # keep
],
embed = [":kvserver_go_proto"],
importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvserver",
Expand Down Expand Up @@ -427,3 +429,9 @@ go_proto_library(
"@io_etcd_go_etcd_raft_v3//raftpb",
],
)

stringer(
name = "gen-refreshraftreason-stringer",
file = "replica_raft.go",
typ = "refreshRaftReason",
)
17 changes: 17 additions & 0 deletions pkg/roachpb/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "roachpb",
Expand All @@ -21,6 +22,8 @@ go_library(
"span_group.go",
"tenant.go",
"version.go",
":gen-errordetailtype-stringer", # keep
":gen-method-stringer", # keep
],
embed = [":roachpb_go_proto"],
importpath = "github.com/cockroachdb/cockroach/pkg/roachpb",
Expand Down Expand Up @@ -146,3 +149,17 @@ go_proto_library(
"@com_github_gogo_protobuf//gogoproto",
],
)

# Using common function for stringer to create method_string.go
stringer(
name = "gen-method-stringer",
file = "method.go",
typ = "Method",
)

# Using common function for stringer to create errordetailtype_string.go
stringer(
name = "gen-errordetailtype-stringer",
file = "errors.go",
typ = "ErrorDetailType",
)
29 changes: 29 additions & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "sql",
Expand Down Expand Up @@ -218,6 +219,10 @@ go_library(
"zero.go",
"zigzag_join.go",
"zone_config.go",
":gen-advancecode-stringer", # keep
":gen-nodestatus-stringer", # keep
":gen-txnevent-stringer", # keep
":gen-txntype-stringer", # keep
],
importpath = "github.com/cockroachdb/cockroach/pkg/sql",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -588,3 +593,27 @@ go_test(
"@org_golang_x_sync//errgroup",
],
)

stringer(
name = "gen-txnevent-stringer",
file = "txn_state.go",
typ = "txnEvent",
)

stringer(
name = "gen-txntype-stringer",
file = "txn_state.go",
typ = "txnType",
)

stringer(
name = "gen-advancecode-stringer",
file = "txn_state.go",
typ = "advanceCode",
)

stringer(
name = "gen-nodestatus-stringer",
file = "distsql_physical_planner.go",
typ = "NodeStatus",
)
8 changes: 8 additions & 0 deletions pkg/sql/catalog/catalogkv/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "catalogkv",
Expand All @@ -7,6 +8,7 @@ go_library(
"descriptorkind_string.go",
"namespace.go",
"test_utils.go",
":gen-descriptorkind-stringer", # keep
],
importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkv",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -49,3 +51,9 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

stringer(
name = "gen-descriptorkind-stringer",
file = "catalogkv.go",
typ = "DescriptorKind",
)
15 changes: 15 additions & 0 deletions pkg/sql/catalog/descpb/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "descpb",
Expand All @@ -16,6 +17,8 @@ go_library(
"privilegedescversion_string.go",
"region_name.go",
"structured.go",
":gen-formatversion-stringer", # keep
":gen-privilegedescversion-stringer", # keep
],
embed = [":descpb_go_proto"],
importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb",
Expand Down Expand Up @@ -83,3 +86,15 @@ go_proto_library(
"@com_github_gogo_protobuf//gogoproto",
],
)

stringer(
name = "gen-privilegedescversion-stringer",
file = "privilege.go",
typ = "PrivilegeDescVersion",
)

stringer(
name = "gen-formatversion-stringer",
file = "structured.go",
typ = "FormatVersion",
)
8 changes: 8 additions & 0 deletions pkg/sql/colfetcher/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "colfetcher",
srcs = [
"cfetcher.go",
"colbatch_scan.go",
"fetcherstate_string.go",
":gen-fetcherstate-stringer", # keep
],
importpath = "github.com/cockroachdb/cockroach/pkg/sql/colfetcher",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -38,3 +40,9 @@ go_library(
"@com_github_cockroachdb_errors//:errors",
],
)

stringer(
name = "gen-fetcherstate-stringer",
file = "cfetcher.go",
typ = "fetcherState",
)
15 changes: 15 additions & 0 deletions pkg/sql/execinfra/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "execinfra",
Expand All @@ -18,6 +19,8 @@ go_library(
"server_config.go",
"testutils.go",
"version.go",
":gen-consumerstatus-stringer", # keep
":gen-procstate-stringer", # keep
],
importpath = "github.com/cockroachdb/cockroach/pkg/sql/execinfra",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -88,3 +91,15 @@ go_test(
"//pkg/util/randutil",
],
)

stringer(
name = "gen-procstate-stringer",
file = "processorsbase.go",
typ = "procState",
)

stringer(
name = "gen-consumerstatus-stringer",
file = "base.go",
typ = "ConsumerStatus",
)
15 changes: 15 additions & 0 deletions pkg/sql/opt/optgen/lang/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg:STRINGER.bzl", "stringer")

go_library(
name = "lang",
Expand All @@ -13,6 +14,8 @@ go_library(
"token_string.go",
":gen-expr", # keep
":gen-operator", # keep
":gen-operator-stringer", # keep
":gen-token-stringer", # keep
],
importpath = "github.com/cockroachdb/cockroach/pkg/sql/opt/optgen/lang",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -78,3 +81,15 @@ genrule(
""",
tools = ["//pkg/sql/opt/optgen/cmd/langgen"],
)

stringer(
name = "gen-token-stringer",
file = "scanner.go",
typ = "Token",
)

stringer(
name = "gen-operator-stringer",
file = "operator.og.go",
typ = "Operator",
)
Loading

0 comments on commit ef93819

Please sign in to comment.