Googler | 018c569 | 2022-07-14 12:52:58 -0700 | [diff] [blame] | 1 | Project: /_project.yaml |
| 2 | Book: /_book.yaml |
| 3 | |
| 4 | # Review the dependency graph |
| 5 | |
Googler | 3b9ed6e | 2022-11-08 02:19:42 -0800 | [diff] [blame^] | 6 | {% include "_buttons.html" %} |
| 7 | |
Googler | 018c569 | 2022-07-14 12:52:58 -0700 | [diff] [blame] | 8 | A successful build has all of its dependencies explicitly stated in the `BUILD` |
| 9 | file. Bazel uses those statements to create the project's dependency graph, |
| 10 | which enables accurate incremental builds. |
| 11 | |
| 12 | To visualize the sample project's dependencies, you can generate a text |
| 13 | representation of the dependency graph by running this command at the |
| 14 | workspace root: |
| 15 | |
| 16 | ``` |
| 17 | bazel query --notool_deps --noimplicit_deps "deps(//main:hello-world)" \ |
| 18 | --output graph |
| 19 | ``` |
| 20 | |
| 21 | The above command tells Bazel to look for all dependencies for the target |
| 22 | `//main:hello-world` (excluding host and implicit dependencies) and format the |
| 23 | output as a graph. |
| 24 | |
| 25 | Then, paste the text into [GraphViz](http://www.webgraphviz.com/). |
| 26 | |
| 27 | On Ubuntu, you can view the graph locally by installing GraphViz and the xdot |
| 28 | Dot Viewer: |
| 29 | |
| 30 | ``` |
| 31 | sudo apt update && sudo apt install graphviz xdot |
| 32 | ``` |
| 33 | |
| 34 | Then you can generate and view the graph by piping the text output above |
| 35 | straight to xdot: |
| 36 | |
| 37 | ``` |
| 38 | xdot <(bazel query --notool_deps --noimplicit_deps "deps(//main:hello-world)" \ |
| 39 | --output graph) |
| 40 | ``` |
| 41 | |
| 42 | As you can see, the first stage of the sample project has a single target |
| 43 | that builds a single source file with no additional dependencies: |
| 44 | |
| 45 |  |
| 46 | |
| 47 | **Figure 1.** Dependency graph for `hello-world` displays a single target with a single |
| 48 | source file. |
| 49 | |
| 50 | After you set up your workspace, build your project, and examine its |
| 51 | dependencies, then you can add some complexity. |