-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
bazel: generate //go:generate stringer
files sandbox
#59146
Conversation
Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR. My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks like we're on the right path.
Please replace gazzelle with gazelle in the commit message.
Also we are repeating the same code fairly often. We can simplify this into a function in, e.g. STRINGER.bzl
, similar to how we do colexec:
def stringer(file, typ):
native.genrule(
name = "gen-" + file, # maybe replace `file` with `-` if bazel doesn't like that.
srcs = [
file,
],
outs = [basename(file) + "_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",
],
)
@@ -58,3 +59,19 @@ go_test( | |||
"@com_github_stretchr_testify//require", | |||
], | |||
) | |||
|
|||
# genrule( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happened here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is showing this:
"stringer: can't handle non-integer constant type Type"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to include pkg/util/encoding/encodingtype
somehow, similar to what we do with x-package stringers here:
cockroach/pkg/sql/opt/BUILD.bazel
Line 109 in 731d083
$(location @org_golang_x_tools//cmd/stringer:stringer) -output=$@ \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code is almost the same, I am using $(location @org_golang_x_tools//cmd/stringer:stringer) -output=$@
inside genrule
I might be confused there but what do you mean with "include pkg/util/encoding/encodingtype
somehow"
The only difference I can see is that you are manually copy one of the source files over into the location of the other. So you mean something like copy pkg/util/encoding/encodingtype
inside the same location in which we are running?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the reason you are seeing this error is because Type
is defined as
// Type represents the type of a value encoded by
// Encode{Null,NotNull,Varint,Uvarint,Float,Bytes}.
//go:generate stringer -type=Type
type Type encodingtype.T
which relies on
"github.com/cockroachdb/cockroach/pkg/util/encoding/encodingtype"
So yes, we'd need to copy the source files over as otherwise bazel's only source is encoding.go
and has no way of reading encodingtype
.
This is a good idea! let me give a shot on it :) Just did it adding |
77b553e
to
e614ec0
Compare
Thank you for updating your pull request. My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan. |
8111818
to
f62b249
Compare
7af4aeb
to
e144f3e
Compare
//go:generate stringer
files sandbox//go:generate stringer
files sandbox
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good after we address the comments. :)
pkg/base/BUILD.bazel
Outdated
@@ -64,3 +65,9 @@ go_test( | |||
"@com_github_stretchr_testify//require", | |||
], | |||
) | |||
|
|||
# Using common function for stringer to create testclusterreplicationmode_string.go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this comment is useful, just delete it at this point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used it as there is no "real" clue on which will be the output (only that you go and check the function) but yes, you are right I can delete it in all of the files :)
@@ -64,3 +65,9 @@ go_test( | |||
"@com_github_stretchr_testify//require", | |||
], | |||
) | |||
|
|||
# Using common function for stringer to create testclusterreplicationmode_string.go | |||
stringer( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I don't like that it's unclear what the name
of this rule is. Can you add a name
argument to the stringer()
helper function? Then the call sites would have to be updated as well.
Basically I'm worried that in the future someone will CTRL-F for gen-testclusterreplicationmode-stringer
and not be able to find it, which is confusing. Also the name gen-testclusterreplicationmode-stringer
itself is long and silly, you could probably pick a better one by hand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am using that name gen-testclusterreplicationmode-stringer
using this logic gen + name of the file + stringer.
that name I think is the longest (so far) but the others are like: Type, Key, etc.
And for the name
in argument I can add it, but not sure if that change will do any major change (as this is the file that has the longest name) but I do agree with you in the part of CTRL-F example, so let me add name over there to avoid this kind of issues
e144f3e
to
bc52401
Compare
First part of cockroachdb#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
bc52401
to
ef93819
Compare
bors r+ |
Build succeeded: |
First part of #57787 work.
As
gazelle
is not taking care by itself in any changes related to//go:generate stringer
we are creating agenrule
to handle it.From all the
//go:generate stringer
files, there are some that are having troubles during bazel build:Release note: None