diff options
author | juliexxia <juliexxia@google.com> | 2018-04-25 09:48:08 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-04-25 09:49:35 -0700 |
commit | e6524b59650f033ef7652c7ff8aa68877f82208d (patch) | |
tree | dcc1a01425d4191370c57d3c56e81da6a2a1497c /site | |
parent | 618ab2c70ec2d9726fb21a98ba25943f13e0d6e8 (diff) |
Delete query how-to goowiki and migrate external query how-to page to html so it can be shared internally as well.
PiperOrigin-RevId: 194253979
Diffstat (limited to 'site')
-rw-r--r-- | site/docs/query-how-to.html | 456 | ||||
-rw-r--r-- | site/docs/query-how-to.md | 472 |
2 files changed, 456 insertions, 472 deletions
diff --git a/site/docs/query-how-to.html b/site/docs/query-how-to.html new file mode 100644 index 0000000000..9dc104cbb1 --- /dev/null +++ b/site/docs/query-how-to.html @@ -0,0 +1,456 @@ +--- +layout: documentation +title: Bazel Query How-To +--- +<h1>Bazel query how-to</h1> + +<p> + This is a quick tutorial to get you started using Bazel's query language to + trace dependencies in your code. +</p> + +<p> + For a language details and <code>--output</code> flag details, please see the reference + manuals, <a href="query.html">Bazel query reference</a> and <a href="cquery.html">Bazel cquery reference</a>. + You can get help by typing <code>bazel help query</code> or <code>bazel help cquery</code> on the command line. +</p> + +<p> + To execute a query while ignoring errors such as missing targets, use the + <code>--keep_going</code> flag. +</p> + +<h2>Contents</h2> + +<ul> +<li><a href="#Finding_the_Dependencies_of_a_Ru">Finding the Dependencies of a Rule</a></li> +<li><a href="#Tracing_the_Dependency_Chain_bet">Tracing the Dependency Chain between Two +Packages</a> +<ul><li><a href="#Aside_implicit_dependencies">Aside: implicit dependencies</a></li></ul></li> +<li><a href="#Reverse_Dependencies">Reverse Dependencies</a></li> +<li><a href="#Miscellaneous_Uses">Miscellaneous Uses</a> +<ul><li><a href="#What_exists_">What exists ...</a> +<ul><li><a href="#What_packages_exist_beneath_foo_">What packages exist beneath +<code>foo</code>?</a></li> +<li><a href="#What_rules_are_defined_in_the_foo">What rules are defined in the <code>foo</code> +package?</a></li> +<li><a href="#What_files_are_generated_by_rule">What files are generated by rules in the <code>foo</code> +package?</a></li> +<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></li> +<li><a href="#What_are_the_individual_tests_th">What are the individual tests that a <code>test_suite</code> expands +to?</a> +<ul><li><a href="#Which_of_those_are_C_tests_">Which of those are C++ tests?</a></li> +<li><a href="#Which_of_those_are_small_Medium_">Which of those are small? Medium? +Large?</a></li></ul></li> +<li><a href="#What_are_the_tests_beneath_foo_t">What are the tests beneath <code>foo</code> that match a +pattern?</a></li> +<li><a href="#What_package_contains_file_java_">What package contains file +<code>//path/to/file/bar.java</code>? +</a></li> +<li><a href="#What_is_the_build_label_for_java">What is the build label for +<code>//path/to/file/bar.java</code>? +</a></li> +<li><a href="#What_build_rule_contains_file_ja">What build rule contains file +<code>//path/to/file/bar.java</code> as a +source?</a></li></ul></li> +<li><a href="#What_package_dependencies_exist_">What package dependencies exist ...</a> +<ul><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></li> +<li><a href="#What_packages_does_the_foo_">What packages does the <code>foo</code> tree depend on, excluding +<code>foo/contrib</code>?</a></li></ul></li> +<li><a href="#What_rule_dependencies_exist_">What rule dependencies exist ...</a> +<ul><li><a href="#What_genproto_rules_does_bar_">What genproto rules does bar depend +upon?</a></li> +<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> +<ul><li><a href="#_Now_find_the_definitions_of_all">...Now find the definitions of all the Java binaries that +depend on them</a></li></ul></li></ul></li> +<li><a href="#What_file_dependencies_exist_">What file dependencies exist ...</a> +<ul><li><a href="#What_s_the_complete_set_of_Java_">What's the complete set of Java source files required to build +QUX?</a></li> +<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></li></ul></li> +<li><a href="#What_differences_in_dependencies">What differences in dependencies between X and Y exist +...</a> +<ul><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></li> +<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></li></ul></li> +<li><a href="#Why_does_this_dependency_exist_">Why does this dependency exist ...</a> +<ul><li><a href="#Why_does_bar_depend_on_groups">Why does <code>bar</code> depend on +<code>groups2</code>?</a></li> +<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></li> +<li><a href="#Why_does_library_photos_frontend">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></li></ul></li> +<li><a href="#What_depends_on_">What depends on ...</a> +<ul><li><a href="#What_rules_under_bar_depend_o">What rules under bar depend on Y?</a></li> +<li><a href="#What_are_the_intra_package_direct_rdeps">What targets directly depend on T, in T's +package</a></li></ul></li> +<li><a href="#How_do_I_break_a_dependency_">How do I break a dependency ...</a> +<ul><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></li></ul></li> +<li><a href="#Misc_">Misc ...</a> +<ul><li><a href="#How_many_sequential_steps_are_th">How many sequential steps are there in the <code>ServletSmokeTests</code> +build?</a></li></ul></li></ul></li> +</ul> + +<p><a name="Finding_the_Dependencies_of_a_Ru"></a></p> + +<h2>Finding the Dependencies of a Rule</h2> + +<p>To see the dependencies of <code>//foo</code>, use the +<code>deps</code> function in bazel query:</p> + +<pre> +$ bazel query "deps(//foo)" +//foo:foo +//foo:foo-dep +... +</pre> + +<p>This is the set of all targets required to build <code>//foo</code>.</p> + +<p><a name="Tracing_the_Dependency_Chain_bet"></a></p> + +<h2>Tracing the Dependency Chain between Two Packages</h2> + +<p>The library <code>//third_party/zlib:zlibonly</code> isn't in the BUILD file for +<code>//foo</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></p> + +<pre> +$ bazel query "somepath(//foo:foo, third_party/zlib:zlibonly)" +//foo:foo +//translations/tools:translator +//translations/base:base +//third_party/py/MySQL:MySQL +//third_party/py/MySQL:_MySQL.so +//third_party/mysql:mysql +//third_party/zlib:zlibonly +$ bazel query "allpaths(//foo, third_party/...)" + ...many errors detected in BUILD files... +//foo:foo +//translations/tools:translator +//translations/tools:aggregator +//translations/base:base +//tools/pkg:pex +//tools/pkg:pex_phase_one +//tools/pkg:pex_lib +//third_party/python:python_lib +//translations/tools:messages +//third_party/py/xml:xml +//third_party/py/xml:utils/boolean.so +//third_party/py/xml:parsers/sgmlop.so +//third_party/py/xml:parsers/pyexpat.so +//third_party/py/MySQL:MySQL +//third_party/py/MySQL:_MySQL.so +//third_party/mysql:mysql +//third_party/openssl:openssl +//third_party/zlib:zlibonly +//third_party/zlib:zlibonly_v1_2_3 +//third_party/python:headers +//third_party/openssl:crypto +</pre> + +<p><a name="Aside_implicit_dependencies"></a></p> + +<h3>Aside: implicit dependencies</h3> + +<p>The BUILD file for <code>//foo</code> never references +<code>//translations/tools:aggregator</code>. So, where's the direct dependency?</p> + +<p>Certain 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. Using <code>--noimplicit_deps</code> allows you to filter out + these deps from your query results. + +<p><a name="Reverse_Dependencies"></a></p> + +<h2>Reverse Dependencies</h2> + +<p>You 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>.</p> + +Bazel's <a href="https://docs.bazel.build/versions/master/query.html#sky-query">Sky Query</a> allows +supports the <code>allrdeps</code> function which allows you to query rdeps in the entire +universe of the build. +<p><a name="Miscellaneous_Uses"></a></p> + +<h2>Miscellaneous Uses</h2> + +<p>You can use <code>bazel query</code> to analyze many dependency relationships.</p> + +<p><a name="What_exists_"></a></p> + +<h3>What exists ...</h3> + +<p><a name="What_packages_exist_beneath_foo_"></a></p> + +<h4>What packages exist beneath <code>foo</code>?</h4> + +<pre>bazel query 'foo/...' --output package</pre> + +<p><a name="What_rules_are_defined_in_the_gw"></a></p> + +<h4>What rules are defined in the <code>foo</code> package?</h4> + +<pre>bazel query 'kind(rule, foo:all)' --output label_kind</pre> + +<p><a name="What_files_are_generated_by_rule"></a></p> + +<h4>What files are generated by rules in the <code>foo</code> package?</h4> + +<pre>bazel query 'kind("generated file", //foo:*)'</pre> + +<p><a name="What_s_the_set_of_BUILD_files_ne"></a></p> + +<h4>What's the set of BUILD files needed to build <code>//foo</code>?</h4> + +<pre>bazel query 'buildfiles(deps(//foo))' --output location | cut -f1 -d:</pre> + +<p><a name="What_are_the_individual_tests_th"></a></p> + +<h4>What are the individual tests that a <code>test_suite</code> expands to?</h4> + +<pre>bazel query 'tests(//foo:smoke_tests)'</pre> + +<p><a name="Which_of_those_are_C_tests_"></a></p> + +<h4>Which of those are C++ tests?</h4> + +<pre>bazel query 'kind(cc_.*, tests(//foo:smoke_tests))'</pre> + +<p><a name="Which_of_those_are_small_Medium_"></a></p> + +<h4>Which of those are small? Medium? Large?</h4> + +<pre> +bazel query 'attr(size, small, tests(//foo:smoke_tests))' + +bazel query 'attr(size, medium, tests(//foo:smoke_tests))' + +bazel query 'attr(size, large, tests(//foo:smoke_tests))' +</pre> + +<p><a name="What_are_the_tests_beneath_foo_t"></a></p> + +<h4>What are the tests beneath <code>foo</code> that match a pattern?</h4> + +<pre>bazel query 'filter("pa?t", kind(".*_test rule", //foo/...))'</pre> + +<p>The pattern is a regex and is applied to the full name of the rule. It's similar to doing</p> + +<pre>bazel query 'kind(".*_test rule", //foo/...)' | grep -E 'pa?t'</pre> + +<p><a name="What_package_contains_file_java_"></a></p> + +<h4>What package contains file //path/to/file/bar.java`?</h4> + +<pre> bazel query 'buildfiles(//path/to/file/bar.java)' --output=package</pre> + +<p><a name="What_is_the_build_label_for_java"></a></p> + +<h4>What is the build label for //path/to/file/bar.java?</h4> + +<pre>bazel query //path/to/file/bar.java</pre> + +<p><a name="What_build_rule_contains_file_ja"></a></p> + +<h4>What rule target(s) contain file <code>//path/to/file/bar.java</code> as a source?</h4> + +<pre> +fullname=$(bazel query //path/to/file/bar.java) +bazel query "attr('srcs', $fullname, ${fullname//:*/}:*)" +</pre> + +<p><a name="What_package_dependencies_exist_"></a></p> + +<h3>What package dependencies exist ...</h3> + +<p><a name="What_packages_does_foo_depend_on"></a></p> + +<h4>What packages does <code>foo</code> depend on? (What do I need to check out to build <code>foo</code>)</h4> + +<pre>bazel query 'buildfiles(deps(//foo:foo))' --output package</pre> + +<p>Note, <code>buildfiles</code> is required in order to correctly obtain all files +referenced by <code>subinclude</code>; see the reference manual for details.</p> + +<p><a name="What_packages_does_the_foo_"></a></p> + +<h4>What packages does the <code>foo</code> tree depend on, excluding <code>foo/contrib</code>?</h4> + +<pre>bazel query 'deps(foo/... except foo/contrib/...)' --output package</pre> + +<p><a name="What_rule_dependencies_exist_"></a></p> + +<h3>What rule dependencies exist ...</h3> + +<p><a name="What_genproto_rules_does_bar_"></a></p> + +<h4>What genproto rules does bar depend upon?</h4> + +<pre>bazel query 'kind(genproto, deps(bar/...))'</pre> + +<p><a name="Find_the_definition_of_some_JNI_"></a></p> + +<h4>Find the definition of some JNI (C++) library that is transitively depended upon by a Java binary rule in the servlet tree.</h4> + +<pre>bazel query 'some(kind(cc_.*library, deps(kind(java_binary, //java/com/example/frontend/...))))' --output location</pre> +<p><a name="_Now_find_the_definitions_of_all"></a></p> + +<h5>...Now find the definitions of all the Java binaries that depend on them</h5> + +<pre>bazel query 'let jbs = kind(java_binary, //java/com/example/frontend/...) in + let cls = kind(cc_.*library, deps($jbs)) in + $jbs intersect allpaths($jbs, $cls)' +</pre> + +<p><a name="What_file_dependencies_exist_"></a></p> + +<h3>What file dependencies exist ...</h3> + +<p><a name="What_s_the_complete_set_of_Java_"></a></p> + +<h4>What's the complete set of Java source files required to build foo?</h4> + +<p>Source files:</p> + +<pre>bazel query 'kind("source file", deps(//path/to/target/foo/...))' | grep java$</pre> + +<p>Generated files:</p> + +<pre>bazel query 'kind("generated file", deps(//path/to/target/foo/...))' | grep java$</pre> + +<p><a name="What_is_the_complete_set_of_Java"></a></p> + +<h4>What is the complete set of Java source files required to build QUX's tests?</h4> + +<p>Source files:</p> + +<pre>bazel query 'kind("source file", deps(kind(".*_test rule", javatests/com/example/qux/...)))' | grep java$</pre> + +<p>Generated files:</p> + +<pre>bazel query 'kind("generated file", deps(kind(".*_test rule", javatests/com/example/qux/...)))' | grep java$</pre> + +<p><a name="What_differences_in_dependencies"></a></p> + +<h3>What differences in dependencies between X and Y exist ...</h3> + +<p><a name="What_targets_does_foo_depend_on_"></a></p> + +<h4>What targets does <code>//foo</code> depend on that <code>//foo:foolib</code> does not?</h4> + +<pre>bazel query 'deps(//foo) except deps(//foo:foolib)'</pre> + +<p><a name="What_C_libraries_do_the_foo_test"></a></p> + +<h4>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> + +<pre>bazel query 'kind("cc_library", deps(kind(".*test rule", foo/...)) except deps(//foo))'</pre> + +<p><a name="Why_does_this_dependency_exist_"></a></p> + +<h3>Why does this dependency exist ...</h3> + +<p><a name="Why_does_bar_depend_on_groups"></a></p> + +<h4>Why does <code>bar</code> depend on <code>groups2</code>?</h4> + +<pre>bazel query 'somepath(bar/...,groups2/...:*)'</pre> + +<p>Once you have the results of this query, you will often find that a single +target stands out as being an unexpected or egregious and undesirable +dependency of <code>bar</code>. The query can then be further refined to:</p> + +<p><a name="Show_me_a_path_from_docker_updater"></a></p> + +<h4>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> + +<pre>bazel query 'let cc = kind(cc_library, deps(docker/updater:updater_systest)) in + somepath(docker/updater:updater_systest, $cc)'</pre> + +<p><a name="Why_does_library_photos_frontend"></a></p> + +<h4>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> + +<p>This query boils down to: "show me the subgraph of <code>//photos/frontend:lib</code> that +depends on both libraries". When shown in topological order, the last element +of the result is the most likely culprit.</p> + +<pre>bazel query 'allpaths(//photos/frontend:lib, //third_party/jpeglib) + intersect + allpaths(//photos/frontend:lib, //third_party/jpeg)' +//photos/frontend:lib +//photos/frontend:lib_impl +//photos/frontend:lib_dispatcher +//photos/frontend:icons +//photos/frontend/modules/gadgets:gadget_icon +//photos/thumbnailer:thumbnail_lib +//third_party/jpeg/img:renderer +</pre> + +<p><a name="What_depends_on_"></a></p> + +<h3>What depends on ...</h3> + +<p><a name="What_rules_under_bar_depend_o"></a></p> + +<h4>What rules under bar depend on Y?</h4> + +<pre>bazel query 'bar/... intersect allpaths(bar/..., Y)'</pre> + +<p>Note: <code>X intersect allpaths(X, Y)</code> is the general idiom for the query "which X +depend on Y?" If expression X is non-trivial, it may be convenient to bind a +name to it using <code>let</code> to avoid duplication.</p> + +<p><a name="What_are_the_intra_package_direct_rdeps"></a></p> + +<h4>What targets directly depend on T, in T's package?</h4> + +<pre>bazel query 'let t = T in rdeps(siblings($t), $t, 1)'</pre> + +<p><a name="How_do_I_break_a_dependency_"></a></p> + +<h3>How do I break a dependency ...</h3> + +<p><!-- TODO find a convincing value of X to plug in here --> +<a name="What_dependency_paths_do_I_have_"></a></p> + +<h4>What dependency paths do I have to break to make <code>bar</code> no longer depend on X?</h4> + +<p>To output the graph to a <code>png</code> file:</p> + +<pre>bazel query 'allpaths(bar/...,X)' --output graph | dot -Tpng > /tmp/dep.png</pre> + +<p><a name="Misc_"></a></p> + +<h3>Misc ...</h3> + +<p><a name="How_many_sequential_steps_are_th"></a></p> + +<h4>How many sequential steps are there in the <code>//foo-tests</code> build?</h4> + +<p>Unfortunately, the query language can't currently give you the longest path +from x to y, but it can find the (or rather <em>a</em>) most distant node from the +starting point, or show you the <em>lengths</em> of the longest path from x to every +y that it depends on. Use <code>maxrank</code>:</p> + +<pre>bazel query 'deps(//foo-tests)' --output maxrank | tail -1 +85 //third_party/zlib:zutil.c</pre> + +<p>The result indicates that there exist paths of length 85 that must occur in +order in this build.</p> diff --git a/site/docs/query-how-to.md b/site/docs/query-how-to.md deleted file mode 100644 index 5b2b352ecd..0000000000 --- a/site/docs/query-how-to.md +++ /dev/null @@ -1,472 +0,0 @@ ---- -layout: documentation -title: Query how-to ---- - -<a name="_Bazel_Query_How_To"> </a> -# Bazel query how-to - -This is a quick tutorial to get you started using Bazel's query language to -trace dependencies in your code. - -For a language details and `--output` flag details, please see the reference -manuals, [Bazel query reference](query.html) and [Bazel cquery reference](cquery.html). You can get help for Bazel query and cquery -by typing `bazel help query` or `bazel help cquery`. - -To execute a query while ignoring errors such as missing targets, use the -`--keep_going` flag. - -<a name="_Contents"></a> -## Contents - -* [Finding the Dependencies of a Rule](#Finding_the_Dependencies_of_a_Ru) -* [Tracing the Dependency Chain between Two - Packages](#Tracing_the_Dependency_Chain_bet) - * [Aside: implicit dependencies](#Aside_implicit_dependencies) -* [Reverse Dependencies](#Reverse_Dependencies) -* [Miscellaneous Uses](#Miscellaneous_Uses) - * [What exists ...](#What_exists_) - * [What packages exist beneath - `foo`?](#What_packages_exist_beneath_foo_) - * [What rules are defined in the `foo` - package?](#What_rules_are_defined_in_the_foo) - * [What files are generated by rules in the `foo` - package?](#What_files_are_generated_by_rule) - * [What's the set of BUILD files needed to build - `//foo`?](#What_s_the_set_of_BUILD_files_ne) - * [What are the individual tests that a `test_suite` expands - to?](#What_are_the_individual_tests_th) - * [Which of those are C++ tests?](#Which_of_those_are_C_tests_) - * [Which of those are small? Medium? - Large?](#Which_of_those_are_small_Medium_) - * [What are the tests beneath `foo` that match a - pattern?](#What_are_the_tests_beneath_foo_t) - * [What package contains file - `src/main/java/com/example/cache/LRUCache.java`? - ](#What_package_contains_file_java_) - * [What is the build label for - `src/main/java/com/example/cache/LRUCache.java`? - ](#What_is_the_build_label_for_java) - * [What build rule contains file - `src/main/java/com/example/cache/LRUCache.java` as a - source?](#What_build_rule_contains_file_ja) - * [What package dependencies exist ...](#What_package_dependencies_exist_) - * [What packages does `foo` depend on? (What do I need to check out - to build `foo`)](#What_packages_does_foo_depend_on) - * [What packages does the `foo` tree depend on, excluding - `foo/contrib`?](#What_packages_does_the_foo_) - * [What rule dependencies exist ...](#What_rule_dependencies_exist_) - * [What genproto rules does bar depend - upon?](#What_genproto_rules_does_bar_) - * [Find the definition of some JNI (C++) library that is transitively - depended upon by a Java binary rule in the servlet - tree.](#Find_the_definition_of_some_JNI_) - * [...Now find the definitions of all the Java binaries that - depend on them](#_Now_find_the_definitions_of_all) - * [What file dependencies exist ...](#What_file_dependencies_exist_) - * [What's the complete set of Java source files required to build - QUX?](#What_s_the_complete_set_of_Java_) - * [What is the complete set of Java source files required to build - QUX's tests?](#What_is_the_complete_set_of_Java) - * [What differences in dependencies between X and Y exist - ...](#What_differences_in_dependencies) - * [What targets does `//foo` depend on that `//foo:foolib` does - not?](#What_targets_does_foo_depend_on_) - * [What C++ libraries do the `foo` tests depend on that the `//foo` - production binary does _not_ depend - on?](#What_C_libraries_do_the_foo_test) - * [Why does this dependency exist ...](#Why_does_this_dependency_exist_) - * [Why does `bar` depend on - `groups2`?](#Why_does_bar_depend_on_groups) - * [Show me a path from `docker/updater:updater_systest` (a `py_test`) - to some `cc_library` that it depends - upon:](#Show_me_a_path_from_docker_updater) - * [Why does library `//photos/frontend:lib` depend on two variants of - the same library `//third_party/jpeglib` and - `//third_party/jpeg`?](#Why_does_library_photos_frontend) - * [What depends on ...](#What_depends_on_) - * [What rules under bar depend on Y?](#What_rules_under_bar_depend_o) - * [What targets directly depend on T, in T's - package](#What_are_the_intra_package_direct_rdeps) - * [How do I break a dependency ...](#How_do_I_break_a_dependency_) - * [What dependency paths do I have to break to make `bar` no longer - depend on X?](#What_dependency_paths_do_I_have_) - * [Misc ...](#Misc_) - * [How many sequential steps are there in the `ServletSmokeTests` - build?](#How_many_sequential_steps_are_th) - -<a name="Finding_the_Dependencies_of_a_Ru"></a> -## Finding the Dependencies of a Rule - -To see the dependencies of `//src/main/java/com/example/base:base`, use the -`deps` function in bazel query: - -``` -$ bazel query "deps(src/main/java/com/example/base:base)" -//resources:translation.xml -//src/main/java/com/example/base:AbstractPublishedUri.java -... -``` - -This is the set of all targets required to build <code>//src/main/java/com/example/base:base</code>. - -<a name="Tracing_the_Dependency_Chain_bet"></a> -## Tracing the Dependency Chain between Two Packages - -The library `//third_party/zlib:zlibonly` isn't in the BUILD file for -`//src/main/java/com/example/base`, but it is an indirect dependency. How can -we trace this dependency path? There are two useful functions here: -`allpaths` and `somepath` - -``` -$ bazel query "somepath(src/main/java/com/example/base:base, third_party/zlib:zlibonly)" -//src/main/java/com/example/base:base -//translations/tools:translator -//translations/base:base -//third_party/py/MySQL:MySQL -//third_party/py/MySQL:_MySQL.so -//third_party/mysql:mysql -//third_party/zlib:zlibonly -$ bazel query "allpaths(src/main/java/com/example/common/base:base, third_party/...)" - ...many errors detected in BUILD files... -//src/main/java/com/example/common/base:base -//third_party/java/jsr166x:jsr166x -//third_party/java/sun_servlet:sun_servlet -//src/main/java/com/example/common/flags:flags -//src/main/java/com/example/common/flags:base -//translations/tools:translator -//translations/tools:aggregator -//translations/base:base -//tools/pkg:pex -//tools/pkg:pex_phase_one -//tools/pkg:pex_lib -//third_party/python:python_lib -//translations/tools:messages -//third_party/py/xml:xml -//third_party/py/xml:utils/boolean.so -//third_party/py/xml:parsers/sgmlop.so -//third_party/py/xml:parsers/pyexpat.so -//third_party/py/MySQL:MySQL -//third_party/py/MySQL:_MySQL.so -//third_party/mysql:mysql -//third_party/openssl:openssl -//third_party/zlib:zlibonly -//third_party/zlib:zlibonly_v1_2_3 -//third_party/python:headers -//third_party/openssl:crypto -``` - -<a name="Aside_implicit_dependencies"></a> -### Aside: implicit dependencies - -The BUILD file for `src/main/java/com/example/common/base` never references -`//translations/tools:aggregator`. So, where's the direct dependency? - -Certain rules include implicit dependencies on additional libraries or tools. -For example, to build a `genproto` rule, you need first to build the Protocol -Compiler, so every `genproto` 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 -[`RuleClassProvider`](https://github.com/bazelbuild/bazel/tree/master/src/main/java/com/google/devtools/build/lib/packages/RuleClassProvider.java). - -<a name="Reverse_Dependencies"></a> -## Reverse Dependencies - -You 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 `rdeps(u, x)` to find the reverse -dependencies of the targets in `x` within the transitive closure of `u`. - -Unfortunately, invoking, e.g., `rdeps(..., daffie/annotations2:constants-lib)` -is 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. - -<a name="Miscellaneous_Uses"></a> -## Miscellaneous Uses - -You can use `bazel query` to analyze many dependency relationships. - -<a name="What_exists_"></a> -### What exists ... - -<a name="What_packages_exist_beneath_foo_"></a> -#### What packages exist beneath `foo`? - -```sh -bazel query 'foo/...' --output package -``` - -<a name="What_rules_are_defined_in_the_gw"></a> -#### What rules are defined in the `foo` package? - -```sh -bazel query 'kind(rule, foo:all)' --output label_kind -``` - -<a name="What_files_are_generated_by_rule"></a> -#### What files are generated by rules in the `foo` package? - -```sh -bazel query 'kind("generated file", //foo:*)' -``` - -<a name="What_s_the_set_of_BUILD_files_ne"></a> -#### What's the set of BUILD files needed to build `//foo`? - -```sh -bazel query 'buildfiles(deps(//foo))' | cut -f1 -d: -``` - -<a name="What_are_the_individual_tests_th"></a> -#### What are the individual tests that a `test_suite` expands to? - -```sh -bazel query 'tests(//foo:smoke_tests)' -``` - -<a name="Which_of_those_are_C_tests_"></a> -##### Which of those are C++ tests? - -```sh -bazel query 'kind(cc_.*, tests(//foo:smoke_tests))' -``` - -<a name="Which_of_those_are_small_Medium_"></a> -#### Which of those are small? Medium? Large? - -```sh -bazel query 'attr(size, small, tests(//foo:smoke_tests))' - -bazel query 'attr(size, medium, tests(//foo:smoke_tests))' - -bazel query 'attr(size, large, tests(//foo:smoke_tests))' -``` - -<a name="What_are_the_tests_beneath_foo_t"></a> -#### What are the tests beneath `foo` that match a pattern? - -```sh -bazel query 'filter("pa?t", kind(".*_test rule", //foo/...))' -``` - -The pattern is a regex and is applied to the full name of the rule. It's similar to doing - -```sh -bazel query 'kind(".*_test rule", //foo/...)' | grep -E 'pa?t' -``` - -<a name="What_package_contains_file_java_"></a> -#### What package contains file src/main/java/com/example/cache/LRUCache.java`? - -```sh -bazel query 'buildfiles(src/main/java/com/example/cache/LRUCache.java)' --output=package -``` - -<a name="What_is_the_build_label_for_java"></a> -#### What is the build label for src/main/java/com/example/cache/LRUCache.java? - -```sh -bazel query src/main/java/com/example/cache/LRUCache.java -``` - -<a name="What_build_rule_contains_file_ja"></a> -#### What rule target(s) contain file `src/main/java/com/example/cache/LRUCache.java` as a source? - -```sh -fullname=$(bazel query src/main/java/com/example/cache/LRUCache.java) -bazel query "attr('srcs', $fullname, ${fullname//:*/}:*)" -``` - -<a name="What_package_dependencies_exist_"></a> -### What package dependencies exist ... - -<a name="What_packages_does_foo_depend_on"></a> -#### What packages does `foo` depend on? (What do I need to check out to build `foo`) - -```sh -bazel query 'buildfiles(deps(//foo:foo))' --output package -``` - -Note, `buildfiles` is required in order to correctly obtain all files -referenced by `subinclude`; see the reference manual for details. - -<a name="What_packages_does_the_foo_"></a> -#### What packages does the `foo` tree depend on, excluding `foo/contrib`? - -```sh -bazel query 'deps(foo/... except foo/contrib/...)' --output package -``` - -<a name="What_rule_dependencies_exist_"></a> -### What rule dependencies exist ... - -<a name="What_genproto_rules_does_bar_"></a> -#### What genproto rules does bar depend upon? - -```sh -bazel query 'kind(genproto, deps(bar/...))' -``` - -<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. - -```sh -bazel query 'some(kind(cc_.*library, deps(kind(java_binary, src/main/java/com/example/frontend/...))))' --output location -``` - -<a name="_Now_find_the_definitions_of_all"></a> -##### ...Now find the definitions of all the Java binaries that depend on them - -```sh -bazel query 'let jbs = kind(java_binary, src/main/java/com/example/frontend/...) in - let cls = kind(cc_.*library, deps($jbs)) in - $jbs intersect allpaths($jbs, $cls)' -``` - -<a name="What_file_dependencies_exist_"></a> -### What file dependencies exist ... - -<a name="What_s_the_complete_set_of_Java_"></a> -#### What's the complete set of Java source files required to build QUX? - -Source files: - -```sh -bazel query 'kind("source file", deps(src/main/java/com/example/qux/...))' | grep java$ -``` - -Generated files: - -```sh -bazel query 'kind("generated file", deps(src/main/java/com/example/qux/...))' | grep java$ -``` - -<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? - -Source files: - -```sh -bazel query 'kind("source file", deps(kind(".*_test rule", javatests/com/example/qux/...)))' | grep java$ -``` - -Generated files: - -```sh -bazel query 'kind("generated file", deps(kind(".*_test rule", javatests/com/example/qux/...)))' | grep java$ -``` - -<a name="What_differences_in_dependencies"></a> -### What differences in dependencies between X and Y exist ... - -<a name="What_targets_does_foo_depend_on_"></a> -#### What targets does `//foo` depend on that `//foo:foolib` does not? - -```sh -bazel query 'deps(//foo) except deps(//foo:foolib)' -``` - -<a name="What_C_libraries_do_the_foo_test"></a> -#### What C++ libraries do the `foo` tests depend on that the `//foo` production binary does _not_ depend on? - -```sh -bazel query 'kind("cc_library", deps(kind(".*test rule", foo/...)) except deps(//foo))' -``` - -<a name="Why_does_this_dependency_exist_"></a> -### Why does this dependency exist ... - -<a name="Why_does_bar_depend_on_groups"></a> -#### Why does <code>bar</code> depend on <code>groups2</code>? - -```sh -bazel query 'somepath(bar/...,groups2/...:*)' -``` - -Once you have the results of this query, you will often find that a single -target stands out as being an unexpected or egregious and undesirable -dependency of `bar`. The query can then be further refined to: - -<a name="Show_me_a_path_from_docker_updater"></a> -#### Show me a path from `docker/updater:updater_systest` (a `py_test`) to some `cc_library` that it depends upon: - -```sh -bazel query 'let cc = kind(cc_library, deps(docker/updater:updater_systest)) in - somepath(docker/updater:updater_systest, $cc)' -``` - -<a name="Why_does_library_photos_frontend"></a> -#### Why does library `//photos/frontend:lib` depend on two variants of the same library `//third_party/jpeglib` and `//third_party/jpeg`? - -This query boils down to: "show me the subgraph of `//photos/frontend:lib` that -depends on both libraries". When shown in topological order, the last element -of the result is the most likely culprit. - -``` -% bazel query 'allpaths(//photos/frontend:lib, //third_party/jpeglib) - intersect - allpaths(//photos/frontend:lib, //third_party/jpeg)' -//photos/frontend:lib -//photos/frontend:lib_impl -//photos/frontend:lib_dispatcher -//photos/frontend:icons -//photos/frontend/modules/gadgets:gadget_icon -//photos/thumbnailer:thumbnail_lib -//third_party/jpeg/img:renderer -``` - -<a name="What_depends_on_"></a> -### What depends on ... - -<a name="What_rules_under_bar_depend_o"></a> -#### What rules under bar depend on Y? - -```sh -bazel query 'bar/... intersect allpaths(bar/..., Y)' -``` - -Note: `X intersect allpaths(X, Y)` is the general idiom for the query "which X -depend on Y?" If expression X is non-trivial, it may be convenient to bind a -name to it using `let` to avoid duplication. - -<a name="What_are_the_intra_package_direct_rdeps"></a> -#### What targets directly depend on T, in T's package? - -```sh -bazel query 'let t = T in rdeps(siblings($t), $t, 1)' -``` - -<a name="How_do_I_break_a_dependency_"></a> -### How do I break a dependency ... - -<!-- TODO find a convincing value of X to plug in here --> -<a name="What_dependency_paths_do_I_have_"></a> -#### What dependency paths do I have to break to make `bar` no longer depend on X? - -To output the graph to a `png` file: - -```sh -bazel query 'allpaths(bar/...,X)' --output graph | dot -Tpng > /tmp/dep.png -``` - -<a name="Misc_"></a> -### Misc ... - -<a name="How_many_sequential_steps_are_th"></a> -#### How many sequential steps are there in the `ServletSmokeTests` build? - -Unfortunately, the query language can't currently give you the longest path -from x to y, but it can find the (or rather _a_) most distant node from the -starting point, or show you the _lengths_ of the longest path from x to every -y that it depends on. Use `maxrank`: - -```sh -% bazel query 'deps(//src/test/java/com/example/servlet:ServletSmokeTests)' --output maxrank | tail -1 -85 //third_party/zlib:zutil.c -``` - -The result indicates that there exist paths of length 85 that must occur in -order in this build. |