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.




How do I find all the targets for a package?

Specify a build target pattern that represents the targets in the package.

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

The buck query command can accept a build target pattern as a parameter. If you specify a build target pattern, Buck evaluates this pattern and shows all the build targets that match it.

How do I get the attribute names and values for the targets returned by a query?

Add the --output-attributes option to the command line, followed by regular expressions that represent the attributes of interest.

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

The --output-attributes option enables you to specify which attributes Buck should return. Instead of returning the names of the targets that match the query expression, Buck returns the names and values of the specified attributes for those targets in JSON format. Attributes are specified as regular expressions. For example, '.*' matches all attributes. See the buck query page for more details. The output for the example query above might look something like the following.

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

How do I find the dependencies for a target?

Use the deps() operator.

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 specified targets. The first argument represents the targets of interest. This can be a single build target or build target pattern, or a set of these.

The optional second argument is the depth of the search for dependencies from the specified targets. For example, 1, as shown in the example above, returns only the direct dependencies. If you do not provide this argument, the output is the complete set of transitive dependencies.