Buck: Buck query - Cheat Sheet

Buck query - Cheat Sheet

Note: For a complete description of the buck query language with many additional examples, see the buck query reference page.

Q: How do I find the existing targets in a package?

buck query "//path/to/dir/..."

buck query supports build target patterns. By specifying just a build target pattern, the output will be the evaluation of this pattern, showing the build targets that match it.

Q: How do I get the content in the attributes of the targets resulting from a query?

buck query "deps(//foo:bar)" --output-attributes 'name' 'exported_headers'

The --output-attributes flag changes the output format. Instead of outputting the names of the targets that match the query expression, the output is composed by the specified attributes. Attributes are regular expressions (e.g. '.*' matches all attributes). See the buck query page for more details. Example:

  "//foo/bar/lib:lib" : {
    "exported_headers" : [ "App/util.h" ],
    "name" : "lib"
  "//foo/bar:app" : {
    "exported_headers" : [ "App/lib.h" ],
    "name" : "app"

Q: How do I find the dependencies of a target?

buck query "deps('//foo:bar')"
buck query "deps('//foo:bar', 1, first_order_deps())"
buck query "deps(set('//foo:bar' '//foo:lib' '//foo/baz:util'))"

The deps operator finds the dependencies of the given targets. The first argument represents the targets we are interested in. This can be a single target, a set of targets or a build target patterns.

The optional second argument is the depth of the search for dependencies from the given targets. If absent, the output will be all the transitive dependencies, whereas 1 gives only the direct dependencies.