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

Update go_repository defaults to only use Go code #1888

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TvdW
Copy link
Contributor

@TvdW TvdW commented Aug 23, 2024

What type of PR is this?

Feature, I guess?

What package or component does this PR mostly affect?

language/go
go_repository

What does this PR do? Why is it needed?

This is an attempt at making go_repository and its bzlmod equivalent pull Go modules as similar as possible to what go get would do: no proto generation, and no BUILD files.

In my experience, the bulk of Go modules that ship with BUILD files don't actually compile, so we need Gazelle to fix them for us. In general Gazelle does a great job at these, but sometimes they will contain genrules and other code meant for updating the sources, and in some cases Gazelle just can't fix those.

I previously added build_file_generation=clean for this, and it works great under gazelle_default_attributes in my local tests. So, this commit makes that the default. Gazelle's build file updating is great for local development, but it almost never makes sense for an external Go module pulled in via go_repository/go_deps.

The same thing applies to protobuf. Some module authors ship .proto files in their codebase, which is great! However, since Gazelle doesn't really implement dependency resolution for proto files (which is fair, because that's hard), we end up with a lot of gazelle:proto disable everywhere, because we import modules without using the proto files.

So, we can insert gazelle:proto disable by default for external repositories. When the proto targets are actually needed, they can be reinstated by passing either an empty directives list, or (more likely) by providing the dependency resolution rules.

The end result is that go_repository and go_deps now basically don't need special configuration or overrides anymore, as evidenced by my cleanup in default_gazelle_overrides.bzl.

Which issues(s) does this PR fix?

Fixes #1736
Closes #1799

... and a bunch more, because this is a very popular issue topic

Other notes for review

The commit, as-is, is backwards incompatible. I first want to gather whether people are aligned on this idea (to update the defaults) before trying to figure out how to make this backwards compatible (if needed).

@TvdW TvdW force-pushed the go-get-as-default branch 2 times, most recently from 6a4804e to 82029b0 Compare August 23, 2024 11:27
@TvdW
Copy link
Contributor Author

TvdW commented Aug 23, 2024

Bit of context: a recent Go module update pulled in a dependency-of-a-dependency that has BUILD files but doesn't actually build. go_deps.gazelle_override isn't allowed in this case (because it's an indirect dependency), so I had to make it an explicit dependency through go_deps.module() first. At this point I updated my gazelle_default_attributes, found that it actually works great, and so now I'd like to contribute that back to the community.

Copy link
Member

@fmeum fmeum left a comment

Choose a reason for hiding this comment

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

I also think that we should make this change. The next two rules_go releases will be somewhat breaking, so we would need to time this carefully.

specific_override = specific_overrides.get(path)
if specific_override and hasattr(specific_override, attribute_name):
if specific_override != None and hasattr(specific_override, attribute_name):
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for the explicit none check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An empty list is False, but we do want to consider that to be an override.

In the default list of overrides I'm adding

"github.com/bazelbuild/buildtools": [],

Since buildtools doesn't like proto:disable. However, the None check is needed since this is still considered False

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The above is partially incorrect: buildtools doesn't even have proto code used by gazelle. That said, the rest is still true: the None check is needed so that we can specify an empty list, which otherwise would get reverted back to the default (proto:disable).

"github.com/grpc-ecosystem/grpc-gateway/v2": "on",
"google.golang.org/grpc": "on",
"github.com/grpc-ecosystem/grpc-gateway/v2": "update",
"github.com/bazelbuild/buildtools": "update",
Copy link
Member

Choose a reason for hiding this comment

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

Your PR contains an override that switches this to off for WORKSPACE reps, is that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, no, I think that's accidental. It should be off, I believe. Will test it to see which one it was 😄

Copy link
Contributor Author

@TvdW TvdW Aug 28, 2024

Choose a reason for hiding this comment

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

Ok, found it.

Any of update, off, or clean will work here in a 'vanilla' bazel repo. The reason I set it to update and off is that my own code was importing from the buildtools repo, and since that was my test case I accidentally blindly copied that over. The reality is that the majority of users have no need for this and only have buildtools as a transient dep of gazelle and rules_go, so... I think it can go? 😄

Since this can never happen in a transient dependency anyway, I will update my PR to remove this.

This is an attempt at making `go_repository` and its bzlmod equivalent
pull Go modules as similar as possible to what `go get` would do: no
proto generation, and no BUILD files.

In my experience, the bulk of Go modules that ship with BUILD files
don't actually compile, so we need Gazelle to fix them for us. In
general Gazelle does a great job at these, but sometimes they will
contain `genrule`s and other code meant for updating the sources, and
in some cases Gazelle just can't fix those.

I previously added `build_file_generation=clean` for this, and it works
great under `gazelle_default_attributes` in my local tests. So, this
commit makes that the default. Gazelle's build file _updating_ is great
for local development, but it almost never makes sense for an external
Go module pulled in via go_repository/go_deps.

The same thing applies to protobuf. Some module authors ship .proto
files in their codebase, which is great! However, since Gazelle doesn't
really implement dependency resolution for proto files (which is fair,
because that's _hard_), we end up with a lot of `gazelle:proto disable`
everywhere, because we import modules without using the proto files.

So, we can insert `gazelle:proto disable` by default for external
repositories. When the proto targets are actually needed, they can be
reinstated by passing either an empty directives list, or (more likely)
by providing the dependency resolution rules.

The end result is that go_repository and go_deps now basically don't
need special configuration or overrides anymore, as evidenced by my
cleanup in `default_gazelle_overrides.bzl`.
@TvdW TvdW force-pushed the go-get-as-default branch from 9ca907f to 62181b1 Compare October 4, 2024 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proto generation is not yet disabled in go.mods provided by go_deps in bzlmod
2 participants