blob: 89208438123751941a026afc75f5d68939929d43 [file] [log] [blame]
David Chen8fe82a32016-08-24 10:55:41 +00001---
2layout: documentation
3title: Query how-to
4---
5<h1><a name="_Bazel_Query_How_To"> </a>Bazel query how-to</h1>
6
7<p>This is a quick tutorial to get you started using Bazel's query language to trace dependencies in your code.<p/>
8
9<p>For a language details and <code>--output</code> flag details, please see the reference manual, <a href="query.html">Bazel query reference</a>. You can get help for Bazel query by typing <code>bazel help query</code>.<p/>
10
11<p>To execute a query while ignoring errors such as missing targets, use the <tt>--keep_going</tt> flag.</p>
12
13<h2><a name="_Contents"> </a> Contents </h2>
14
15<p />
16<ul>
17<li> <a href="#Finding_the_Dependencies_of_a_Ru">Finding the Dependencies of a Rule</a>
18</li>
19<li> <a href="#Tracing_the_Dependency_Chain_bet">Tracing the Dependency Chain between Two Packages</a>
20<ul>
21<li> <a href="#Aside_implicit_dependencies">Aside: implicit dependencies</a>
22</li>
23</ul>
24</li>
25<li> <a href="#Reverse_Dependencies">Reverse Dependencies</a>
26</li>
27<li> <a href="#Miscellaneous_Uses">Miscellaneous Uses</a>
28<ul>
29<li> <a href="#What_exists_">What exists ...</a>
30<ul>
31<li> <a href="#What_packages_exist_beneath_foo_">What packages exist beneath <code>foo</code>?</a>
32</li>
33<li> <a href="#What_rules_are_defined_in_the_foo">What rules are defined in the <code>foo</code> package?</a>
34</li>
35<li> <a href="#What_files_are_generated_by_rule">What files are generated by rules in the <code>foo</code> package?</a>
36</li>
37<li> <a href="#What_s_the_set_of_BUILD_files_ne">What's the set of BUILD files needed to build <code>//foo</code>?</a>
38</li>
39<li> <a href="#What_are_the_individual_tests_th">What are the individual tests that a <code>test_suite</code> expands to?</a>
40<ul>
41<li> <a href="#Which_of_those_are_C_tests_">Which of those are C++ tests?</a>
42</li>
43<li> <a href="#Which_of_those_are_small_Medium_">Which of those are small? Medium? Large?</a>
44</li>
45</ul>
46</li>
47<li> <a href="#What_are_the_tests_beneath_foo_t">What are the tests beneath <code>foo</code> that match a pattern?</a>
48</li>
49<li> <a href="#What_package_contains_file_java_">What package contains file <code>src/main/java/com/example/cache/LRUCache.java</code>?</a>
50</li>
51<li> <a href="#What_is_the_build_label_for_java">What is the build label for <code>src/main/java/com/example/cache/LRUCache.java</code>?</a>
52</li>
53<li> <a href="#What_build_rule_contains_file_ja">What build rule contains file <code>src/main/java/com/example/cache/LRUCache.java</code> as a source?</a>
54</li>
55</ul>
56</li>
57<li> <a href="#What_package_dependencies_exist_">What package dependencies exist ...</a>
58<ul>
59<li> <a href="#What_packages_does_foo_depend_on">What packages does <code>foo</code> depend on? (What do I need to check out to build <code>foo</code>)</a>
60</li>
61<li> <a href="#What_packages_does_the_foo_">What packages does the <code>foo</code> tree depend on, excluding <code>foo/contrib</code>?</a>
62</li>
63</ul>
64</li>
65<li> <a href="#What_rule_dependencies_exist_">What rule dependencies exist ...</a>
66<ul>
67<li> <a href="#What_genproto_rules_does_bar_">What genproto rules does bar depend upon?</a>
68</li>
69<li> <a href="#Find_the_definition_of_some_JNI_">Find the definition of some JNI (C++) library that is transitively depended upon by a Java binary rule in the servlet tree.</a>
70<ul>
71<li> <a href="#_Now_find_the_definitions_of_all">...Now find the definitions of all the Java binaries that depend on them</a>
72</li>
73</ul>
74</li>
75</ul>
76</li>
77<li> <a href="#What_file_dependencies_exist_">What file dependencies exist ...</a>
78<ul>
79<li> <a href="#What_s_the_complete_set_of_Java_">What's the complete set of Java source files required to build QUX?</a>
80</li>
81<li> <a href="#What_is_the_complete_set_of_Java">What is the complete set of Java source files required to build QUX's tests?</a>
82</li>
83</ul>
84</li>
85<li> <a href="#What_differences_in_dependencies">What differences in dependencies between X and Y exist ...</a>
86<ul>
87<li> <a href="#What_targets_does_foo_depend_on_">What targets does <code>//foo</code> depend on that <code>//foo:foolib</code> does not?</a>
88</li>
89<li> <a href="#What_C_libraries_do_the_foo_test">What C++ libraries do the <code>foo</code> tests depend on that the <code>//foo</code> production binary does <em>not</em> depend on?</a>
90</li>
91</ul>
92</li>
93<li> <a href="#Why_does_this_dependency_exist_">Why does this dependency exist ...</a>
94<ul>
95<li> <a href="#Why_does_bar_depend_on_groups">Why does <code>bar</code> depend on <code>groups2</code>?</a>
96</li>
97<li> <a href="#Show_me_a_path_from_docker_updater">Show me a path from <code>docker/updater:updater_systest</code> (a <code>py_test</code>) to some <code>cc_library</code> that it depends upon:</a>
98</li>
99<li> <a href="#Why_does_library_photos_fronten">Why does library <code>//photos/frontend:lib</code> depend on two variants of the same library <code>//third_party/jpeglib</code> and <code>//third_party/jpeg</code>?</a>
100</li>
101</ul>
102</li>
103<li> <a href="#What_depends_on_">What depends on ...</a>
104<ul>
105<li> <a href="#What_rules_under_bar_depend_o">What rules under bar depend on Y?</a>
106</li>
107</ul>
108</li>
109<li> <a href="#How_do_I_break_a_dependency_">How do I break a dependency ...</a>
110<ul>
111<li> <a href="#What_dependency_paths_do_I_have_">What dependency paths do I have to break to make <code>bar</code> no longer depend on X?</a>
112</li>
113</ul>
114</li>
115<li> <a href="#Misc_">Misc ...</a>
116<ul>
117<li> <a href="#How_many_sequential_steps_are_th">How many sequential steps are there in the <code>ServletSmokeTests</code> build?</a>
118</li>
119</ul>
120</li>
121</ul>
122</li>
123</ul>
124<p />
125<h2><a name="Finding_the_Dependencies_of_a_Ru"> </a> Finding the Dependencies of a Rule </h2>
126<p />
127To see the dependencies of <code>//src/main/java/com/example/base:base</code>, use the <code><b>deps</b></code> function in bazel query:
128<p />
129<pre>
130 $ <kbd>bazel query "deps(src/main/java/com/example/base:base)"</kbd>
131 //resources:translation.xml
132 //src/main/java/com/example/base:AbstractPublishedUri.java
133 <var>...</var>
134</pre>
135<p />
136 This is the set of all targets required to build <code>//src/main/java/com/example/base:base</code>.
137<p />
138<h2><a name="Tracing_the_Dependency_Chain_bet"> </a> Tracing the Dependency Chain between Two Packages </h2>
139<p />
140The library <code><b>//third_party/zlib:zlibonly</b></code> isn't in the BUILD file for <code>//src/main/java/com/example/base</code>, but it is an indirect dependency. How can we trace this dependency path? There are two useful functions here: <code>allpaths</code> and <code>somepath</code>
141<p />
142<pre class="prettyprint">
143$ <kbd>bazel query "somepath(src/main/java/com/example/base:base, third_party/zlib:zlibonly)"</kbd>
144//src/main/java/com/example/base:base
145//translations/tools:translator
146//translations/base:base
147//third_party/py/MySQL:MySQL
148//third_party/py/MySQL:_MySQL.so
149//third_party/mysql:mysql
150//third_party/zlib:zlibonly
151$ <kbd>bazel query "allpaths(src/main/java/com/example/common/base:base, third_party/...)"</kbd>
152 <var>...many errors detected in BUILD files...</var>
153//src/main/java/com/example/common/base:base
154//third_party/java/jsr166x:jsr166x
155//third_party/java/sun_servlet:sun_servlet
156//src/main/java/com/example/common/flags:flags
157//src/main/java/com/example/common/flags:base
158//translations/tools:translator
159//translations/tools:aggregator
160//translations/base:base
161//tools/pkg:pex
162//tools/pkg:pex_phase_one
163//tools/pkg:pex_lib
164//third_party/python:python_lib
165//translations/tools:messages
166//third_party/py/xml:xml
167//third_party/py/xml:utils/boolean.so
168//third_party/py/xml:parsers/sgmlop.so
169//third_party/py/xml:parsers/pyexpat.so
170//third_party/py/MySQL:MySQL
171//third_party/py/MySQL:_MySQL.so
172//third_party/mysql:mysql
173//third_party/openssl:openssl
174//third_party/zlib:zlibonly
175//third_party/zlib:zlibonly_v1_2_3
176//third_party/python:headers
177//third_party/openssl:crypto
178</pre>
179<p />
180<h3><a name="Aside_implicit_dependencies"> </a> Aside: implicit dependencies </h3>
181<div>
182<p />
183The BUILD file for <code>src/main/java/com/example/common/base</code> never references <code>//translations/tools:aggregator</code>. So, where's the direct dependency?
184<p />
185Certain rules include implicit dependencies on additional libraries or tools. For example, to build a <code>genproto</code> rule, you need first to build the Protocol Compiler, so every <code>genproto</code> rule carries an implicit dependency on the protocol compiler. These dependencies are not mentioned in the build file, but added in by the build tool. The full set of implicit dependencies is currently undocumented; read the source code of <a href="https://github.com/bazelbuild/bazel/tree/master/src/main/java/com/example/devtools/build/lib/packages/RuleClassProvider.java">RuleClassProvider</a>.)
186</div>
187<p />
188<h2><a name="Reverse_Dependencies"> </a> Reverse Dependencies </h2>
189<p />
190You might want to know the set of targets that depends on some target. e.g., if you're going to change some code, you might want to know what other code you're about to break. You can use <code>rdeps(u, x)</code> to find the reverse dependencies of the targets in <code>x</code> within the transitive closure of <code>u</code>.
191<p />
192Unfortunately, invoking, e.g.,
193<code>rdeps(..., daffie/annotations2:constants-lib)</code>
194is not practical for a large tree, because it requires parsing every BUILD file and building a very large dependency graph (Bazel may run out of memory). If you would like to execute this query across a large repository, you may have to query subtrees and then combine the results.
195<p />
196<h2><a name="Miscellaneous_Uses"> </a> Miscellaneous Uses </h2>
197<p />
198You can use <code>bazel query</code> to analyze many dependency relationships.
199<p />
200<h3><a name="What_exists_"> </a> What exists ... </h3>
201<p />
202<h4><a name="What_packages_exist_beneath_foo_"> </a> What packages exist beneath <code>foo</code>? </h4>
203<div>
204 <code>bazel query 'foo/...' --output package</code>
205</div>
206<p />
207<h4><a name="What_rules_are_defined_in_the_gw"> </a> What rules are defined in the <code>foo</code> package? </h4>
208<div>
209 <code>bazel query 'kind(rule, foo:all)' --output label_kind</code>
210</div>
211<p />
212<h4><a name="What_files_are_generated_by_rule"> </a> What files are generated by rules in the <code>foo</code> package? </h4>
213 <code>bazel query 'kind("generated file", //foo:*)'</code>
214<p />
215<h4><a name="What_s_the_set_of_BUILD_files_ne"> </a> What's the set of BUILD files needed to build <code>//foo</code>? </h4>
216<div>
217 <code>bazel query 'buildfiles(deps(//foo))' --output location | cut -f1 -d:</code>
218</div>
219<p />
220<h4><a name="What_are_the_individual_tests_th"> </a> What are the individual tests that a <code>test_suite</code> expands to? </h4>
221<div>
222 <code>bazel query 'tests(//foo:smoke_tests)'</code>
223</div>
224<p />
225<h5><a name="Which_of_those_are_C_tests_"> </a> Which of those are C++ tests? </h5>
226<div>
227 <code>bazel query 'kind(cc_.*, tests(//foo:smoke_tests))'</code>
228</div>
229<p />
230<h5><a name="Which_of_those_are_small_Medium_"> </a> Which of those are small? Medium? Large? </h5>
231<div>
232 <code>bazel query 'attr(size, small, tests(//foo:smoke_tests))'</code>
233<p />
234 <code>bazel query 'attr(size, medium, tests(//foo:smoke_tests))'</code>
235<p />
236 <code>bazel query 'attr(size, large, tests(//foo:smoke_tests))'</code>
237</div>
238<p />
239<p />
240<h4><a name="What_are_the_tests_beneath_foo_t"> </a> What are the tests beneath <code>foo</code> that match a pattern? </h4>
241 <code>bazel query 'filter("pa?t", kind(".*_test rule", //foo/...))'</code>
242The pattern is a regex and is applied to the full name of the rule. It's similar to doing
243 <code>bazel query 'kind(".*_test rule", //foo/...)' | grep -E 'pa?t'</code>
244<p />
245<h4><a name="What_package_contains_file_java_"> </a> What package contains file <code>src/main/java/com/example/cache/LRUCache.java</code>? </h4>
246 <code>bazel query 'buildfiles(src/main/java/com/example/cache/LRUCache.java)' --output=package</code>
247<p />
248<h4><a name="What_is_the_build_label_for_java"> </a> What is the build label for <code>src/main/java/com/example/cache/LRUCache.java</code>? </h4>
249 <code>bazel query src/main/java/com/example/cache/LRUCache.java</code>
250<p />
251<h4><a name="What_build_rule_contains_file_ja"> </a> What build rule contains file <code>src/main/java/com/example/cache/LRUCache.java</code> as a source? </h4>
252<div>
253<pre>
254fullname=$(bazel query src/main/java/com/example/cache/LRUCache.java)
255bazel query "attr('srcs', $fullname, ${fullname//:*/}:*)"
256</pre>
257</div>
258<p />
259<h3><a name="What_package_dependencies_exist_"> </a> What package dependencies exist ... </h3>
260<p />
261<h4><a name="What_packages_does_foo_depend_on"> </a> What packages does <code>foo</code> depend on? (What do I need to check out to build <code>foo</code>) </h4>
262<div>
263 <code>bazel query 'buildfiles(deps(//foo:foo))' --output package</code>
264<p />
265Note, <code>buildfiles</code> is required in order to correctly obtain all files
266referenced by <code>subinclude</code>; see the reference manual for details.
267</div>
268<p />
269<h4><a name="What_packages_does_the_foo_"> </a> What packages does the <code>foo</code> tree depend on, excluding <code>foo/contrib</code>? </h4>
270<div>
271 <code>bazel query 'deps(foo/... except foo/contrib/...)' --output package</code>
272</div>
273<p />
274<h3><a name="What_rule_dependencies_exist_"> </a> What rule dependencies exist ... </h3>
275<h4><a name="What_genproto_rules_does_bar_"> </a> What genproto rules does bar depend upon? </h4>
276<div>
277 <code>bazel query 'kind(genproto, deps(bar/...))'</code>
278</div>
279<p />
280<h4><a name="Find_the_definition_of_some_JNI_"> </a> Find the definition of some JNI (C++) library that is transitively depended upon by a Java binary rule in the servlet tree. </h4>
281<div>
282 <code>bazel query 'some(kind(cc_.*library, deps(kind(java_binary, src/main/java/com/example/frontend/...))))' --output location</code>
283</div>
284<p />
285<h5><a name="_Now_find_the_definitions_of_all"> </a> ...Now find the definitions of all the Java binaries that depend on them </h5>
286<div>
287 <pre>bazel query 'let jbs = kind(java_binary, src/main/java/com/example/frontend/...) in
288 let cls = kind(cc_.*library, deps($jbs)) in
289 $jbs intersect allpaths($jbs, $cls)'</pre>
290</div>
291<p />
292<p />
293<h3><a name="What_file_dependencies_exist_"> </a> What file dependencies exist ... </h3>
294<h4><a name="What_s_the_complete_set_of_Java_"> </a> What's the complete set of Java source files required to build QUX? </h4>
295<div>
296 Source files: <code>bazel query 'kind("source file", deps(src/main/java/com/example/qux/...))' | grep java$</code>
297<p />
298 Generated files: <code>bazel query 'kind("generated file", deps(src/main/java/com/example/qux/...))' | grep java$</code>
299</div>
300<p />
301<h4><a name="What_is_the_complete_set_of_Java"> </a> What is the complete set of Java source files required to build QUX's tests? </h4>
302<div>
303 Source files: <code>bazel query 'kind("source file", deps(kind(".*_test rule", javatests/com/example/qux/...)))' | grep java$</code>
304<p />
305 Generated files: <code>bazel query 'kind("generated file", deps(kind(".*_test rule", javatests/com/example/qux/...)))' | grep java$</code>
306</div>
307<p />
308<p />
309<h3><a name="What_differences_in_dependencies"> </a> What differences in dependencies between X and Y exist ... </h3>
310<h4><a name="What_targets_does_foo_depend_on_"> </a> What targets does <code>//foo</code> depend on that <code>//foo:foolib</code> does not? </h4>
311<div>
312 <code>bazel query 'deps(//foo) except deps(//foo:foolib)'</code>
313</div>
314<p />
315<h4><a name="What_C_libraries_do_the_foo_test"> </a> What C++ libraries do the <code>foo</code> tests depend on that the <code>//foo</code> production binary does <em>not</em> depend on? </h4>
316<div>
317 <code>bazel query 'kind("cc_library", deps(kind(".*test rule", foo/...)) except deps(//foo))'</code>
318</div>
319<p />
320<h3><a name="Why_does_this_dependency_exist_"> </a> Why does this dependency exist ... </h3>
321<p />
322<h4><a name="Why_does_bar_depend_on_groups"> </a> Why does <code>bar</code> depend on <code>groups2</code>? </h4>
323<div>
324<p />
325 <code>bazel query 'somepath(bar/...,groups2/...:*)'</code>
326<p />
327 Once you have the results of this query, you will often find that a
328 single target stands out as being an unexpected or egregious and
329 undesirable dependency of <code>bar</code>. The query can then
330 be further refined to:
331</div>
332<p />
333<h4><a name="Show_me_a_path_from_docker_updater"> </a> Show me a path from <code>docker/updater:updater_systest</code> (a <code>py_test</code>) to some <code>cc_library</code> that it depends upon: </h4>
334<div>
335<pre>bazel query 'let cc = kind(cc_library, deps(docker/updater:updater_systest)) in
336 somepath(docker/updater:updater_systest, $cc)'</pre>
337</div>
338<p />
339<h4><a name="Why_does_library_photos_fronten"> </a> Why does library <code>//photos/frontend:lib</code> depend on two variants of the same library <code>//third_party/jpeglib</code> and <code>//third_party/jpeg</code>? </h4>
340<div>
341This query boils down to: "show me the subgraph of
342<code>//photos/frontend:lib</code> that depends on both libraries". When shown
343in topological order, the last element of the result is the most
344likely culprit.
345<p />
346<pre>
347% bazel query 'allpaths(//photos/frontend:lib, //third_party/jpeglib)
348 intersect
349 allpaths(//photos/frontend:lib, //third_party/jpeg)'
350//photos/frontend:lib
351//photos/frontend:lib_impl
352//photos/frontend:lib_dispatcher
353//photos/frontend:icons
354//photos/frontend/modules/gadgets:gadget_icon
355//photos/thumbnailer:thumbnail_lib
356//third_party/jpeg/img:renderer
357</pre>
358</div>
359<p />
360<h3><a name="What_depends_on_"> </a> What depends on ... </h3>
361<h4><a name="What_rules_under_bar_depend_o"> </a> What rules under bar depend on Y? </h4>
362<div>
363 <code>bazel query 'bar/... intersect allpaths(bar/..., Y)'</code>
364<p />
365 Note: <code>X intersect allpaths(X, Y)</code> is the general idiom for the query "which X depend on Y?"
366 If expression X is non-trivial, it may be convenient to bind a name to it using
367 <code>let</code> to avoid duplication.
368</div>
369<p />
370<h3><a name="How_do_I_break_a_dependency_"> </a> How do I break a dependency ... </h3>
371<!-- TODO find a convincing value of X to plug in here -->
372<h4><a name="What_dependency_paths_do_I_have_"> </a> What dependency paths do I have to break to make <code>bar</code> no longer depend on X? </h4>
373<div>
374To output the graph to a <code>png</code> file:
375<p />
376<pre>
377bazel query 'allpaths(bar/...,X)' --output graph | dot -Tpng > /tmp/dep.png
378</pre>
379</div>
380<p />
381<h3><a name="Misc_"> </a> Misc ... </h3>
382<h4><a name="How_many_sequential_steps_are_th"> </a> How many sequential steps are there in the <code>ServletSmokeTests</code> build? </h4>
383<div>
384Unfortunately, the query language can't currently give you the longest
385path from x to y, but it can find the (or rather <i>a</i>) most
386distant node from the starting point, or show you the <em>lengths</em> of the
387longest path from x to every y that it depends on. Use <code>maxrank</code>:
388<p />
389<pre> % bazel query 'deps(//src/test/java/com/example/servlet:ServletSmokeTests)'
390--output maxrank | tail -1
39185 //third_party/zlib:zutil.c
392</pre>
393<p />
394The result indicates that there exist paths of length 85 that must
395occur in order in this build.
396</div>
397<p />
398</table>
399<p />