51

How can I increase the verbosity of the build process? Bazel seems to print compiler commands only if something goes wrong during the build.

I would like to see which compiler comands the cc_library rule fires, even if everything seems to be fine, to debug linking problems. I already tried various bazel command line parameters but nothing gives me the compiler commands :(

Jan
  • 1,359
  • 1
  • 13
  • 18
  • Possible duplicate of [How do I get the commands executed by Bazel](https://stackoverflow.com/questions/33983711/how-do-i-get-the-commands-executed-by-bazel) – 200_success Apr 13 '18 at 21:42
  • 3
    Which is 2 months younger than this question. So why do you mark my question as a duplicate? – Jan Apr 16 '18 at 05:47
  • Because the other question presents the challenge in more detail, and has an answer with a citation. – 200_success Apr 16 '18 at 06:31

4 Answers4

74

This is probably what you are looking for:

bazel build --subcommands //my:target

The --subcommands option causes Bazel's execution phase to print the full command line for each command prior to executing it.

John
  • 29,546
  • 11
  • 78
  • 79
  • 2
    You also have to change something in the code base to make it rebuild--even if that just means deleting a parenthesis somewhere to make the build break. – Gabriel Staples Feb 27 '20 at 23:58
  • Where is the official documentation for the `-s` option? Is there a long form of this short option which is more easy to Google? – Gabriel Staples Jun 24 '20 at 23:46
  • 2
    https://docs.bazel.build/versions/master/user-manual.html#flag--subcommands `bazel help build` prints: `--[no]subcommands [-s] (true, pretty_print or false; default: "false")` – hnakamur Nov 26 '20 at 14:03
  • +1 to hnakamur's comment. I think people should usually use the verbose version of flags in stackoverflow answers because they are more explanatory. `bazel build --subcommands //my:target` is clear and easy to search for compared to `-s`. – gonzojive Jul 27 '21 at 15:53
15

Useful information taken from Envoy's bazel readme (https://github.com/envoyproxy/envoy/blob/master/bazel/README.md)

When trying to understand what Bazel is doing, the -s and --explain options are useful. To have Bazel provide verbose output on which commands it is executing:

bazel build -s //source/...

To have Bazel emit to a text file the rationale for rebuilding a target:

bazel build --explain=file.txt //source/...

To get more verbose explanations:

bazel build --explain=file.txt --verbose_explanations //source/...
mancini0
  • 4,285
  • 1
  • 29
  • 31
2

You might also find the following useful in addition to the accepted answer of using --subcommands (-s):

bazel build --subcommands --verbose_failures //my:target

The --verbose_failures option causes Bazel's execution phase to print the full command line for commands that failed.

Although it would seem the --subcommands option supercedes it given it is documented to display prior to command execution, I have found cases (with bazel 5.2.0) where for a failing command, --subcommands alone shows only a portion of the command along with <remaining N arguments skipped>. Using both --subcommands and --verbose_failures displays the full command line in these cases.

uptime
  • 21
  • 3
1

Maybe you can generate the compile_commands.json file. I have created Shell scripts (under Linux) to automate that: https://github.com/vincent-picaud/Bazel_and_CompileCommands.

Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70