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

bazel: generate //go:generate stringer files sandbox #59146

Merged
merged 1 commit into from
Jan 25, 2021

Conversation

alan-mas
Copy link
Contributor

@alan-mas alan-mas commented Jan 19, 2021

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"

Release note: None

@alan-mas alan-mas requested review from a team as code owners January 19, 2021 17:09
@blathers-crl
Copy link

blathers-crl bot commented Jan 19, 2021

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.

@blathers-crl blathers-crl bot added the O-community Originated from the community label Jan 19, 2021
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@alan-mas alan-mas requested review from otan and removed request for a team January 19, 2021 17:21
Copy link
Contributor

@otan otan left a 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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happened here?

Copy link
Contributor Author

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"

Copy link
Contributor

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:

$(location @org_golang_x_tools//cmd/stringer:stringer) -output=$@ \

Copy link
Contributor Author

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?

Copy link
Contributor

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.

@alan-mas
Copy link
Contributor Author

alan-mas commented Jan 19, 2021

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",
       ],
   )

This is a good idea! let me give a shot on it :)

Just did it adding pkg/STRINGER.bzlbazel file to reuse the code across project :)

@alan-mas alan-mas force-pushed the 57787-go-generate-stringer branch from 77b553e to e614ec0 Compare January 20, 2021 00:20
@blathers-crl blathers-crl bot requested a review from otan January 20, 2021 00:20
@blathers-crl
Copy link

blathers-crl bot commented Jan 20, 2021

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.

@alan-mas alan-mas force-pushed the 57787-go-generate-stringer branch 5 times, most recently from 8111818 to f62b249 Compare January 21, 2021 22:08
@alan-mas alan-mas requested a review from rickystewart January 21, 2021 22:09
@alan-mas alan-mas force-pushed the 57787-go-generate-stringer branch 2 times, most recently from 7af4aeb to e144f3e Compare January 22, 2021 00:03
@alan-mas alan-mas changed the title *WIP* bazel: generate //go:generate stringer files sandbox bazel: generate //go:generate stringer files sandbox Jan 22, 2021
Copy link
Collaborator

@rickystewart rickystewart left a 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. :)

@@ -64,3 +65,9 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

# Using common function for stringer to create testclusterreplicationmode_string.go
Copy link
Collaborator

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.

Copy link
Contributor Author

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(
Copy link
Collaborator

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.

Copy link
Contributor Author

@alan-mas alan-mas Jan 22, 2021

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

@alan-mas alan-mas force-pushed the 57787-go-generate-stringer branch from e144f3e to bc52401 Compare January 22, 2021 19:51
@cockroachdb cockroachdb deleted a comment from craig bot Jan 23, 2021
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
@alan-mas alan-mas force-pushed the 57787-go-generate-stringer branch from bc52401 to ef93819 Compare January 25, 2021 15:41
@rickystewart
Copy link
Collaborator

bors r+

@craig
Copy link
Contributor

craig bot commented Jan 25, 2021

Build succeeded:

@craig craig bot merged commit bf439cd into cockroachdb:master Jan 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-community Originated from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants