Sharing --repository_cache with multiple users results in cache misses and overrides of entries
Description of the problem / feature request:
I would like to set a system-wide local path for the repository cache (--repository_cache) so multiple users on the same system can avoid having to re-download the same external dependency. This implies setting the repository cache directory to be group readable and writable, in addition to setting the SGID bit, so new files are owned by the group that is common to all users. This allows everyone to still be able to read from the cache despite which particular user generated the hash entries when running bazel. World readable/writable could also be used.
However, when an entry of interest at repository_cache_dir/content_addressable/sha256/{hash}/file is owned by a different user, bazel treats it as a cache miss, and causes a re-download of the external dependency and overwrites the existing entry with the new file but owned by the user that ran bazel.
Feature requests: what underlying problem are you trying to solve with this feature?
Trying to avoid each user on a particular system to have to re-download an external dependency when there's already an entry for it in the repository cache directory specified by the --repository_cache flag, while also avoiding copying the dependency to each user's bazel output base directory by using the --experimental_repository_cache_hardlinks flag in order to save disk space.
Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
.bazelrc
build --experimental_repository_cache_hardlinks
build --repository_cache=/path/to/bazel-rep-cache
build --disk_cache=/path/to/bazel-disk-cache
WORKSPACE
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
http_file(
name = "bazel_source_code",
urls=["https://github.com/bazelbuild/bazel/archive/refs/tags/4.1.0.tar.gz"],
sha256="27b887da66789638d904cb3cd3526418145e1f2f1e2891209f12d60f566850ff",
)
BUILD
genrule(
name="bazel_source_file_paths",
srcs=[
"@bazel_source_code//file",
],
outs=["bazel_source_file_paths.txt"],
cmd="tar -tf $(SRCS) > $@",
)
bazel build :bazel_source_file_pathsstat $(bazel info output_base)/external/bazel_source_code/file/downloaded# note that it's a regular filebazel clean --expunge# needed to cause a hardlink generation on subsequent bazel invocations.bazel build :bazel_source_file_paths# will result in a cache hit this time and create a hardlinkstat $(bazel info output_base)/external/bazel_source_code/file/downloaded# note that it's a hardlink filesudo chown -R root:root /path/to/bazel-rep-cache# simulate entries generated by another usersudo chmod -R o+r /path/to/bazel-rep-cache# allow all to read. One could use a common group instead but simplifying for this examplesudo chmod o+w /path/to/bazel-rep-cache /content_addressable/sha256/27b887da66789638d904cb3cd3526418145e1f2f1e2891209f12d60f566850ff# allow all to writebazel clean --expungebazel build :bazel_source_file_paths# ideally should result in a cache hit again since entry still exists, but owned by a different user (rootin this case now), but it doesn't. Causes re-download and an overwrite of cache entry. Noticed as well that it creates atmp-...file inside the/path/to/bazel-rep-cache/content_addressable/sha256/{hash}directory first before overwriting original file.
What operating system are you running Bazel on?
RedHat 7.9
What's the output of bazel info release?
release 3.6.0
Have you found anything relevant by searching the web?
https://docs.bazel.build/versions/main/guide.html#repository-cache https://bazel.build/designs/2016/09/30/repository-cache.html https://groups.google.com/g/bazel-discuss/c/mzEXZMCa2Ic https://github.com/bazelbuild/bazel/issues/8496
Same issue with Bazel 4.1.0
@meteorcloudy @Wyverald Could you please triage this?
Note @oquenchil @meteorcloudy this is not a feature request, but a potential bug. Everything I've outlined should work.
Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 14 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler". Please reach out to the triage team (@bazelbuild/triage) if you think this issue is still relevant or you are interested in getting the issue resolved.
I think this issue is still relevant @bazelbuild/triage and still interested in getting this resolved. Having the cache shared across multiple users on the same server will help to reduce network transfer and avoid having multiple users having copies of the same artifacts on the same server.
Agreed, I am just trying to do the same thing and hit the problem of some directories not being writable by the group, even with setting a facl rule.
That being said, one could argue that setting up a remote cache would allow you to solve some of the issue? At least for the disk / build cache. That might be an explanation why this issue is not getting much attention? :/
I agree that remote cache is probably why this issue isn't getting much attention, but using it only helps to reduce the build execution time, not disk space. The required artifact in my case is a large file where build time is negligible. Using a remote cache still requires each user to download a copy of the same file to their local environment, and in a shared server environment with multiple users, one ends up with as many copies of the artifact on the server's local storage as there are users. It should be possible to have ONE file in the server's local storage and multiple users that are on the same server be able to use it without Bazel copying it into each user's Bazel workspace, ending up with many copies of the same file.
Agreed, I am just trying to do the same thing and hit the problem of some directories not being writable by the group, even with setting a facl rule.
That being said, one could argue that setting up a remote cache would allow you to solve some of the issue? At least for the disk / build cache. That might be an explanation why this issue is not getting much attention? :/
remote cache caches only action results (and its references), so bazel still needs to go through analysis phase which triggers fetch, which still reads distdir and repository_cache. Please correct me if I am wrong.
Hi all,
stumbled over this thread as we're investigating to have a shared repository_cache on a build node in our jenkins CI. If I understand this thread correctly - as well as several articles on the web - sharing the repository cache is supported, right?
https://sluongng.hashnode.dev/bazel-caching-explained-pt-3-repository-cache https://narang99.github.io/2025-03-22-monorepo-bazel-jenkins/#repository-cache
I am asking, because when executing multiple bazel builds locally with a shared repository cache (which is already the default), I get some very weird (or for me not understandable) issues, like e.g.:
ERROR: no such package '@@rules_rust++i2+rules_rust_ctve__percent-encoding-2.3.1//': BUILD file not found in directory '' of external repository @@rules_rust++i2+rules_rust_ctve__percent-encoding-2.3.1. Add a BUILD file to a directory to mark it as a package.
At the moment I dont have a minimal repo - I am more interested to get a confirmation if this is really supported in general
@TimotheusBachinger Does this reproduce with --repo_contents_cache=? Which bazel version are you on?
you mean setting both?
bazel build --repository_cache=/tmp/new_repo_cache --repo_contents_cache=/tmp/new_repo_content_cache
I can reproduce it with aboves command, yes. We're on 8.3.0:
cat .bazelversion
8.3.0
The repo contents cache in that version of Bazel is affected by a bug that manifests with failures like this. You can disable it by updating to the latest 8.x release or passing in an empty value for the flag.
@fmeum cool! Thanks a lot for that hint! Will try your suggestions!