Hermetic Builds & Remote Caches
A build is hermetic when its outputs depend only on inputs the build system knows about: declared source files, declared dependencies at fixed versions, a declared toolchain, and declared flags. Nothing leaks in from the machine: not the user's environment variables, not whatever happens to be in /usr/include, not the network, not the clock.
Hermeticity is what makes caching and incremental builds safe. A cache key can only cover inputs the build system knows about; anything that leaks in from outside is an input missing from the key.
A leak: environment variables
GNU Make's built-in rule for %.o: %.c uses $(CFLAGS), and Make imports every environment variable as a Make variable. So the same Makefile builds different programs depending on the shell it runs in, and Make doesn't notice when that changes:
mkdir -p leak && cd leak
cat > main.c <<'SRC'
#include <stdio.h>
int main(void) {
#ifdef FAST_PATH
puts("fast path enabled");
#else
puts("normal path");
#endif
return 0;
}
SRC
printf 'app: main.o\n\t$(CC) main.o -o app\n' > Makefile
make -j1 && ./app
echo "--- with CFLAGS in the environment (nothing else changed)"
CFLAGS=-DFAST_PATH make -j1 && ./app
echo "--- clean build with the same environment"
rm -f main.o app && CFLAGS=-DFAST_PATH make -j1 && ./appcc -c -o main.o main.c
cc main.o -o app
normal path
--- with CFLAGS in the environment (nothing else changed)
make: 'app' is up to date.
normal path
--- clean build with the same environment
cc -DFAST_PATH -c -o main.o main.c
cc main.o -o app
fast path enabled
Two different results from the same sources and the same environment, depending on whether main.o happened to exist. Hand that to a shared cache and the wrong object may end up in a colleague's build. Hermetic build systems avoid this by not passing the environment through: Bazel runs actions with a fixed, minimal environment (PATH and the variables you explicitly allow), and CMake records flags in its cache at configure time instead of reading them on every build.
You can approximate it by hand with env -i, which starts a command with an empty environment:
export CFLAGS=-DFAST_PATH LANGUAGE=de MY_SECRET=hunter2
env | grep -cE '^(CFLAGS|LANGUAGE|MY_SECRET)='
env -i PATH=/usr/bin:/bin bash -c 'env | sort'3
PATH=/usr/bin:/bin
PWD=/home/learner
SHLVL=1
_=/usr/bin/env
Other common leaks
| Leak | Symptom | Hermetic fix |
|---|---|---|
system headers and libraries (/usr/include/zlib.h) |
builds differ between developer machines and CI | dependencies from a package manager at locked versions, or a sysroot |
the compiler on PATH |
"works with my GCC" | a declared toolchain (Bazel toolchains, a pinned Docker image, a Conan/vcpkg toolchain) |
network access during the build (curl in a script, FetchContent without a hash) |
builds break when a server is down, or change when a file is replaced | download in a separate, hashed fetch step; build offline |
the clock (__DATE__, timestamps in archives) |
every build is different, the cache never hits | SOURCE_DATE_EPOCH (Reproducible Builds) |
absolute paths (/home/ada/proj in debug info) |
caches don't share between checkouts | prefix maps, relative paths |
| undeclared generated files | works serially, fails with -j |
declare every output and input (Parallel Builds) |
The usual way to enforce hermeticity is a sandbox: run each action where only its declared inputs exist. Bazel does this with Linux namespaces (linux-sandbox); a missing input then becomes a "file not found" error on the first build instead of a subtle bug months later. Building inside a fixed container image (like the one that verified this course's sessions, with --network none) is a coarser version of the same idea.
Local and remote caches
| Local cache | Remote (shared) cache | |
|---|---|---|
| Where | a folder on your machine (~/.cache/ccache, ~/.cache/bazel) |
a server: bazel-remote, BuildBuddy, EngFlow, Gradle Develocity, an S3 bucket for sccache, Nx Cloud |
| Who benefits | you, after switching branches or cleaning | everyone: CI fills it, developers download instead of compiling |
| Risk | low | cache poisoning: one non-hermetic or malicious writer corrupts everyone's builds |
| Typical policy | read + write | CI writes, developers only read |
A remote cache multiplies the payoff of hermeticity and also the damage of a leak. The model shows how a header that is not part of the key lets one machine's output leak into another machine's build:
import hashlib
def h(*parts):
return hashlib.sha256("|".join(parts).encode()).hexdigest()[:10]
remote_cache = {}
def build(machine, system_zlib_h, declared_inputs):
# the real output depends on the system header, whether declared or not
output = h("object", "main.c", "-O2", system_zlib_h)
key = h("action", *declared_inputs)
if key in remote_cache:
got = remote_cache[key]
verdict = "correct" if got == output else "WRONG OUTPUT (built against another zlib)"
print(f"{machine:8} cache HIT key={key} -> {got} {verdict}")
else:
remote_cache[key] = output
print(f"{machine:8} cache MISS key={key} -> built {output}, uploaded")
print("--- key without the system header (non-hermetic)")
build("ci", "zlib 1.3.1", ["main.c", "-O2"])
build("laptop", "zlib 1.2.11", ["main.c", "-O2"])
remote_cache.clear()
print("--- key includes the header's content (hermetic)")
build("ci", "zlib 1.3.1", ["main.c", "-O2", "zlib 1.3.1"])
build("laptop", "zlib 1.2.11", ["main.c", "-O2", "zlib 1.2.11"])
build("laptop2", "zlib 1.3.1", ["main.c", "-O2", "zlib 1.3.1"])--- key without the system header (non-hermetic)
ci cache MISS key=469614d1a1 -> built ad0141d039, uploaded
laptop cache HIT key=469614d1a1 -> ad0141d039 WRONG OUTPUT (built against another zlib)
--- key includes the header's content (hermetic)
ci cache MISS key=f8f2e65596 -> built ad0141d039, uploaded
laptop cache MISS key=f1e4e304e7 -> built 7e7d7f8fac, uploaded
laptop2 cache HIT key=f8f2e65596 -> ad0141d039 correct
Test yourself
What makes a build hermetic?
- It runs inside Docker
- Its outputs depend only on inputs the build system declares and tracks
- It uses only one CPU
- It has no tests
In the CFLAGS session, why did the second make print 'normal path'?
- CFLAGS was ignored
main.oalready existed and Make doesn't track environment variables, so it wasn't rebuilt-DFAST_PATHis invalid- The program caches output
Why do many teams let only CI write to the remote cache?
- CI is faster
- To limit cache poisoning: outputs from non-hermetic or untrusted machines could be served to everyone
- Developers have no network
- Caches are read-only by design