Title: A Gazelle extension featuring LifecycleManager
Slug: gazelle-extension-featuring-lifecyclemanager
Date: 2024-03-12 22:15:00
Author: Kartones
Lang: en
Tags: Development, Bazel, Gazelle, Go
og_image:
Description: A brief mention of relevant Bazel's Gazelle source code test and logic to manage the lifecycle inside extensions.

One of the biggest caveats of working with Bazel's tool [Gazelle](https://github.com/bazelbuild/bazel-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](https://github.com/bazelbuild/bazel-gazelle/blob/f590ca1488eff5063a9549a3edba8266a6ce6134/internal/language/test_load_for_packed_rules/lang.go)), 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()](https://github.com/bazelbuild/bazel-gazelle/blob/f590ca1488eff5063a9549a3edba8266a6ce6134/language/lang.go#L92-L93) instead of Bzlmod's [ApparentLoads()](https://github.com/bazelbuild/bazel-gazelle/blob/f590ca1488eff5063a9549a3edba8266a6ce6134/language/lang.go#L137), 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](https://github.com/bazelbuild/bazel-gazelle/blob/f590ca1488eff5063a9549a3edba8266a6ce6134/cmd/autogazelle/README.rst).