2

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?

abergmeier
  • 13,224
  • 13
  • 64
  • 120
Natan
  • 1,944
  • 1
  • 11
  • 16

2 Answers2

2

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

lberki
  • 141
  • 4
1

Natan,

If I understand correctly, you want to transitively propagate the information from each aspect node out into a single result. To do this, build the transitive set in your aspect rule implementation and pass it via the "provider" mechanism [^1]. I wrote up some examples on bazel aspects, perhaps you'll find it useful[^2].

  1. https://github.com/pcj/bazel_aspects/blob/master/aspects.bzl#L94-L104

  2. https://github.com/pcj/bazel_aspects

Paul
  • 595
  • 6
  • 6
  • Paul, The targets which the aspects visit are not part of a tree of dependencies. That is why i suggested to create a synthetic target which depends on all of the real targets, and then propagate the data up to it, as you suggested. My question was, whether creating synthetic targets is not a smell... – Natan May 01 '17 at 08:07
  • [example repo](https://github.com/natansil/bazel-learning/tree/master/bazel_aspect), for example, I want to visit all targets in m0, and find all their jars locations – Natan May 04 '17 at 12:37