bazelbazel-javabazel-query

How do I query only for the dependencies declared under deps in the BUILD file?


How do I query only for the dependencies declared under deps?

For example, given the given target

java_library(
    name = "my-library",
    srcs = glob(["src/main/java/**/*.java"]),
    deps = [
        ":some-target",
        "//some-package:another-target",
        artifact("some-maven:some-artifact"),
        artifact("another-maven:another-artifact"),
    ],
)

How do I query just the dependencies directly defined in deps? That is,

":some-target",
"//some-package:another-target",
artifact("some-maven:some-artifact"),
artifact("another-maven:another-artifact"),

bazel query deps(my-library) returns me many other dependencies I am not interested in (e.g. source files, platform targets, other internal targets, etc.).


Solution

  • Download buildozer from here. Add buildozer to your environment path.

    buildozer "print deps" "//some:target"
    

    This will list what can be found in the deps attribute, e.g.:

    [
        ":main_qrc",
        ":main_window",
        ":resource2",
        "//okapi:print_version",
        "@rules_qt//:qt_core",
        "@rules_qt//:qt_qml",
        "@rules_qt//:qt_widgets",
        "//okapi:command_line_arguments",
    ] + select({
        ":windows_x86_64": [":win_resources"],
        "//conditions:default": [],
    })
    

    Builddozer just gives you a text copy of what can be found in the deps attribute. There will be no transitive dependencies and things like selectes will not be resolved.

    In contrast using Bazel's cquery you can get all dependencies including transitive ones without source files this way:

    bazel cquery --config=your_config 'deps(//some:target) except kind("source file", deps(//some:target))'