Skip to content

Commit

Permalink
encoding: bazelify stringer
Browse files Browse the repository at this point in the history
This one is a total hack. See comment for details.

Release note: None
  • Loading branch information
otan committed Mar 17, 2021
1 parent caca8df commit 2504594
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ load("@bazel_gazelle//:def.bzl", "gazelle")
# gazelle:exclude pkg/sql/sem/tree/statementtype_string.go
# gazelle:exclude pkg/sql/txnevent_string.go
# gazelle:exclude pkg/sql/txntype_string.go
# gazelle:exclude pkg/util/encoding/type_string.go
# gazelle:exclude pkg/util/timeutil/pgdate/field_string.go
# gazelle:exclude pkg/util/timeutil/pgdate/parsemode_string.go
# gazelle:exclude pkg/workload/schemachange/optype_string.go
Expand Down
64 changes: 33 additions & 31 deletions pkg/util/encoding/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_library(
"decimal.go",
"encoding.go",
"float.go",
"type_string.go",
":gen-type-stringer", # keep
],
importpath = "github.com/cockroachdb/cockroach/pkg/util/encoding",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -60,33 +60,35 @@ go_test(
],
)

# TODO (alanmas): Solve stringer issue "stringer: can't handle non-integer constant type Type"
# Seems that we need to include pkg/util/encoding/encodingtype somehow.
# We already tried to copy the source files over so bazel source now are enconding and encodingtype
# but it is still failing due to the same error.

# genrule(
# name = "gen-type-stringer",
# srcs = [
# "//pkg/util/encoding/encodingtype:encoding_type.go",
# "encoding.go",
# ],
# outs = ["type_string.go"],
#
# # golang.org/x/tools executes commands via
# # golang.org/x/sys/execabs which requires all PATH lookups to
# # result in absolute paths. To account for this, we resolve the
# # relative path returned by location to an absolute path.
# cmd = """
# cp $(location encoding.go) `dirname $(location //pkg/util/encoding/encodingtype:encoding_type.go)`/encoding.go
# GO_REL_PATH=`dirname $(location @go_sdk//:bin/go)`
# GO_ABS_PATH=`cd $$GO_REL_PATH && pwd`
# env PATH=$$GO_ABS_PATH HOME=$(GENDIR) \
# $(location @org_golang_x_tools//cmd/stringer:stringer) -output=$@ \
# -type=Type `dirname $(location //pkg/util/encoding/encodingtype:encoding_type.go)`/encoding.go $(location //pkg/util/encoding/encodingtype:encoding_type.go)
# """,
# tools = [
# "@go_sdk//:bin/go",
# "@org_golang_x_tools//cmd/stringer",
# ],
# )
genrule(
name = "gen-type-stringer",
srcs = [
"encoding.go",
],
outs = ["type_string.go"],
# golang.org/x/tools executes commands via
# golang.org/x/sys/execabs which requires all PATH lookups to
# result in absolute paths. To account for this, we resolve the
# relative path returned by location to an absolute path.
#
# We do an additional hack here, replacing `type Type encodingtype.T`
# with `type Type int`, as stringer in a bazel environment does not
# enjoy encodingtype being a separate package. All our attempts
# ended up in the error `stringer: no values defined for type Type`,
# which is due to some weird stringer interaction with the packages.Load.
#
# This has caused a long day of headache for @otan / weeks for @alan-mas.
# Try and fix this at the risk of banging your head against 100 watermelons
# in Guinness World Record time.
cmd = """
GO_REL_PATH=`dirname $(location @go_sdk//:bin/go)`
GO_ABS_PATH=`cd $$GO_REL_PATH && pwd`
sed -e 's/type Type encodingtype.T/type Type int/' $(location encoding.go) > encoding_tmp.go && \
env PATH=$$GO_ABS_PATH HOME=$(GENDIR) \
$(location @org_golang_x_tools//cmd/stringer:stringer) -output=$@ -type=Type encoding_tmp.go
""",
tools = [
"@go_sdk//:bin/go",
"@org_golang_x_tools//cmd/stringer",
],
)
7 changes: 0 additions & 7 deletions pkg/util/encoding/encodingtype/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/util/encoding/encodingtype",
visibility = ["//visibility:public"],
)

exports_files(
[
"encoding_type.go",
],
visibility = ["//visibility:public"],
)

0 comments on commit 2504594

Please sign in to comment.