Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bazel builds from scratch ignoring cache

Tags:

bazel

I observe that my Bazel build agent frequently builds the project from scratch (including compiling grpc, which keeps unchanged) instead of taking results from cache. Is there a way, like query or cquery (pardon my ignorance) to determine why is the cache considered invalid for particular target? Or any techniques to tackle cache invalidation problem?

like image 247
Piotr Jachowicz Avatar asked Nov 16 '25 18:11

Piotr Jachowicz


1 Answers

This is most likely due to the rebuild sensitivity to particular environment variables. Many build actions will read from environment variables and use them to change the outputs. Bazel keeps track of this and will rebuild seemingly unchanged remote targets when your env changes.

To demonstrate this;

  • Build grpc (2x ensure it is cached the second time)
  • Change the PATH environment variable (your IDE may do this without you knowing)
    • mkdir ~/bin && export PATH=$PATH:~/bin
  • Rebuild grpc (This should trigger a complete rebuild)

There are a couple helpful flags to combat this rebuild sensitivity, and I'd recommend adding them to your bazelrc.

  • incompatible_strict_action_env: Freezes your environment and doesn't source environment variables from your shell.
  • action_env modify environment variables as needed for you build.
# file //.bazelrc

# Don't source environment from shell
build --incompatible_strict_action_env

# Use action_env as needed for your project
build --action_env=CC=clang
like image 90
silvergasp Avatar answered Nov 18 '25 21:11

silvergasp