bazel

Given 2 files and a build target, how to identify which is a dependency of which in Bazel?


I have a "file.py" and "other_file.py" that are both somehow a part of bazel run my:target. Is there a way to tell in the dependency tree would target of "file.py" depend on target of "other_file.py", or vise-versa, or would they both have a common ancestor.


Solution

  • You can use bazel query for this: https://bazel.build/query/guide

    E.g. for:

    py_binary(
      name = "main",
      srcs = ["main.py"],
      deps = ["file", "other_file"],
    )
    
    py_library(
      name = "file",
      srcs = ["file.py"],
      deps = [":other_file"],
    )
    
    py_library(
      name = "other_file",
      srcs = ["other_file.py"],
    )
    

    bazel query --notool_deps --noimplicit_deps "deps(main)" --output graph | dot -Tpng > /tmp/graph.png

    produces

    bazel query graph

    To do the reverse:

    bazel query --notool_deps --noimplicit_deps "rdeps(//..., other_file.py)" --output graph | dot -Tpng > /tmp/rdeps_graph.png

    enter image description here