blob: 605c15910a187f60839f238f8492b5586bc77a01 [file] [log] [blame]
David Chen8fe82a32016-08-24 10:55:41 +00001---
2layout: documentation
3title: Query Language
4---
5<h1>The Bazel Query Reference</h1>
6
gregceffb8a982018-02-12 15:34:00 -08007
dannark5c4e1242018-03-06 07:25:28 -08008<ul class="toc">
dannarkc0abf242018-03-12 05:59:44 -07009 <li><a href="query.html#examples">Examples</a></li>
10 <li><a href="query.html#tokens">Tokens: The Lexical Syntax</a></li>
11 <li><a href="query.html#concepts">Bazel Query Language Concepts</a></li>
12 <li><a href="query.html#expressions">Expressions: Syntax and Semantics of the Grammar</a></li>
13 <li><a href="query.html#functions">Functions</a></li>
14 <li><a href="query.html#output-formats">Output Formats</a></li>
gregceffb8a982018-02-12 15:34:00 -080015</ul>
16
David Chen8fe82a32016-08-24 10:55:41 +000017<p>
18 When you use <code>bazel query</code> to analyze build
19 dependencies, you use a little language, the <em>Bazel Query
20 Language</em>. This document is the reference manual for that
21 language. This document also describes the output
22 formats <code>bazel query</code> supports.
23</p>
24
juliexxia548c3952018-03-15 09:23:34 -070025<p>
26 If you were looking for a how-to, please see the <a href='query-how-to.md'>Bazel Query How-To</a>.
27</p>
28<h3> bazel cquery </h3>
29<p>Traditional bazel query runs on the post-loading phase target graph and therefore
30 has no concept of configurations and their related concepts. Notably, this means it does not
31 correctly resolve
32 <a href = "https://docs.bazel.build/versions/master/be/functions.html#select">select statements</a>
33 and rather returns all possible resolutions of selects. A newer query environment, cquery,
34 properly handles configurations but doesn't provide all of the functionality of this original
35 query. For more information, see the <a href="cquery.html">Bazel cquery reference</a>.
36</p>
gregceffb8a982018-02-12 15:34:00 -080037<h2 id='examples'>Examples</h2>
David Chen8fe82a32016-08-24 10:55:41 +000038
39<p>
40 How do people use <code>bazel query</code>? Here are typical examples:
41</p>
42
43<p>
44 Why does the <code>//foo</code> tree depend on <code>//bar/baz</code>?
45 Show a path:</p>
46 <pre>somepath(foo/..., //bar/baz:all)</pre>
47
48
49<p>
50 What C++ libraries do all the <code>foo</code> tests depend on that
51 the <code>foo_bin</code> target does not?</p>
52 <pre>kind("cc_library", deps(kind(".*test rule", foo/...)) except deps(//foo:foo_bin))</pre>
53
54
gregceffb8a982018-02-12 15:34:00 -080055<h2 id='tokens'>Tokens: The Lexical Syntax</h2>
David Chen8fe82a32016-08-24 10:55:41 +000056
57<p>
58 Expressions in the query language are composed of the following
59 tokens:</p>
60 <ul>
61 <li>
62 <p>
Googler020ef072017-07-18 10:47:01 +020063 <b>Keywords</b>, such as <code>let</code>. Keywords are the reserved words of the
David Chen8fe82a32016-08-24 10:55:41 +000064 language, and each of them is described below. The complete set
65 of keywords is:
66 </p>
67
68<code><!-- keep this alphabetically sorted -->
David Chen8fe82a32016-08-24 10:55:41 +000069<a href="#set-operations">except</a><br/>
David Chen8fe82a32016-08-24 10:55:41 +000070<a href="#variables">in</a><br/>
71<a href="#set-operations">intersect</a><br/>
David Chen8fe82a32016-08-24 10:55:41 +000072<a href="#variables">let</a><br/>
David Chen8fe82a32016-08-24 10:55:41 +000073<a href="#set">set</a><br/>
David Chen8fe82a32016-08-24 10:55:41 +000074<a href="#set-operations">union</a><br/>
75</code>
76 </li>
77
78 <li>
79 <p>
80 <b>Words</b>, such as <code>foo/...</code> or
81 <code>".*test rule"</code> or
82 <code>//bar/baz:all</code>.
83 If a character sequence is "quoted" (begins and ends with a
84 single-quote <code>'</code>, or begins and ends with a
85 double-quote <code>&quot;</code>), it is a word.
86 If a character sequence is not quoted, it may still be parsed as a word.
87 Unquoted words are sequences of characters drawn from
88 the set of alphabet characters, numerals, slash <code>/</code>,
89 hyphen <code>-</code>, underscore <code>_</code>, star <code>*</code>, and
90 period <code>.</code>. Unquoted words may not start with a
91 hyphen or period.
92 </p>
93
94 <p>We chose this syntax so that quote marks aren't needed in most cases.
95 The (unusual) <code>".*test rule"</code> example needs quotes: it
96 starts with a period and contains a space.
97 Quoting <code>"cc_library"</code> is unnecessary but harmless.
98 </p>
99
100 <p>
101 Quoting <em>is</em> necessary when writing scripts that
102 construct Bazel query expressions from user-supplied values.
103
104 </p>
105 <pre>
106 //foo:bar+wiz # WRONG: scanned as //foo:bar + wiz.
107 //foo:bar=wiz # WRONG: scanned as //foo:bar = wiz.
108 "//foo:bar+wiz" # ok.
109 "//foo:bar=wiz" # ok.
110 </pre>
111 <p>
112 Note that this quoting is in addition to any quoting that may
113 be required by your shell. e.g.
114 </p>
laszlocsomor79251002017-04-03 13:09:47 +0000115 <pre>bazel query ' "//foo:bar=wiz" ' # single-quotes for shell, double-quotes for Bazel.</pre>
David Chen8fe82a32016-08-24 10:55:41 +0000116
117 <p>
118 Keywords, when quoted, are treated as ordinary words, thus
119 <code>some</code> is a keyword but <code>"some"</code> is a word.
120 Both <code>foo</code> and <code>"foo"</code> are words.
121 </p>
122
123 <li><b>Punctuation</b>, such as parens <code>()</code>, period
124 <code>.</code> and comma <code>,</code>, etc. Words containing
125 punctuation (other than the exceptions listed above) must be quoted.
126 </ul>
127
128<p>
129 Whitespace characters outside of a quoted word are ignored.
130</p>
131
132<h2 id='concepts'>Bazel Query Language Concepts</h2>
133<p>
134 The Bazel query language is a language of expressions. Every
135 expression evaluates to a <b>partially-ordered set</b> of targets,
136 or equivalently, a <b>graph</b> (DAG) of targets. This is the only
137 datatype.
138</p>
139<p>
140 In some expressions, the partial order of the graph is
141 not interesting; In this case, we call the values
142 "sets". In cases where the partial order of elements
143 is significant, we call values "graphs". Note
144 that both terms refer to the same datatype, but merely emphasize
145 different aspects of it.
146</p>
147
148<h3>Cycles in the dependency graph</h3>
149<p>
150 Build dependency graphs should be acyclic.
151
152 The algorithms used by the query language are intended for use in
153 acyclic graphs, but are robust against cycles. The details of how
154 cycles are treated are not specified and should not be relied upon.
155</p>
156
157<h3 id='implicit_deps'>Implicit dependencies</h3>
158
159<p>
160 In addition to build dependencies that are defined explicitly in BUILD files,
161 Bazel adds additional <em>implicit</em> dependencies to rules. For example
162 every Java rule implicitly depends on the JavaBuilder. Implicit dependencies
163 are established using attributes that start with <code>$</code> and they
164 cannot be overridden in BUILD files.
165
166</p>
167
168<p>
169 Per default <code>bazel query</code> takes implicit dependencies into account
170 when computing the query result. This behavior can be changed with
171 the <code>--[no]implicit_deps</code> option.
172</p>
173
174<h3 id='soundness'>Soundness</h3>
175
176<p>
177 Bazel query language expressions operate over the build
178 dependency graph, which is the graph implicitly defined by all
179 rule declarations in all BUILD files. It is important to understand
180 that this graph is somewhat abstract, and does not constitute a
181 complete description of how to perform all the steps of a build. In
182 order to perform a build, a <em>configuration</em> is required too;
dmartinge4262e32017-09-07 16:54:44 +0200183 see the <a href='user-manual.html#configurations'>configurations</a>
David Chen8fe82a32016-08-24 10:55:41 +0000184 section of the User's Guide for more detail.
185</p>
186
187<p>
188 The result of evaluating an expression in the Bazel query language
189 is true <em>for all configurations</em>, which means that it may be
190 a conservative over-approximation, and not exactly precise. If you
191 use the query tool to compute the set of all source files needed
192 during a build, it may report more than are actually necessary
193 because, for example, the query tool will include all the files
194 needed to support message translation, even though you don't intend
195 to use that feature in your build.
196</p>
197
198<h3 id='graph-order'>On the preservation of graph order</h3>
199
200<p>
201 Operations preserve any ordering
202 constraints inherited from their subexpressions. You can think of
203 this as "the law of conservation of partial order". Consider an
204 example: if you issue a query to determine the transitive closure of
205 dependencies of a particular target, the resulting set is ordered
206 according to the dependency graph. If you filter that set to
207 include only the targets of <code>file</code> kind, the same
208 <em>transitive</em> partial ordering relation holds between every
209 pair of targets in the resulting subset&mdash;even though none of
210 these pairs is actually directly connected in the original graph.
211 (There are no file&ndash;file edges in the build dependency graph).
212</p>
213
214<p>
215 However, while all operators <em>preserve</em> order, some
216 operations, such as the <a href='#set-operations'>set operations</a>
217 don't <em>introduce</em> any ordering constraints of their own.
218 Consider this expression:
219</p>
220
221<pre>deps(x) union y</pre>
222
223<p>
224 The order of the final result set is guaranteed to preserve all the
225 ordering constraints of its subexpressions, namely, that all the
226 transitive dependencies of <code>x</code> are correctly ordered with
227 respect to each other. However, the query guarantees nothing about
228 the ordering of the targets in <code>y</code>, nor about the
229 ordering of the targets in <code>deps(x)</code> relative to those in
230 <code>y</code> (except for those targets in
231 <code>y</code> that also happen to be in <code>deps(x)</code>).
232</p>
233
234<p>
235 Operators that introduce ordering constraints include:
236 <code>allpaths</code>,
237 <code>deps</code>,
238 <code>rdeps</code>,
239 <code>somepath</code>,
240 and the target pattern wildcards
241 <code>package:*</code>,
242 <code>dir/...</code>, etc.
243</p>
244
janakrfd9e5a22017-12-06 09:59:52 -0800245<h3 id='sky-query'>Sky Query</h3>
246
247<p>
248 Query has two different implementations, with slightly different features. The alternative one is
249 called "Sky Query", and is activated by passing the following two flags:
250 <code>--universe_scope</code> and <code>--order_output=no</code>.
251 <code>--universe_scope=&lt;target_pattern1&gt;,...,&lt;target_patternN&gt;</code> tells query to
252 preload the transitive closure of the target pattern specified by the target patterns, which can
253 be both additive and subtractive. All queries are then evaluated in this "scope". In particular,
254 the <a href='#allrdeps'><code>allrdeps</code></a> and
255 <a href='#rbuildfiles'><code>rbuildfiles</code></a> operators only return results from this scope.
256</p>
257
258<p>
259 Sky Query has some advantages and disadvantages compared to the default query. The main
260 disadvantage is that it cannot order its output according to graph order, and thus certain
261 <a href='#output-formats'>output formats</a> are forbidden. Its advantages are that it provides
262 two operators (<a href='#allrdeps'><code>allrdeps</code></a> and
263 <a href='#rbuildfiles'><code>rbuildfiles</code></a>) that are not available in the default query.
264 As well, Sky Query does its work by introspecting the
265 <a href='https://bazel.build/designs/skyframe.html'>Skyframe</a> graph, rather than creating a new
266 graph, which is what the default implementation does. Thus, there are some circumstances in which
267 it is faster and uses less memory.
268</p>
269
gregceffb8a982018-02-12 15:34:00 -0800270<h2 id='expressions'>Expressions: Syntax and Semantics of the Grammar</h2>
David Chen8fe82a32016-08-24 10:55:41 +0000271
272<p>
273 This is the grammar of the Bazel query language, expressed in EBNF
274 notation:
275</p>
276
277
278<pre>expr ::= <var>word</var>
279 | let <var>name</var> = <var>expr</var> in <var>expr</var>
280 | (<var>expr</var>)
281 | <var>expr</var> intersect <var>expr</var>
282 | <var>expr</var> ^ <var>expr</var>
283 | <var>expr</var> union <var>expr</var>
284 | <var>expr</var> + <var>expr</var>
285 | <var>expr</var> except <var>expr</var>
286 | <var>expr</var> - <var>expr</var>
David Chen8fe82a32016-08-24 10:55:41 +0000287 | set(<var>word</var> *)
lberkiaf70ce52017-06-12 18:15:29 +0200288 | <var>word</var> '(' <var>int</var> | <var>word</var> | <var>expr</var> ... ')'
David Chen8fe82a32016-08-24 10:55:41 +0000289</pre>
290
291<p>
292 We will examine each of the productions of this grammar in order.
293</p>
294
295<h3 id="target-patterns">Target patterns</h3>
296<pre>expr ::= <var>word</var></pre>
297<p>
298 Syntactically, a <em>target pattern</em> is just a word. It
299 is interpreted as an (unordered) set of targets. The simplest
300 target pattern is a label,
301 which identifies a single target (file or rule). For example, the
302 target pattern <code>//foo:bar</code> evaluates to a set
303 containing one element, the target, the <code>bar</code>
304 rule.
305</p>
306
307<p>
308 Target patterns generalize labels to include wildcards over packages
309 and targets. For example, <code>foo/...:all</code> (or
310 just <code>foo/...</code>) is a target pattern that evaluates to a
311 set containing all <em>rules</em> in every package recursively
312 beneath the <code>foo</code> directory;
313 <code>bar/baz:all</code> is a target pattern that
314 evaluates to a set containing all the rules in the
315 <code>bar/baz</code> package, but not its subpackages.
316</p>
317
318<p>
319 Similarly, <code>foo/...:*</code> is a target pattern that evaluates
320 to a set containing all <em>targets</em> (rules <em>and</em> files) in
321 every package recursively beneath the <code>foo</code> directory;
322 <code>bar/baz:*</code> evaluates to a set containing
323 all the targets in the
324 <code>bar/baz</code> package, but not its subpackages.
325</p>
326
327<p>
328 Because the <code>:*</code> wildcard matches files as well as rules,
329 it is often more useful than <code>:all</code> for queries.
330 Conversely, the <code>:all</code> wildcard (implicit in target
331 patterns like <code>foo/...</code>) is typically more useful for
332 builds.
333</p>
334
335<p>
336 <code>bazel query</code> target patterns work the same as
337 <code>bazel build</code> build targets do;
338 refer to <a href='bazel-user-manual.html#target-patterns'>Target Patterns</a>
339 in the Bazel User Manual for further details, or type <code>bazel
340 help target-syntax</code>.
341
342</p>
343
344<p>
345 Target patterns may evaluate to a singleton set (in the case of a
346 label), to a set containing many elements (as in the case of
347 <code>foo/...</code>, which has thousands of elements) or to the
348 empty set, if the target pattern matches no targets.
349</p>
350
351<p>
352 All nodes in the result of a target pattern expression are correctly
353 ordered relative to each other according to the dependency relation.
354 So, the result of <code>foo:*</code> is not just the set of targets
355 in package <code>foo</code>, it is also the <em>graph</em> over
356 those targets. (No guarantees are made about the relative ordering
357 of the result nodes against other nodes.) See the section
358 on <a href='#graph-order'>graph order</a> for more details.
359</p>
360
361<h3 id="variables">Variables</h3>
362<pre>expr ::= let <var>name</var> = <var>expr</var><sub>1</sub> in <var>expr</var><sub>2</sub>
363 | <var>$name</var></pre>
364<p>
365 The Bazel query language allows definitions of and references to
366 variables. The
367 result of evaluation of a <code>let</code> expression is the same as
368 that of <var>expr</var><sub>2</sub>, with all free occurrences of
369 variable <var>name</var> replaced by the value of
370 <var>expr</var><sub>1</sub>.
371</p>
372
373<p>
374 For example, <code>let v = foo/... in allpaths($v, //common)
375 intersect $v</code> is equivalent to the <code>allpaths(foo/...,
376 //common) intersect foo/...</code>.
377</p>
378
379<p>
380 An occurrence of a variable reference <code>name</code> other than in
381 an enclosing <code>let <var>name</var> = ...</code> expression is an
382 error. In other words, toplevel query expressions cannot have free
383 variables.
384</p>
385
386<p>
387 In the above grammar productions, <code>name</code> is like
388 <em>word</em>, but with the additional constraint that it be a legal
389 identifier in the C programming language. References to the variable
390 must be prepended with the "$" character.
391</p>
392
393<p>
394 Each <code>let</code> expression defines only a single variable,
395 but you can nest them.
396</p>
397
398<p>
399 (Both <a
400 href='#target-patterns'>target patterns</a> and variable references
401 consist of just a single token, a word, creating a syntactic
402 ambiguity. However, there is no semantic ambiguity, because the
403 subset of words that are legal variable names is disjoint from the
404 subset of words that are legal target patterns.)
405</p>
406
407<p>
408 (Technically speaking, <code>let</code> expressions do not increase
409 the expressiveness of the query language: any query expressible in
410 the language can also be expressed without them. However, they
411 improve the conciseness of many queries, and may also lead to more
412 efficient query evaluation.)
413</p>
414
415<h3 id="parentheses">Parenthesized expressions</h3>
416<pre>expr ::= (<var>expr</var>)</pre>
417
418<p>
419 Parentheses associate subexpressions to force an
420 order of evaluation.
421 A parenthesized expression evaluates
422 to the value of its argument.
423</p>
424
425<h3 id="set-operations">Algebraic set operations:
426 intersection, union, set difference</h3>
427
428<pre>expr ::= <var>expr</var> intersect <var>expr</var>
429 | <var>expr</var> ^ <var>expr</var>
430 | <var>expr</var> union <var>expr</var>
431 | <var>expr</var> + <var>expr</var>
432 | <var>expr</var> except <var>expr</var>
433 | <var>expr</var> - <var>expr</var>
434</pre>
435
436<p>
437 These three operators compute the usual set operations over their
438 arguments. Each operator has two forms, a nominal form such
439 as <code>intersect</code> and a symbolic form such
440 as <code>^</code>. Both forms are equivalent;
441 the symbolic forms are quicker to type. (For clarity, the rest of
442 this manual uses the nominal forms.) For example,
443</p>
444
445<pre>foo/... except foo/bar/...</pre>
446
447 evaluates to the set of targets that match
448 <code>foo/...</code> but not
449 <code>foo/bar/...</code>&nbsp;. Equivalently:
450
451<pre>foo/... - foo/bar/...</pre>
452
453 The <code>intersect</code> (<code>^</code>)
454 and <code>union</code> (<code>+</code>) operations are commutative
455 (symmetric); <code>except</code> (<code>-</code>) is
456 asymmetric. The parser treats all three operators as
457 left-associative and of equal precedence, so you might want parentheses.
458 For example, the first two of these expressions are
459 equivalent, but the third is not:
460
461<pre>x intersect y union z
462(x intersect y) union z
463x intersect (y union z)</pre>
464
465<p>
466 (We strongly recommend that you use parentheses where there is
467 any danger of ambiguity in reading a query expression.)
468</p>
469
470<h3 id="set">Read targets from an external source: set</h3>
471<pre>expr ::= set(<var>word</var> *) </pre>
472<p>
473 The <code>set(<var>a</var> <var>b</var> <var>c</var> ...)</code>
474 operator computes the union of a set of zero or
475 more <a href='#target-patterns'>target patterns</a>, separated by
476 whitespace (no commas).
477</p>
478
479<p>
480 In conjunction with the Bourne shell's <code>$(...)</code>
481 feature, <code>set()</code> provides a means of saving the results
482 of one query in a regular text file, manipulating that text file
483 using other programs (e.g. standard UNIX shell tools), and then
484 introducing the result back into the query tool as a value for
485 further processing. For example:
486</p>
487<pre>
laszlocsomor79251002017-04-03 13:09:47 +0000488 bazel query deps(//my:target) --output=label | grep ... | sed ... | awk ... &gt; foo
489 bazel query "kind(cc_binary, set($(&lt;foo)))"
David Chen8fe82a32016-08-24 10:55:41 +0000490</pre>
491<p>
492 In the next example, <code>kind(cc_library,
493 deps(//some_dir/foo:main, 5))</code> is effectively computed
494 by filtering on the <code>maxrank</code> values using
495 an <code>awk</code> program.
496</p>
497<pre>
laszlocsomor79251002017-04-03 13:09:47 +0000498 bazel query 'deps(//some_dir/foo:main)' --output maxrank |
David Chen8fe82a32016-08-24 10:55:41 +0000499 awk '($1 &lt; 5) { print $2;} ' &gt; foo
laszlocsomor79251002017-04-03 13:09:47 +0000500 bazel query "kind(cc_library, set($(&lt;foo)))"
David Chen8fe82a32016-08-24 10:55:41 +0000501</pre>
502<p>
503 In these examples, <code>$(&lt;foo)</code> is a shorthand
504 for <code>$(cat foo)</code>, but shell commands other
505 than <code>cat</code> may be used too&mdash;such as
506 the previous <code>awk</code> command.
507</p>
508
509<p>
510 Note, <code>set()</code> introduces no graph ordering constraints,
511 so path information may be lost when saving and reloading sets of
512 nodes using it. See the <a href='#graph-order'>graph order</a>
513 section below for more detail.
514</p>
515
gregceffb8a982018-02-12 15:34:00 -0800516<h2 id="functions">Functions</h2>
lberkiaf70ce52017-06-12 18:15:29 +0200517<pre>expr ::= <var>word</var> '(' <var>int</var> | <var>word</var> | <var>expr</var> ... ')'</pre>
518<p>
519 The query language defines several functions. The name of the function
520 determines the number and type of arguments it requires. The following
521 functions are available:
522</p>
523
524<code><!-- keep this alphabetically sorted -->
525<a href="#path-operators">allpaths</a><br/>
526<a href="#attr">attr</a><br/>
527
528<a href="#buildfiles">buildfiles</a><br/>
janakrfd9e5a22017-12-06 09:59:52 -0800529<a href="#rbuildfiles">rbuildfiles</a><br/>
lberkiaf70ce52017-06-12 18:15:29 +0200530
531<a href="#deps">deps</a><br/>
532<a href="#filter">filter</a><br/>
533<a href="#kind">kind</a><br/>
534<a href="#labels">labels</a><br/>
535<a href="#loadfiles">loadfiles</a><br/>
536<a href="#rdeps">rdeps</a><br/>
janakrfd9e5a22017-12-06 09:59:52 -0800537<a href="#allrdeps">allrdeps</a><br/>
nharmata50f72492017-08-11 21:31:03 +0200538<a href="#siblings">siblings</a><br/>
lberkiaf70ce52017-06-12 18:15:29 +0200539<a href="#some">some</a><br/>
540<a href="#path-operators">somepath</a><br/>
541<a href="#tests">tests</a><br/>
Googlerd1c9bec2017-10-26 17:35:59 +0200542<a href="#visible">visible</a><br/>
lberkiaf70ce52017-06-12 18:15:29 +0200543</code>
544
David Chen8fe82a32016-08-24 10:55:41 +0000545<h3 id="deps">Transitive closure of dependencies: deps</h3>
546<pre>expr ::= deps(<var>expr</var>)
547 | deps(<var>expr</var>, <var>depth</var>)</pre>
548<p>
549 The <code>deps(<var>x</var>)</code> operator evaluates to the graph
550 formed by the transitive closure of dependencies of its argument set
551 <var>x</var>. For example, the value of <code>deps(//foo)</code> is
552 the dependency graph rooted at the single node <code>foo</code>,
553 including all its dependencies. The value of
554 <code>deps(foo/...)</code> is the dependency graphs whose roots are
555 all rules in every package beneath the <code>foo</code> directory.
556 Please note that 'dependencies' means only rule and file targets
Googler151717c2018-07-02 11:54:11 -0700557 in this context, therefore the BUILD and Skylark files needed to
David Chen8fe82a32016-08-24 10:55:41 +0000558 create these targets are not included here. For that you should use the
559 <a href="#buildfiles"><code>buildfiles</code></a> operator.
560</p>
561
562<p>
563 The resulting graph is ordered according to the dependency relation.
564 See the section on <a href='#graph-order'>graph order</a> for more
565 details.
566</p>
567
568<p>
569 The <code>deps</code> operator accepts an optional second argument,
570 which is an integer literal specifying an upper bound on the depth
571 of the search. So <code>deps(foo:*, 1)</code> evaluates to all the
572 direct prerequisites of any target in the <code>foo</code> package,
573 and <code>deps(foo:*, 2)</code> further includes the nodes directly
574 reachable from the nodes in <code>deps(foo:*, 1)</code>, and so on.
575 (These numbers correspond to the ranks shown in
576 the <a href='#output-ranked'><code>minrank</code></a> output
577 format.) If the <var>depth</var> parameter is omitted, the search
578 is unbounded, i.e. it computes the reflexive transitive closure of
579 prerequsites.
580</p>
581
582<h3 id="rdeps">Transitive closure of reverse dependencies: rdeps</h3>
583<pre>expr ::= rdeps(<var>expr</var>, <var>expr</var>)
584 | rdeps(<var>expr</var>, <var>expr</var>, <var>depth</var>)</pre>
585<p>
586 The <code>rdeps(<var>u</var>, <var>x</var>)</code> operator evaluates
587 to the reverse dependencies of the argument set <var>x</var> within the
588 transitive closure of the universe set <var>u</var>.
589</p>
590
591<p>
592 The resulting graph is ordered according to the dependency relation. See the
593 section on <a href='#graph-order'>graph order</a> for more details.
594</p>
595
596<p>
597 The <code>rdeps</code> operator accepts an optional third argument,
598 which is an integer literal specifying an upper bound on the depth of the
599 search. The resulting graph will only include nodes within a distance of the
600 specified depth from any node in the argument set. So
601 <code>rdeps(//foo, //common, 1)</code> evaluates to all nodes in the
602 transitive closure of <code>//foo</code> that directly depend on
603 <code>//common</code>. (These numbers correspond to the ranks shown in the
604 <a href='#output-ranked'><code>minrank</code></a> output format.) If the
605 <var>depth</var> parameter is omitted, the search is unbounded.
606</p>
607
janakrfd9e5a22017-12-06 09:59:52 -0800608<h3 id="allrdeps">Transitive closure of all reverse dependencies: allrdeps</h3>
609<pre>expr ::= allrdeps(<var>expr</var>)
610 | allrdeps(<var>expr</var>, <var>depth</var>)</pre>
611<b>Only available with <a href='#sky-query'>Sky Query</a></b><br/>
612<p>
613 The <code>allrdeps</code> operator behaves just like the <a href='#rdeps'><code>rdeps</code></a>
614 operator, except that the "universe set" is whatever the <code>--universe_scope</code> flag
615 evaluated to, instead of being separately specified. Thus, if
616 <code>--universe_scope=//foo/...</code> was passed, then <code>allrdeps(//bar)</code> is
nharmata57066ff2018-03-19 11:52:53 -0700617 equivalent to <code>rdeps(//foo/..., //bar)</code>.
janakrfd9e5a22017-12-06 09:59:52 -0800618</p>
619
nharmata50f72492017-08-11 21:31:03 +0200620<h3 id="siblings">Dealing with a target's package: siblings</h3>
621<pre>expr ::= siblings(<var>expr</var>)</pre>
622<p>
623 The <code>siblings(<var>x</var>)</code> operator evalutes to the full set of targets that are in
624 the same package as a target in the argument set.
625</p>
626
David Chen8fe82a32016-08-24 10:55:41 +0000627<h3 id="some">Arbitrary choice: some</h3>
628<pre>expr ::= some(<var>expr</var>)</pre>
629<p>
630 The <code>some(<var>x</var>)</code> operator selects one target
631 arbitrarily from its argument set <var>x</var>, and evaluates to a
632 singleton set containing only that target. For example, the
633 expression <code>some(//foo:main union //bar:baz)</code>
634 evaluates to a set containing either <code>//foo:main</code> or
635 <code>//bar:baz</code>&mdash;though which one is not defined.
636</p>
637
638<p>
639 If the argument is a singleton, then <code>some</code>
640 computes the identity function: <code>some(//foo:main)</code> is
641 equivalent to <code>//foo:main</code>.
642
643 It is an error if the specified argument set is empty, as in the
644 expression <code>some(//foo:main intersect //bar:baz)</code>.
645</p>
646
647<h3 id="path-operators">Path operators: somepath, allpaths</h3>
648<pre>expr ::= somepath(<var>expr</var>, <var>expr</var>)
649 | allpaths(<var>expr</var>, <var>expr</var>)</pre>
650<p>
651 The <code>somepath(<var>S</var>, <var>E</var>)</code> and
652 <code>allpaths(<var>S</var>, <var>E</var>)</code> operators compute
653 paths between two sets of targets. Both queries accept two
654 arguments, a set <var>S</var> of starting points and a set
655 <var>E</var> of ending points. <code>somepath</code> returns the
656 graph of nodes on <em>some</em> arbitrary path from a target in
657 <var>S</var> to a target in <var>E</var>; <code>allpaths</code>
658 returns the graph of nodes on <em>all</em> paths from any target in
659 <var>S</var> to any target in <var>E</var>.
660</p>
661
662<p>
663 The resulting graphs are ordered according to the dependency relation.
664 See the section on <a href='#graph-order'>graph order</a> for more
665 details.
666</p>
667
668<table style='margin: auto'><tr>
669<td style='text-align: center'>
Kristina Chodorowfc9750c2017-02-09 21:13:13 +0000670<div class='graphviz dot'><!--
671digraph somepath1 {
672 graph [size="4,4"]
673 node [label="",shape=circle];
674 n1;
675 n2 [fillcolor="pink",style=filled];
676 n3 [fillcolor="pink",style=filled];
677 n4 [fillcolor="pink",style=filled,label="E"];
678 n5; n6;
679 n7 [fillcolor="pink",style=filled,label="S1"];
680 n8 [label="S2"];
681 n9;
682 n10 [fillcolor="pink",style=filled];
683
684 n1 -> n2;
685 n2 -> n3;
686 n7 -> n5;
687 n7 -> n2;
688 n5 -> n6;
689 n6 -> n4;
690 n8 -> n6;
691 n6 -> n9;
692 n2 -> n10;
693 n3 -> n10;
694 n10 -> n4;
695 n10 -> n11;
696}
697--></div>
David Chen8fe82a32016-08-24 10:55:41 +0000698<p><code>somepath(S1 + S2, E)</code>,<br/>one possible result.</p>
699</td>
700<td style='padding: 40px; text-align: center'>
Kristina Chodorowfc9750c2017-02-09 21:13:13 +0000701<div class='graphviz dot'><!--
702digraph somepath2 {
703 graph [size="4,4"]
704 node [label="",shape=circle];
705
706 n1; n2; n3;
707 n4 [fillcolor="pink",style=filled,label="E"];
708 n5;
709 n6 [fillcolor="pink",style=filled];
710 n7 [label="S1"];
711 n8 [fillcolor="pink",style=filled,label="S2"];
712 n9; n10;
713
714 n1 -> n2;
715 n2 -> n3;
716 n7 -> n5;
717 n7 -> n2;
718 n5 -> n6;
719 n6 -> n4;
720 n8 -> n6;
721 n6 -> n9;
722 n2 -> n10;
723 n3 -> n10;
724 n10 -> n4;
725 n10 -> n11;
726}
727--></div>
David Chen8fe82a32016-08-24 10:55:41 +0000728<p><code>somepath(S1 + S2, E)</code>,<br/>another possible result.</p>
729</td>
730<td style='text-align: center'>
Kristina Chodorowfc9750c2017-02-09 21:13:13 +0000731<div class='graphviz dot'><!--
732digraph allpaths {
733 graph [size="4,4"]
734 node [label="",shape=circle];
735 n1;
736 n2 [fillcolor="pink",style=filled];
737 n3 [fillcolor="pink",style=filled];
738 n4 [fillcolor="pink",style=filled,label="E"];
739 n5 [fillcolor="pink",style=filled];
740 n6 [fillcolor="pink",style=filled];
741 n7 [fillcolor="pink",style=filled, label="S1"];
742 n8 [fillcolor="pink",style=filled, label="S2"];
743 n9;
744 n10 [fillcolor="pink",style=filled];
745
746 n1 -> n2;
747 n2 -> n3;
748 n7 -> n5;
749 n7 -> n2;
750 n5 -> n6;
751 n6 -> n4;
752 n8 -> n6;
753 n6 -> n9;
754 n2 -> n10;
755 n3 -> n10;
756 n10 -> n4;
757 n10 -> n11;
758}
759--></div>
David Chen8fe82a32016-08-24 10:55:41 +0000760<p><code>allpaths(S1 + S2, E)</code>.</p>
761</td>
762</tr></table>
763
764<h3 id="kind">Target kind filtering: kind</h3>
765<pre>expr ::= kind(<var>word</var>, <var>expr</var>) </pre>
766<p>
767 The <code>kind(<var>pattern</var>, <var>input</var>)</code> operator
768 applies a filter to a set of targets, and discards those targets
769 that are not of the expected kind. The <var>pattern</var> parameter specifies
770 what kind of target to match.
771</p>
772<ul>
773<li><b>file</b> patterns can be one of:
774 <ul>
775 <li><code>source file</code>
776 <li><code>generated file</code>
777 </ul>
778<li><b>rule</b> patterns can be one of:
779 <ul>
780 <li><code><var>ruletype</var> rule</code>
781 <li><code><var>ruletype</var></code><br>
782 Where <var>ruletype</var> is a build rule. The difference between these
783 forms is that including "rule" causes the regular expression match for
784 <var>ruletype</var> to be anchored.
785 </ul>
786<li><b>package group</b> patterns should simply be:
787 <ul>
788 <li><code>package group</code>
789 </ul>
790</ul>
791<p>
792 For example, the kinds for the four targets defined by the BUILD file
793 (for package <code>p</code>) shown below are illustrated in the
794 table:
795</p>
796
797<table style='margin: auto'><tr><td style='padding-right:10px'>
798<pre style='margin-left: 0em;'>
799genrule(
800 name = "a",
801 srcs = ["a.in"],
802 outs = ["a.out"],
803 cmd = "...",
804)
805</pre>
806</td><td>
807 <table class="grid">
808 <tr><th>Target</th><th>Kind</th></tr>
809 <tr class='tt'><td>//p:a</td><td>genrule rule</td></tr>
810 <tr class='tt'><td>//p:a.in</td><td>source file</td></tr>
811 <tr class='tt'><td>//p:a.out</td><td>generated file</td></tr>
812 <tr class='tt'><td>//p:BUILD</td><td>source file</td></tr>
813 </table>
814</td></tr></table>
815
816<p>
817 Thus, <code>kind("cc_.* rule", foo/...)</code> evaluates to the set
818 of all <code>cc_library</code>, <code>cc_binary</code>, etc,
819 rule targets beneath
820 <code>foo</code>, and <code>kind("source file", deps(//foo))</code>
821 evaluates to the set of all source files in the transitive closure
822 of dependencies of the <code>//foo</code> target.
823</p>
824
825<p>
826 Quotation of the <var>pattern</var> argument is often required
827 because without it, many regular expressions, such as <code>source
828 file</code> and <code>.*_test</code>, are not considered words by
829 the parser.
830</p>
831
832<p>
833 When matching for <code>package group</code>, targets ending in
834 <code>:all</code> may not yield any results.
835 Use <code>:all-targets</code> instead.
836</p>
837
838<h3 id="filter">Target name filtering: filter</h3>
839<pre>expr ::= filter(<var>word</var>, <var>expr</var>) </pre>
840<p>
841 The <code>filter(<var>pattern</var>, <var>input</var>)</code> operator
842 applies a filter to a set of targets, and discards targets whose
843 labels (in absolute form) do not match the pattern; it
844 evaluates to a subset of its input.
845</p>
846
847<p>
848 The first argument, <var>pattern</var> is a word containing a
849 regular expression over target names. A <code>filter</code> expression
850 evaluates to the set containing all targets <var>x</var> such that
851 <var>x</var> is a member of the set <var>input</var> and the
852 label (in absolute form, e.g. <code>//foo:bar</code>)
853 of <var>x</var> contains an (unanchored) match
854 for the regular expression <var>pattern</var>. Since all
855 target names start with <code>//</code>, it may be used as an alternative
856 to the <code>^</code> regular expression anchor.
857</p>
858
859<p>
860 This operator often provides a much faster and more robust alternative to the
861 <code>intersect</code> operator. For example, in order to see all
862 <code>bar</code> dependencies of the <code>//foo:foo</code> target, one could
863 evaluate
864</p>
865<pre>deps(//foo) intersect //bar/...</pre>
866<p>
867 This statement, however, will require parsing of all BUILD files in the
868 <code>bar</code> tree, which will be slow and prone to errors in
869 irrelevant BUILD files. An alternative would be:
870</p>
871<pre>filter(//bar, deps(//foo))</pre>
872<p>
873 which would first calculate the set of <code>//foo</code> dependencies and
874 then would filter only targets matching the provided pattern&mdash;in other
875 words, targets with names containing <code>//bar</code> as a
876 substring.
877</p>
878
879<p>
880 Another common use of the <code>filter(<var>pattern</var>,
881 <var>expr</var>)</code> operator is to filter specific files by their
882 name or extension. For example,
883</p>
884<pre>filter("\.cc$", deps(//foo))</pre>
885<p>
886 will provide a list of all <code>.cc</code> files used to build
887 <code>//foo</code>.
888</p>
889
890<h3 id="attr">Rule attribute filtering: attr</h3>
891<pre>expr ::= attr(<var>word</var>, <var>word</var>, <var>expr</var>) </pre>
892<p>
893 The <code>attr(<var>name</var>, <var>pattern</var>, <var>input</var>)</code>
894 operator applies a filter to a set of targets, and discards targets that
895 are not rules, rule targets that do not have attribute <var>name</var>
896 defined or rule targets where the attribute value does not match the provided
897 regular expression <var>pattern</var>; it evaluates to a subset of its input.
898</p>
899
900<p>
901 The first argument, <var>name</var> is the name of the rule attribute that
902 should be matched against the provided regular expression pattern. The second
903 argument, <var>pattern</var> is a regular expression over the attribute
904 values. An <code>attr</code> expression evaluates to the set containing all
905 targets <var>x</var> such that <var>x</var> is a member of the set
906 <var>input</var>, is a rule with the defined attribute <var>name</var> and
907 the attribute value contains an (unanchored) match for the regular expression
908 <var>pattern</var>. Please note, that if <var>name</var> is an optional
909 attribute and rule does not specify it explicitly then default attribute
910 value will be used for comparison. For example,
911</p>
912<pre>attr(linkshared, 0, deps(//foo))</pre>
913<p>
914 will select all <code>//foo</code> dependencies that are allowed to have a
915 linkshared attribute (e.g., <code>cc_binary</code> rule) and have it either
916 explicitly set to 0 or do not set it at all but default value is 0 (e.g. for
917 <code>cc_binary</code> rules).
918</p>
919
920<p>
921 List-type attributes (such as <code>srcs</code>, <code>data</code>, etc) are
922 converted to strings of the form <code>[value<sub>1</sub>, ..., value<sub>n</sub>]</code>,
923 starting with a <code>[</code> bracket, ending with a <code>]</code> bracket
924 and using "<code>, </code>" (comma, space) to delimit multiple values.
925 Labels are converted to strings by using the absolute form of the
926 label. For example, an attribute <code>deps=[":foo",
927 "//otherpkg:bar", "wiz"]</code> would be converted to the
928 string <code>[//thispkg:foo, //otherpkg:bar, //thispkg:wiz]</code>.
929 Brackets
930 are always present, so the empty list would use string value <code>[]</code>
931 for matching purposes. For example,
932</p>
933<pre>attr("srcs", "\[\]", deps(//foo))</pre>
934<p>
935 will select all rules among <code>//foo</code> dependencies that have an
936 empty <code>srcs</code> attribute, while
937</p>
938<pre>attr("data", ".{3,}", deps(//foo))</pre>
939<p>
940 will select all rules among <code>//foo</code> dependencies that specify at
941 least one value in the <code>data</code> attribute (every label is at least
942 3 characters long due to the <code>//</code> and <code>:</code>).
943</p>
944
945<h3 id="visible">Rule visibility filtering: visible</h3>
946<pre>expr ::= visible(<var>expr</var>, <var>expr</var>) </pre>
947<p>
948 The <code>visible(<var>predicate</var>, <var>input</var>)</code> operator
949 applies a filter to a set of targets, and discards targets without the
950 required visibility.
951</p>
952
953<p>
954 The first argument, <var>predicate</var>, is a set of targets that all targets
955 in the output must be visible to. A <var>visible</var> expression
956 evaluates to the set containing all targets <var>x</var> such that <var>x</var>
957 is a member of the set <var>input</var>, and for all targets <var>y</var> in
958 <var>predicate</var> <var>x</var> is visible to <var>y</var>. For example:
959</p>
960<pre>visible(//foo, //bar:*)</pre>
961<p>
962 will select all targets in the package <code>//bar</code> that <code>//foo</code>
963 can depend on without violating visibility restrictions.
964</p>
965
966<h3 id="labels">Evaluation of rule attributes of type label: labels</h3>
967<pre>expr ::= labels(<var>word</var>, <var>expr</var>) </pre>
968<p>
969 The <code>labels(<var>attr_name</var>, <var>inputs</var>)</code>
970 operator returns the set of targets specified in the
971 attribute <var>attr_name</var> of type "label" or "list of label" in
972 some rule in set <var>inputs</var>.
973</p>
974
975<p>
976 For example, <code>labels(srcs, //foo)</code> returns the set of
977 targets appearing in the <code>srcs</code> attribute of
978 the <code>//foo</code> rule. If there are multiple rules
979 with <code>srcs</code> attributes in the <var>inputs</var> set, the
980 union of their <code>srcs</code> is returned.
981</p>
982
David Chen8fe82a32016-08-24 10:55:41 +0000983<h3 id="tests">Expand and filter test_suites: tests</h3>
984<pre>expr ::= tests(<var>expr</var>)</pre>
985<p>
986 The <code>tests(<var>x</var>)</code> operator returns the set of all test
987 rules in set <var>x</var>, expanding any <code>test_suite</code> rules into
988 the set of individual tests that they refer to, and applying filtering by
989 <code>tag</code> and <code>size</code>.
990
991 By default, query evaluation
992 ignores any non-test targets in all <code>test_suite</code> rules. This can be
993 changed to errors with the <code>--strict_test_suite</code> option.
994</p>
995
996<p>
997 For example, the query <code>kind(test, foo:*)</code> lists all
998 the <code>*_test</code> and <code>test_suite</code> rules
999 in the <code>foo</code> package. All the results are (by
1000 definition) members of the <code>foo</code> package. In contrast,
1001 the query <code>tests(foo:*)</code> will return all of the
1002 individual tests that would be executed by <code>bazel test
1003 foo:*</code>: this may include tests belonging to other packages,
1004 that are referenced directly or indirectly
1005 via <code>test_suite</code> rules.
1006</p>
1007
1008
1009<h3 id="buildfiles">Package definition files: buildfiles</h3>
1010<pre>expr ::= buildfiles(<var>expr</var>)</pre>
1011<p>
1012 The <code>buildfiles(<var>x</var>)</code> operator returns the set
1013 of files that define the packages of each target in
1014 set <var>x</var>; in other words, for each package, its BUILD file,
janakrfd9e5a22017-12-06 09:59:52 -08001015 plus any files it references via <code>load</code>. Note that this also returns the BUILD files
1016 of the packages containing these <code>load</code>ed files.
David Chen8fe82a32016-08-24 10:55:41 +00001017</p>
1018
1019<p>
1020 This operator is typically used when determining what files or
1021 packages are required to build a specified target, often in conjunction with
1022 the <a href='#output-package'><code>--output package</code></a>
1023 option, below). For example,
1024</p>
1025<pre>bazel query 'buildfiles(deps(//foo))' --output package</pre>
1026<p>
1027 returns the set of all packages on which <code>//foo</code> transitively
1028 depends.
1029</p>
1030
1031<p>
1032 (Note: a naive attempt at the above query would omit
1033 the <code>buildfiles</code> operator and use only <code>deps</code>,
1034 but this yields an incorrect result: while the result contains the
1035 majority of needed packages, those packages that contain only files
janakrfd9e5a22017-12-06 09:59:52 -08001036 that are <code>load()</code>'ed will be missing.
1037</p>
David Chen8fe82a32016-08-24 10:55:41 +00001038
janakrfd9e5a22017-12-06 09:59:52 -08001039<h3 id="rbuildfiles">Package definition files: rbuildfiles</h3>
nharmatab7922ef2018-03-23 12:21:34 -07001040<pre>expr ::= rbuildfiles(<var>word</var>, ...)</pre>
janakrfd9e5a22017-12-06 09:59:52 -08001041<b>Only available with <a href='#sky-query'>Sky Query</a></b><br/>
1042<p>
nharmatab7922ef2018-03-23 12:21:34 -07001043 The <code>rbuildfiles</code> operator takes a comma-separated list of path fragments and returns
1044 the set of BUILD files that transitively depend on these path fragments. For instance, if
1045 <code>//foo</code> is a package, then <code>rbuildfiles(foo/BUILD)</code> will return the
1046 <code>//foo:BUILD</code> target. If the <code>foo/BUILD</code> file has
1047 <code>load('//bar:file.bzl'...</code> in it, then <code>rbuildfiles(bar/file.bzl)</code> will
1048 return the <code>//foo:BUILD</code> target, as well as the targets for any other BUILD files that
1049 load <code>//bar:file.bzl</code>
janakrfd9e5a22017-12-06 09:59:52 -08001050</p>
1051
1052<p>
1053 The scope of the <scope>rbuildfiles</scope> operator is the universe specified by the
1054 <code>--universe_scope</code> flag. Files that do not correspond directly to BUILD files and .bzl
1055 files do not affect the results. For instance, source files (like <code>foo.cc</code>) are ignored,
1056 even if they are explicitly mentioned in the BUILD file. Symlinks, however, are respected, so that
1057 if <code>foo/BUILD</code> is a symlink to <code>bar/BUILD</code>, then
1058 <code>rbuildfiles(bar/BUILD)</code> will include <code>//foo:BUILD</code> in its results.
1059</p>
1060
1061<p>
nharmatab7922ef2018-03-23 12:21:34 -07001062 The <code>rbuildfiles</code> operator is almost morally the inverse of the
janakrfd9e5a22017-12-06 09:59:52 -08001063 <a href='#buildfiles'><code>buildfiles</code></a> operator. However, this moral inversion
1064 holds more strongly in one direction: the outputs of <code>rbuildfiles</code> are just like the
nharmatab7922ef2018-03-23 12:21:34 -07001065 inputs of <code>buildfiles</code>; the former will only contain BUILD file targets in packages,
1066 and the latter may contain such targets. In the other direction, the correspondence is weaker. The
1067 outputs of the <code>buildfiles</code> operator are targets corresponding to all packages and .bzl
1068 files needed by a given input. However, the inputs of the <code>rbuildfiles</code> operator are
1069 not those targets, but rather the path fragments that correspond to those targets.
David Chen8fe82a32016-08-24 10:55:41 +00001070</p>
1071
1072<h3 id="loadfiles">Package definition files: loadfiles</h3>
1073<pre>expr ::= loadfiles(<var>expr</var>)</pre>
1074<p>
1075 The <code>loadfiles(<var>x</var>)</code> operator returns the set of
1076 Skylark files that are needed to load the packages of each target in
1077 set <var>x</var>. In other words, for each package, it returns the
1078 .bzl files that are referenced from its BUILD files.
1079</p>
1080
gregceffb8a982018-02-12 15:34:00 -08001081<h2 id='output-formats'>Output Formats</h2>
David Chen8fe82a32016-08-24 10:55:41 +00001082
1083<p>
1084 <code>bazel query</code> generates a graph.
1085 You specify the content, format, and ordering by which
1086 <code>bazel query</code> presents this graph
1087 by means of the <code>--output</code>
1088 command-line option.
1089 </p>
1090
1091<p>
Googlere07a0b82018-04-30 07:35:26 -07001092 When running with <a href='#sky-query'>Sky Query</a>, only output formats that are compatible with
janakrfd9e5a22017-12-06 09:59:52 -08001093 unordered output are allowed. Specifically, <code>graph</code>, <code>minrank</code>, and
1094 <code>maxrank</code> output formats are forbidden.
1095</p>
1096
1097<p>
David Chen8fe82a32016-08-24 10:55:41 +00001098 Some of the output formats accept additional options. The name of
1099 each output option is prefixed with the output format to which it
1100 applies, so <code>--graph:factored</code> applies only
1101 when <code>--output=graph</code> is being used; it has no effect if
1102 an output format other than <code>graph</code> is used. Similarly,
1103 <code>--xml:line_numbers</code> applies only when <code>--output=xml</code>
1104 is being used.
1105</p>
1106
1107<h3 id='result-order'>On the ordering of results</h3>
1108
1109<p>
1110 Although query expressions always follow the "<a href='#graph-order'>law of
1111 conservation of graph order</a>", <i>presenting</i> the results may be done
1112 in either a dependency-ordered or unordered manner. This does <b>not</b>
1113 influence the targets in the result set or how the query is computed. It only
1114 affects how the results are printed to stdout. Moreover, nodes that are
1115 equivalent in the dependency order may or may not be ordered alphabetically.
1116 The <code>--order_output</code> flag can be used to control this behavior.
1117 (The <code>--[no]order_results</code> flag has a subset of the functionality
1118 of the <code>--order_output</code> flag and is deprecated.)
1119</p>
1120<p>
1121 The default value of this flag is <code>auto</code>, which is equivalent to
1122 <code>full</code> for every output format except for <code>proto</code>,
1123 <code>graph</code>, <code>minrank</code>, and <code>maxrank</code>, for which
1124 it is equivalent to <code>deps</code>.
1125</p>
1126<p>
1127 When this flag is <code>no</code> and <code>--output</code> is one of
1128 <code>build</code>, <code>label</code>, <code>label_kind</code>,
1129 <code>location</code>, <code>package</code>, <code>proto</code>,
1130 <code>record</code> or <code>xml</code>, the outputs will be printed in
1131 arbitrary order. <b>This is generally the fastest option</b>. It is not
1132 supported though when <code>--output</code> is one of <code>graph</code>,
twerthd2ce2702018-08-09 08:00:40 -07001133 <code>minrank</code> or <code>maxrank</code>: with these formats, bazel will
David Chen8fe82a32016-08-24 10:55:41 +00001134 always print results ordered by the dependency order or rank.
1135</p>
1136<p>
1137 When this flag is <code>deps</code>, bazel will print results ordered by the
1138 dependency order. However, nodes that are unordered by the dependency order
1139 (because there is no path from either one to the other) may be printed in any
1140 order.
1141</p>
1142<p>
1143 When this flag is <code>full</code>, bazel will print results ordered by the
1144 dependency order, with unordered nodes ordered alphabetically or reverse
1145 alphabetically, depending on the output format. This may be slower than the
1146 other options, and so should only be used when deterministic results are
1147 important &mdash; it is guaranteed with this option that running the same query
1148 multiple times will always produce the same output.
1149</p>
1150
1151<h3 id="output-build">Print the source form of targets as they would appear in BUILD</h3>
1152<pre>--output build</pre>
1153<p>
1154 With this option, the representation of each target is as if it were
1155 hand-written in the BUILD language. All variables and function calls
1156 (e.g. glob, macros) are expanded, which is useful for seeing the effect
1157 of Skylark macros. Additionally, each effective rule is annotated with
1158 the name of the macro (if any, see <code>generator_name</code> and
1159 <code>generator_function</code>) that produced it.
1160</p>
1161<p>
1162 Although the output uses the same syntax as BUILD files, it is not
1163 guaranteed to produce a valid BUILD file.
1164</p>
1165
1166<h3 id="output-label">Print the label of each target</h3>
1167<pre>--output label</pre>
1168<p>
1169 With this option, the set of names (or <em>labels</em>) of each target
1170 in the resulting graph is printed, one label per line, in
1171 topological order (unless <code>--noorder_results</code> is specified, see
1172 <a href='#result-order'>notes on the ordering of results</a>).
1173 (A topological ordering is one in which a graph
1174 node appears earlier than all of its successors.) Of course there
1175 are many possible topological orderings of a graph (<em>reverse
1176 postorder</em> is just one); which one is chosen is not specified.
1177
1178 When printing the output of a <code>somepath</code> query, the order
1179 in which the nodes are printed is the order of the path.
1180</p>
1181
1182<p>
1183 Caveat: in some corner cases, there may be two distinct targets with
1184 the same label; for example, a <code>sh_binary</code> rule and its
1185 sole (implicit) <code>srcs</code> file may both be called
1186 <code>foo.sh</code>. If the result of a query contains both of
1187 these targets, the output (in <code>label</code> format) will appear
1188 to contain a duplicate. When using the <code>label_kind</code> (see
1189 below) format, the distinction becomes clear: the two targets have
1190 the same name, but one has kind <code>sh_binary rule</code> and the
1191 other kind <code>source file</code>.
1192</p>
1193
1194<h3 id="output-label_kind">Print the label and kind of each target</h3>
1195<pre>--output label_kind</pre>
1196<p>
1197 Like <code>label</code>, this output format prints the labels of
1198 each target in the resulting graph, in topological order, but it
1199 additionally precedes the label by
1200 the <a href='#kind'><em>kind</em></a> of the target.
1201</p>
1202
1203<h3 id="output-ranked">Print the label of each target, in rank order</h3>
1204<pre>--output minrank
1205--output maxrank</pre>
1206<p>
1207 Like <code>label</code>, the <code>minrank</code>
1208 and <code>maxrank</code> output formats print the labels of each
1209 target in the resulting graph, but instead of appearing in
1210 topological order, they appear in rank order, preceded by their
1211 rank number. These are unaffected by the result ordering
1212 <code>--[no]order_results</code> flag (see <a href='#result-order'>notes on
1213 the ordering of results</a>).
1214</p>
1215
1216<p>
1217 There are two variants of this format: <code>minrank</code> ranks
1218 each node by the length of the shortest path from a root node to it.
1219 "Root" nodes (those which have no incoming edges) are of rank 0,
1220 their successors are of rank 1, etc. (As always, edges point from a
1221 target to its prerequisites: the targets it depends upon.)
1222</p>
1223
1224<p>
1225 <code>maxrank</code> ranks each node by the length of the longest
1226 path from a root node to it. Again, "roots" have rank 0, all other
1227 nodes have a rank which is one greater than the maximum rank of all
1228 their predecessors.
1229</p>
1230
1231<p>
1232 All nodes in a cycle are considered of equal rank. (Most graphs are
1233 acyclic, but cycles do occur
1234 simply because BUILD files contain erroneous cycles.)
1235</p>
1236
1237<p>
1238 These output formats are useful for discovering how deep a graph is.
1239 If used for the result of a <code>deps(x)</code>, <code>rdeps(x)</code>,
1240 or <code>allpaths</code> query, then the rank number is equal to the
1241 length of the shortest (with <code>minrank</code>) or longest
1242 (with <code>maxrank</code>) path from <code>x</code> to a node in
1243 that rank. <code>maxrank</code> can be used to determine the
1244 longest sequence of build steps required to build a target.
1245</p>
1246
1247<p>
1248 Please note, the ranked output of a <code>somepath</code> query is
1249 basically meaningless because <code>somepath</code> doesn't
1250 guarantee to return either a shortest or a longest path, and it may
1251 include "transitive" edges from one path node to another that are
1252 not direct edges in original graph.
1253</p>
1254
1255<p>
1256 For example, the graph on the left yields the outputs on the right
1257 when <code>--output minrank</code> and <code>--output maxrank</code>
1258 are specified, respectively.
1259</p>
1260
1261<table style='margin: auto'><tr><td>
Kristina Chodorowfc9750c2017-02-09 21:13:13 +00001262<div class='graphviz dot'><!--
1263digraph mygraph {
1264 node [shape=box];
1265"//a:a" -> "//a:a.cc"
1266"//b:b" -> "//a:a"
1267"//b:b" -> "//b:b.cc"
1268"//c:c" -> "//b:b"
1269"//c:c" -> "//a:a"
1270}
1271--></div>
David Chen8fe82a32016-08-24 10:55:41 +00001272</td><td>
1273<pre>
1274minrank
1275
12760 //c:c
12771 //b:b
12781 //a:a
12792 //b:b.cc
12802 //a:a.cc
1281</pre>
1282</td><td>
1283<pre>
1284maxrank
1285
12860 //c:c
12871 //b:b
12882 //a:a
12892 //b:b.cc
12903 //a:a.cc
1291</pre>
1292</td></tr></table>
1293
1294<h3 id="output-location">Print the location of each target</h3>
1295<pre>--output location</pre>
1296<p>
1297 Like <code>label_kind</code>, this option prints out, for each
1298 target in the result, the target's kind and label, but it is
1299 prefixed by a string describing the location of that target, as a
1300 filename and line number. The format resembles the output of
1301 <code>grep</code>. Thus, tools that can parse the latter (such as Emacs
1302 or vi) can also use the query output to step through a series of
1303 matches, allowing the Bazel query tool to be used as a
1304 dependency-graph-aware "grep for BUILD files".
1305</p>
1306
1307<p>
1308 The location information varies by target kind (see the <a
1309 href='#kind'>kind</a> operator). For rules, the
1310 location of the rule's declaration within the BUILD file is printed.
1311 For source files, the location of line 1 of the actual file is
1312 printed. For a generated file, the location of the rule that
1313 generates it is printed. (The query tool does not have sufficient
1314 information to find the actual location of the generated file, and
1315 in any case, it might not exist if a build has not yet been
1316 performed.)
1317</p>
1318
1319<h3 id="output-package">Print the set of packages</h3>
1320<pre>--output package</pre>
1321<p>
1322 This option prints the name of all packages to which
1323 some target in the result set belongs. The names are printed in
1324 lexicographical order; duplicates are excluded. Formally, this
1325 is a <em>projection</em> from the set of labels (package, target) onto
1326 packages.
1327</p>
1328
1329<p>
mschaller3c566c62017-06-22 00:11:44 +02001330 Packages in external repositories are formatted as
1331 <code>@repo//foo/bar</code> while packages in the main repository are
1332 formatted as <code>foo/bar</code>.
1333</p>
1334<p>
ajmichael0cce52e2017-06-08 18:40:00 +02001335 In conjunction with the <code>deps(...)</code> query, this output
1336 option can be used to find the set of packages that must be checked
1337 out in order to build a given set of targets.
1338</p>
1339
1340<h3 id="output-graph">Display a graph of the result</h3>
1341<pre>--output graph</pre>
1342<p>
1343 This option causes the query result to be printed as a directed
1344 graph in the popular AT&amp;T GraphViz format. Typically the
1345 result is saved to a file, such as <code>.png</code> or <code>.svg</code>.
1346 (If the <code>dot</code> program is not installed on your workstation, you
1347 can install it using the command <code>sudo apt-get install graphviz</code>.)
1348 See the example section below for a sample invocation.
1349</p>
1350
1351<p>
Googler493a4b12018-04-30 08:51:36 -07001352 This output format is particularly useful for <code>allpaths</code>,
ajmichael0cce52e2017-06-08 18:40:00 +02001353 <code>deps</code>, or <code>rdeps</code> queries, where the result
1354 includes a <em>set of paths</em> that cannot be easily visualized when
1355 rendered in a linear form, such as with <code>--output label</code>.
1356</p>
1357
1358<p>
1359 By default, the graph is rendered in a <em>factored</em> form. That is,
1360 topologically-equivalent nodes are merged together into a single
1361 node with multiple labels. This makes the graph more compact
1362 and readable, because typical result graphs contain highly
1363 repetitive patterns. For example, a <code>java_library</code> rule
1364 may depend on hundreds of Java source files all generated by the
1365 same <code>genrule</code>; in the factored graph, all these files
1366 are represented by a single node. This behavior may be disabled
1367 with the <code>--nograph:factored</code> option.
1368</p>
1369
1370<h4><code>--graph:node_limit <var>n</var></code></h4>
1371<p>
1372 The option specifies the maximum length of the label string for a
1373 graph node in the output. Longer labels will be truncated; -1
1374 disables truncation. Due to the factored form in which graphs are
1375 usually printed, the node labels may be very long. GraphViz cannot
1376 handle labels exceeding 1024 characters, which is the default value
1377 of this option. This option has no effect unless
1378 <code>--output=graph</code> is being used.
1379</p>
1380
1381<h4><code>--[no]graph:factored</code></h4>
1382<p>
1383 By default, graphs are displayed in factored form, as explained
1384 <a href='#output-graph'>above</a>.
1385 When <code>--nograph:factored</code> is specified, graphs are
1386 printed without factoring. This makes visualization using GraphViz
1387 impractical, but the simpler format may ease processing by other
1388 tools (e.g. grep). This option has no effect
1389 unless <code>--output=graph</code> is being used.
1390</p>
1391
David Chen8fe82a32016-08-24 10:55:41 +00001392<h3 id="output-xml">XML</h3>
1393<pre>--output xml</pre>
1394<p>
1395 This option causes the resulting targets to be printed in an XML
1396 form. The output starts with an XML header such as this
1397</p>
1398<pre>
1399 &lt;?xml version="1.0" encoding="UTF-8"?&gt;
1400 &lt;query version="2"&gt;
1401</pre>
1402<!-- The docs should continue to document version 2 into perpetuity,
1403 even if we add new formats, to handle clients synced to old CLs. -->
1404<p>
1405 and then continues with an XML element for each target
1406 in the result graph, in topological order (unless
1407 <a href='#result-order'>unordered results</a> are requested),
1408 and then finishes with a terminating
1409</p>
1410<pre>
1411&lt;/query&gt;
1412</pre>
1413<p>
1414 Simple entries are emitted for targets of <code>file</code>
1415 kind:
1416</p>
1417<pre>
1418 &lt;source-file name='//foo:foo_main.cc' .../&gt;
1419 &lt;generated-file name='//foo:libfoo.so' .../&gt;
1420</pre>
1421<p>
1422 But for rules, the XML is structured and contains definitions of all
1423 the attributes of the rule, including those whose value was not
1424 explicitly specified in the rule's BUILD file.
1425</p>
1426<p>
1427 Additionally, the result includes <code>rule-input</code> and
1428 <code>rule-output</code> elements so that the topology of the
1429 dependency graph can be reconstructed without having to know that,
1430 for example, the elements of the <code>srcs</code> attribute are
1431 forward dependencies (prerequisites) and the contents of the
1432 <code>outs</code> attribute are backward dependencies (consumers).
1433
1434 <code>rule-input</code> elements for <a
1435 href='#implicit_deps'>implicit dependencies</a> are suppressed if
1436 <code>--noimplicit_deps</code> is specified.
1437</p>
1438<pre>
1439 &lt;rule class='cc_binary rule' name='//foo:foo' ...&gt;
1440 &lt;list name='srcs'&gt;
1441 &lt;label value='//foo:foo_main.cc'/&gt;
1442 &lt;label value='//foo:bar.cc'/&gt;
1443 ...
1444 &lt;/list&gt;
1445 &lt;list name='deps'&gt;
1446 &lt;label value='//common:common'/&gt;
1447 &lt;label value='//collections:collections'/&gt;
1448 ...
1449 &lt;/list&gt;
1450 &lt;list name='data'&gt;
1451 ...
1452 &lt;/list&gt;
1453 &lt;int name='linkstatic' value='0'/&gt;
1454 &lt;int name='linkshared' value='0'/&gt;
1455 &lt;list name='licenses'/&gt;
1456 &lt;list name='distribs'&gt;
1457 &lt;distribution value="INTERNAL" /&gt;
1458 &lt;/list&gt;
1459 &lt;rule-input name="//common:common" /&gt;
1460 &lt;rule-input name="//collections:collections" /&gt;
1461 &lt;rule-input name="//foo:foo_main.cc" /&gt;
1462 &lt;rule-input name="//foo:bar.cc" /&gt;
1463 ...
1464 &lt;/rule&gt;
1465</pre>
1466
1467<p>
1468 Every XML element for a target contains a <code>name</code>
1469 attribute, whose value is the target's label, and
1470 a <code>location</code> attribute, whose value is the target's
1471 location as printed by the <a href='output-location'><code>--output
1472 location</code></a>.
1473</p>
1474
1475<h4><code>--[no]xml:line_numbers</code></h4>
1476<p>
1477 By default, the locations displayed in the XML output contain line numbers.
1478 When <code>--noxml:line_numbers</code> is specified, line numbers are not
1479 printed.
1480</p>
1481
1482<h4><code>--[no]xml:default_values</code></h4>
1483<p>
1484 By default, XML output does not include rule attribute whose value
1485 is the default value for that kind of attribute (e.g. because it
1486 were not specified in the BUILD file, or the default value was
1487 provided explicitly). This option causes such attribute values to
1488 be included in the XML output.
1489</p>
1490
1491
1492<h3 id="external-repos">Querying with external repositories</h3>
1493
1494<p>
1495 If the build depends on rules from external repositories (defined in the
1496 WORKSPACE file) then query results will include these dependencies. For
1497 example, if <code>//foo:bar</code> depends on <code>//external:some-lib</code>
1498 and <code>//external:some-lib</code> is bound to
1499 <code>@other-repo//baz:lib</code>, then
1500 <code>bazel query 'deps(//foo:bar)'</code>
1501 will list both <code>@other-repo//baz:lib</code> and
1502 <code>//external:some-lib</code> as dependencies.
1503</p>
1504
1505<p>
1506 External repositories themselves are not dependencies of a build. That is, in
1507 the example above, <code>//external:other-repo</code> is not a dependency. It
1508 can be queried for as a member of the <code>//external</code> package, though,
1509 for example:
1510<p>
1511
1512<pre>
laszlocsomor79251002017-04-03 13:09:47 +00001513 # Querying over all members of //external returns the repository.
1514 bazel query 'kind(maven_jar, //external:*)'
David Chen8fe82a32016-08-24 10:55:41 +00001515 //external:other-repo
1516
laszlocsomor79251002017-04-03 13:09:47 +00001517 # ...but the repository is not a dependency.
1518 bazel query 'kind(maven_jar, deps(//foo:bar))'
David Chen8fe82a32016-08-24 10:55:41 +00001519 INFO: Empty results
1520</pre>