bazelbuild / 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.15k stars 369 forks source link

Add support for generated files in gazelle #1801

Open mpatou opened 1 month ago

mpatou commented 1 month ago

What version of gazelle are you using?

0.35

What version of rules_go are you using?

0.46

What version of Bazel are you using?

6.5

Does this issue reproduce with the latest releases of all the above?

Yes

What operating system and processor architecture are you using?

x86-64

What did you do?

We are using counterfeiter to generate mocks structs for our unit tests. Currently gazelledo parse any generate comment so we have to generate the file manually and then run gazelle

What did you expect to see?

gazelle generates the targets to call the generator and then references those targets in other targets that use the modules.

What did you see instead?

gazelledoes nothing.

mpatou commented 1 month ago

I would like to discuss this a bit more as I'm thinking about trying to do something. I had a look at how language/go handle protobuf and I think this could be extended to support other generator. My current train of thought is that you would pass a configuration to gazelle that would map a given type of generator to the macro to call to to the generation:

{
 "counterfeiter": {
   "macro": "go_generate_counterfeiter",
   "macro_path": "@//bazel:generator.bzl"
   },
 "cue": {
   "macro": "go_generate_cue",
   "macro_path": "@//bazel:generator.bzl",
   "package": "github.com/company/repo/go/cluster"
 ...
 }

Package name would be either inferred from the package where the code with the comment is contained or if specified in the configuration it would use the one specified.