A Gazelle extension featuring LifecycleManager

One of the biggest caveats of working with Bazel's tool Gazelle is the lack of up to date documentation. Yes, the tool is still at version 0.x, but the docs are quite obsolete: often they don't mention few features, there are missing configuration settings, and in general there is a lack of examples of how to use anything other than directives. There is even a "hidden tool", autogazelle, not even mentioned in the main README. [1]

In the end, what always works is reading the source code... and when present, the tests. And it is while checking Gazelle's tests (searching for an example of using the LifecycleManager extensions interface), that I found a nice little test with some special properties; The test is test_load_for_packed_rules (located here), and I think it is relevant because:

  • It showcases how to build a tiny Gazelle extension. It is almost empty, but all the critical parts are present
  • Despite beeing small, it shows all the relevant interface methods you should implement for a valid/complete extension. It does use the now deprecated Loads() instead of Bzlmod's ApparentLoads(), but Bzlmod needs to mature a bit so not a big deal (yet)
  • And finally, it includes a working example of LifecycleManager, which is what I was looking for

Once you have seen an example, it becomes trivial to add support of the new lifecycle interface functions to an existing extension, but at least for a non-Gopher like me, that first sample was helpful.

[1] But, if you find it, it contains its own README.

Tags: Bazel Development Gazelle Go

A Gazelle extension featuring LifecycleManager article, written by Kartones. Published @