bazel

How to do verify that some code doesn't compile with Bazel?


I have several pieces of C++ code (templates, macros, etc) which are intended to not compile when used certain ways. Writing a piece of code by hand which does the things that shouldn't compile and verifying it doesn't compile is easy enough, but that's not automated. It seems like Bazel should be able to compile a piece of code and automatically verify it doesn't compile as part of bazel test, and this seems like a fairly common situation with C++ code. Is there a builtin way to do that?

I'm trying to avoid rolling my own Skylark rule that generates a shell script which calls the C++ compiler because handling all the arguments Bazel passes to the C++ toolchain is really hard. Currently, bazelbuild/bazel#146 and some other related issues mean all the information isn't even available.


Solution

  • Write a test that creates a scratch workspace and BUILD and C++ files in it, then calls Bazel inside the test, and verifies its exit code and stderr output.

    We do similar things all the time with Bazel's own integration tests. See a complete example in commit e78ad83: the test data-depends on Bazel itself, sets up a mock workspace and BUILD file and runs Bazel as a subprocess, then asserts its stdout (in this case: the result of bazel info server_pid). (NB: some of these source files were updated since then, so look at the latest versions.)