11

I am currently trying to measure the time bazel build //api/... takes to build our "api" project with different --spawn_strategys. I am having a hard time doing so because Bazel doesn't rebuild anything as long as I don't touch the source files.

I was able to force rebuilds by editing all files inside our "api" project, but doing this repeatedly is cumbersome.

What is the best way to force Bazel to rebuild so that I can measure build times for our repository?

Preferably, I would like to use something like bazel build //api/... --some_option_which_forces_rebuilding or something similar.

Vogelsgesang
  • 684
  • 1
  • 4
  • 15

2 Answers2

18

A bit dirty, but you could use --action_env to change the build environment and invalidate all the actions. From the docs:

Environment variables are considered an essential part of an action. In other words, an action is expected to produce a different output, if the environment it is invoked in differs; in particular, a previously cached value cannot be taken if the effective environment changes.

also (from this page):

[...] The value of those environment variable can be enforced from the command line with the --action_env flag (but this flag will invalidate every action of the build).

Just setting a random variable should be enough:

> bazel build --action_env="avariable=1" :mytarget
> bazel build --action_env="avariable=2" :mytarget
> ...
dms
  • 1,260
  • 7
  • 12
  • Thanks! This is already much better than my current approach. However, I would like to keep the cache for everything else, except for `//api/...`. (Many other dependencies in the dependency tree for which I would like to keep the cache) Is there a way to let `--action_env` only influence parts of my dependency tree? – Vogelsgesang Nov 29 '19 at 15:04
  • You could try using `config_setting` in `//api` with different values of the env variable for the different settings, see https://docs.bazel.build/versions/master/configurable-attributes.html – dms Dec 02 '19 at 01:21
  • 1
    I've decided to use the following flavor to generally get a rebuild ;) `--action_env=SHIT=$(date -Ins)` – Eric Cousineau Feb 24 '21 at 22:05
2

If you delete all the stuff in the output directory for the api package and its subpackages (should be under the bazel-bin symlink), bazel will rerun all the actions that produced those things, and not run any actions that produced things in other packages. This should also avoid rerunning the analysis phase, which changing config_settings or --action_env will do.

ahumesky
  • 4,203
  • 8
  • 12