|
|
# Bazel Aspects
|
|
|
Notice: This article is under construction, some sections may not be complete. This notice will be removed as it exits it's initial draft.
|
|
|
_____
|
|
|
[Aspects](https://docs.bazel.build/versions/master/skylark/aspects.html) are a relatively unknown feature of Bazel that can be extremely useful in the right circumstances. At a basic level, they mirror the dependency graph of a regular rule, in order to run extra jobs in parallel.
|
|
|
|
|
|
## Use Cases
|
|
|
One area where this is extremely useful is for auto-generation of source code, such as a rule to compile protobuf files into language specific source code. If this were to be implemented using a normal rule, it would have to collect all of the proto files from its dependencies, then loop through each one to create the source code in the rule's output directory.
|
|
|
|
|
|
This works fine at first glance, but integrating this with other rules causes a few issues. The most problematic one is that if two proto files both depend on the same helper proto, Bazel will throw an error during the analysis phase saying `ERROR: file '<filename>' is generated by these conflicting actions:`
|
|
|
|
|
|
Since each instance of the rule must generate the source code separately, they will conflict when trying to build the same files. The best solution for this is to use an aspect instead. The functionality of aspects can be leveraged to mirror the dependency graph of the native `proto_library` rules into a parallel graph that builds each `proto_library` into individual source code files. After this, a regular rule can be created that executes this aspect on its dependencies, then collects the source code and runs the library compilation. Now, instead of having each top level rule generate its own source files, this work is passed on to the aspect, avoiding duplication. |
|
|
\ No newline at end of file |