bazel-contrib / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.21k stars 381 forks source link

Allow wrappers #18

Open jayconrod opened 6 years ago

jayconrod commented 6 years ago

Migrated from bazelbuild/rules_go#748

Allow people to mark rules as a wrapper for a go rule, so they can be managed by gazelle as if the were the underlying rule. It would be even nicer if you could do this once for an entire rule type rather than per rule. The original example was bazelbuild/rules_docker#119 where go_image is a proxy for go_binary as far as gazelle should be concerned.

achew22 commented 6 years ago

This would also be useful for bazel-integration-testing's bazel_go_integration_test

AFMiziara commented 3 years ago

This would be very useful also to allow gazelle to generate predefined go_image and container_push targets from rules_docker together with go_binary, instead of having to manually write those.

Any ideas on how to do that? Where to start? I would love to help.

achew22 commented 3 years ago

Have you tried

# gazelle:map_kind go_binary go_image @io_bazel_rules_docker//go:image.bzl

I've been using this in my repo for a long time to great success. Where it would have generated a go_binary it generates a go_image imported from @io_bazel_rules_docker//go:image.bzl.

AFMiziara commented 3 years ago

@achew22 I didn't now about that. Worked here, thanks a lot.

About container_push, do you know if there is a similar approach or for that case we have to manually write on all targets?

achew22 commented 3 years ago

What would the container_push wrap? If you want one next to every go_image you could make a macro that has the rules_docker go_image in addition to a container_push and then you'd get it at the same time. That said, macros that silently create resources that you probably want to run are suboptimal from a usability standpoint. It's very nice to be able to see all the targets that you care about explicitly listed in a BUILD.

You could pretty easily put together a Gazelle plugin to create a container_push automatically for every *_image that it finds.

AFMiziara commented 3 years ago

Thanks. Yes, that was my goal: to have container_push next to each go_image. I will try to write a Gazelle plugin. If I succeed I will post here. It might be useful, because I didn't find anything similar to that atm.

achew22 commented 3 years ago

If you're looking for some inspiration, there are a couple of simple plugins that are pretty easy to work through. The easiest is one that makes a filegroup for every file in the repo[0]. A slightly more involved one is the xlang[1] that you can use as a sample as well.

[0] https://github.com/bazelbuild/bazel-gazelle/blob/master/internal/language/test_filegroup/lang.go [1] https://github.com/bazelbuild/bazel-gazelle/blob/master/internal/gazellebinarytest/xlang.go

alex-torok commented 6 hours ago

I added a # gazelle:macro directive in #1986 to allow gazelle to recognize that a macro wraps an underlying rule, but not force all targets to become the macro like map_kind does.