Search code examples
bazelbazel-aspect

Recommended strategy to accumulate data in bazel aspects output files


I'm writing a post-build tool that needs the location of a list of target's jar files. For these locations I have an aspect that runs on a list of targets (separately for each target using --aspects) and fetch the jar file path for each of them.

I've managed to get each jar file path in a custom output file (e.g. jar.txt) in each target's output folder.

But this will mean I would need to go over each jar.txt file separately to get the location.

Is there a way to accumulate the jar files paths in a single file?
Something like:

  1. Try and write to the same output folder with append command in the aspect. I'm not sure if a shared output folder is possible.

  2. Create a synthetic target which depends on all the relevant targets, then run an aspect on this target and accumulate the jars and only write them at the root after the recursion is back.

Are 1. or 2. valid options?
What is the recommended strategy to accumulate data in bazel aspects output files?


Solution

  • Bazel doesn't provide facitlities in Skylark for accumulating information between targets that are not related to each other in the target graph (e.g. ones that are mentioned on the command line next to each other).

    One possibility would be to write a Skylark rule that depends on all the targets you usually mention on the command line and built that one; that rule will be able to collate the classpaths from each Java target to a single file.

    Another possibility is to tell Bazel to write build events (that includes all the outputs of all targets the specified build pattern expands to) to a file using the --experimental_build_event_{json,text,binary}_file. (The "experimental" will be removed soon.). The files contain instances of this message:

    https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/buildeventstream/proto/build_event_stream.proto