Issue

I found the issue when I was following the tutorial on a new Macbook Pro: Introduction to Bazel: Building a C++ Project.

1
2
3
4
5
6
7
8
9
10
11
12
➜  stage1 git:(master) ✗ bazel build //main:hello-world
INFO: Analysed target //main:hello-world (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /Users/{...}/google/bazel_example/cpp-tutorial/stage1/main/BUILD:1:1: C++ compilation of rule '//main:hello-world' failed (Exit 1) cc_wrapper.sh failed: error executing command external/local_config_cc/cc_wrapper.sh -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics ... (remaining 28 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
clang: error: unknown argument: '-fno-canonical-system-headers'
Target //main:hello-world failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.287s, Critical Path: 0.11s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

The version information of gcc and bazel that I’m using is

1
2
3
4
5
6
7
8
9
➜  stage1 git:(master) ✗  gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.11.45.2)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

➜ stage1 git:(master) ✗ bazel info release
release 0.23.1

Things I Tried

Update bazel

Follow their official guide, Installing Bazel on macOS. it doesn’t work.

Install/Update XCode

Install or update via App Store, it doesn’t work.

Clean the Cache

1
2
bazel clean             # it doesn't work
bazel clean --expunge # it works!!!

It works once cleaned with --expunge!

Why

The issue was actually caused by outdated gcc version, it should be fixed by updating XCode. However, it requires bazel clean to remove the working tree. The bazel clean only cleans the repository, while bazel clean --expunge completely removes the entire working tree created by a Bazel instance.