how to add more data to each node of bazel query

35 Views Asked by At

I wanted to see all of my dependency graph for a bazel target, for that I can use below query

bazelisk query --notool_deps --noimplicit_deps "deps(//sample-target:sample-target)" --output graph

this will show me the dependency graph,

now I wanted to show more data on each node, for example if a node/target has attribute tags I wanted to show all tags there

is this possible?

1

There are 1 best solutions below

0
ahumesky On

Filtering by tag can be done with the attr() function:

https://bazel.build/query/language#attr

But I don't think there's a direct way to print all the tags. A way around this is to use another output format like --output=streamed_jsonproto which will include the attributes of the targets among other things.

For example:

genrule(
  name = "gen",
  outs = ["gen.txt"],
  cmd = "touch $@",
  tags = ["foo", "bar"],
)

bazel query --notool_deps --noimplicit_deps "gen" --output=streamed_jsonproto will include:

{
  "type": "RULE",
  "rule": {
    "name": "//:gen",
    "ruleClass": "genrule",
    "attribute": [
      ...
      {
        "name": "tags",
        "type": "STRING_LIST",
        "stringListValue": [
          "bar",
          "foo"
        ],
        "explicitlySpecified": true,
        "nodep": false
      },
      ...
    ],
    ...
  }
}

Or if you feel like getting into it, the code to generate the graph output is here and it might not be too difficult to have it add the tags to the node text:

https://github.com/bazelbuild/bazel/blob/3713606de9aa5d85f71cc07d466b911d79024183/src/main/java/com/google/devtools/build/lib/query2/query/output/GraphOutputWriter.java#L167