Home > database >  Best practices for efficient Bazel builds when using Git worktrees
Best practices for efficient Bazel builds when using Git worktrees

Time:07-20

I used the "worktrees" feature of git to manage branches of my repo. This means that every active branch sits in its own directory, and switching branches is accomplished by switching directories.

Unfortunately, this seems to interact badly with bazel. Even with a shared disk cache, I find that bazel re-downloads remote repositories from my WORKSPACE file for each worktree directory (possibly because of remote actions). Some of these are quite large and slow to fetch. (I could install a squid proxy, but I feel like bazel's cache should be sufficient).

I've already found this discussion, but no solutions.

I'm sure other users are using bazel and git worktrees solution. What advice can other users offer? What bazel configurations have other users found to ameliorate this situation?

CodePudding user response:

For different branches, sharing an output base will be fine. Set --output_base to some shared folder, like bazel --output_base=my_output_base build //.... You can make it automatic by adding a line like startup --output_base=my_output_base to an applicable bazelrc.

Do note that this will prevent concurrent builds in different folders. They will queue up and run one at a time instead.

CodePudding user response:

Make use of a remote build cache

You can spin up your own local instance_ via:

sudo docker run -u 1000:1000 -v ~/bazel_remote_cache:/data \                       
        -p 9090:8080 -p 9092:9092 buchgr/bazel-remote-cache

You can set up a home RC file (.bazelrc) to use the remote build cache:

cd ~
echo "build --remote_cache=http://localhost:9090" >> .bazelrc

If you do a build now the remote cache is used.

When switching branches a lot of stuff should be already cached. The remote cache works better here than the local cache from my experience.

  • Related