Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregate filegroups in subpackages into a large filegroup in bazel?

Tags:

bazel

I have a parent directory foo, and child directories bar, baz, and qux. All four directories contain a bazel BUILD file and define filegroup rules that contain all files in the subdirectory (plus various other rules). The problem with this is that the filegroup in the parent directory foo cannot use a glob to ensure that all files are included (because globs do not cross package boundaries). Instead, I'm currently manually listing all of the children's rules as srcs in foo, and this manual listing is error-prone because when another child of foo is added, the author must remember to add it to the srcs of foo. I tried to make some progress solving this problem by looking at adding a genquery rule in foo (thinking I could somehow extract a list of srcs from this programmatically at build time), but recursive patterns are not allowed in the expression of a genquery rule, so this was unsuccessful.

What is the least mistake-prone way of creating such a filegroup? Is there anything better than my current manual construction of it by listing srcs?

like image 449
jonderry Avatar asked Oct 27 '18 01:10

jonderry


1 Answers

The src of a filegroup is a list of labels.

Hence, you can (and should) do

filegroup(
  name = "foo_supergroup",
  srcs = [
     "//foo/bar:smallergroup",
     "//foo/baz:smallergroup",
     "//foo/qux:smallergroup",
  ],
)

Edit: You can then add a presubmit check that these dependencies are the same as the subgroups.

For, this purpose, I suggest you introduce a tag "yeah": foo/BUILD contains

filegroup(
  name = "smallergroup",
  srcs = glob(["*.txt"]),
  tags = ["yeah"],
)

Thanks to this:

blaze query 'attr("tags", ".*yeah.*", deps(//foo/...))'
//foo/bar:smallergroup
//foo/baz:smallergroup
//foo/qux:smallergroup

It then becomes easy to compare with the sources of the supergroup:

blaze query 'deps(//foo:foo_supergroup, 1)'
//foo:foo_supergroup
//foo/bar:smallergroup
//foo/baz:smallergroup
//foo/qux:smallergroup

In fact, you don't need a specific presubmit. You can use a sh_test (using command diff) to compare the output of this two blaze queries made with gen_query

like image 180
rds Avatar answered Nov 24 '22 06:11

rds