Sandboxing & Undeclared Inputs
Bazel's promise of correct incremental builds and shareable caches rests on one assumption: an action reads only its declared inputs. If a compile secretly reads a header nobody declared, Bazel won't rerun it when that header changes, and a cached result from another machine may have been built with a different version of it. Sandboxing turns that assumption into a check: each action runs in a directory that contains only its declared inputs.
Spawn strategies
How an action (a spawn) runs is chosen by --spawn_strategy (a comma-separated preference list) or per mnemonic with --strategy=Mnemonic=…:
| Strategy | What it does |
|---|---|
linux-sandbox |
Linux namespaces: a private mount namespace with only declared inputs, optional network isolation (--sandbox_default_allow_network=false), tmpfs; the default on Linux when namespaces are available |
processwrapper-sandbox |
a symlink forest of the declared inputs in a fresh directory; works everywhere, isolates files but not the network or the rest of the file system |
darwin-sandbox |
macOS sandbox-exec profiles |
worker |
a persistent worker process (javac, some compilers), sandboxed with --worker_sandboxing |
local (alias standalone) |
run directly in the execution root, no isolation |
remote |
REAPI remote execution |
The course container doesn't allow the namespaces linux-sandbox needs, so Bazel falls back to processwrapper-sandbox, which is what the N processwrapper-sandbox counts in every summary line mean. It's enough to catch undeclared files.
An undeclared input, caught
The genrule below reads banner.txt without declaring it:
mkdir ws && cd ws
echo 'module(name = "sandbox_demo")' > MODULE.bazel
mkdir site
echo "Spring sale" > site/banner.txt
cat > site/BUILD.bazel <<'EOF'
genrule(
name = "page",
outs = ["page.html"],
cmd = "text=$(cat site/banner.txt) && echo \"<h1>$text</h1>\" > $@", # reads an undeclared file
)
EOF
echo "== sandboxed (the default)"
bazel build //site:page 2>&1 | grep -E '^cat:|Build did NOT|completed'
echo "== --spawn_strategy=local"
bazel build --spawn_strategy=local //site:page 2>&1 | grep -E '^cat:|Build did NOT|completed|processes'
cat bazel-bin/site/page.html
echo "== the banner changes, the output doesn't"
echo "Summer sale" > site/banner.txt
bazel build --spawn_strategy=local //site:page 2>&1 | grep -E 'process'
cat bazel-bin/site/page.html== sandboxed (the default)
cat: site/banner.txt: No such file or directory
ERROR: Build did NOT complete successfully
== --spawn_strategy=local
INFO: 2 processes: 1 internal, 1 local.
INFO: Build completed successfully, 2 total actions
<h1>Spring sale</h1>
== the banner changes, the output doesn't
INFO: 1 process: 1 internal.
<h1>Spring sale</h1>
This is the whole argument for sandboxing in one session:
- In the sandbox the file simply isn't there, so the build fails loudly, at the action that's wrong.
- Without the sandbox (
--spawn_strategy=local) the command finds the file in the execution root and "works". - Then the banner changes and Bazel doesn't rebuild:
banner.txtisn't an input, so nothing in the action key changed. The output is stale, and on a shared cache that stale page would be served to everyone.
The fix is to declare the input: srcs = ["banner.txt"] and cmd = "text=$(cat lt;) && echo \"<h1>$text</h1>\" > $@".
Debugging a sandboxed action
When an action fails only in the sandbox, add --sandbox_debug: Bazel keeps the sandbox directory of the failing action (under $(bazel info output_base)/sandbox/) and prints the command it used, so you can look at what was, and wasn't, there. You can also simply ask an action to list its world:
mkdir ws && cd ws
echo 'module(name = "sandbox_demo")' > MODULE.bazel
mkdir site
echo "Spring sale" > site/banner.txt
echo "draft" > site/notes.txt
printf 'genrule(name = "page", srcs = ["banner.txt"], outs = ["page.html"], cmd = "find . -type l | sort > $@")\n' > site/BUILD.bazel
bazel build //site:page 2>/dev/null
cat bazel-bin/site/page.html./external/bazel_tools/tools/genrule/genrule-setup.sh
./site/banner.txt
The action's working directory contains symlinks to exactly two files: the declared site/banner.txt and the genrule setup script from @bazel_tools. site/notes.txt, MODULE.bazel and everything else in the workspace are absent.
Escape hatches, and their cost
| Mechanism | Effect | When it's acceptable |
|---|---|---|
tags = ["no-sandbox"] |
this target's actions run locally, unsandboxed | a tool that can't run in a symlink forest; document why |
tags = ["requires-network"] |
network allowed in linux-sandbox |
integration tests against a local service |
tags = ["local"] / local = True (genrule) |
never sandboxed, never cached remotely | almost never |
--strategy=Javac=worker |
a strategy per mnemonic | performance tuning |
--sandbox_writable_path=/dir |
extra writable path | tools that insist on writing to a fixed location |
--experimental_reuse_sandbox_directories, --sandbox_base=/dev/shm |
faster sandbox setup | large builds where sandbox creation shows up in profiles |
Sandboxing costs time (creating thousands of symlinks per action), which is why some teams turn it off locally. Don't: the bugs it catches are the ones that poison caches. If it's slow, use --experimental_reuse_sandbox_directories and a tmpfs --sandbox_base, and let remote execution, which is always isolated, take the heavy actions.
Test yourself
A genrule reads a file it didn't declare. What happens in a sandboxed build?
- It works
- The file isn't in the sandbox, so the command fails
- Bazel adds the file to srcs
- Only a warning
With --spawn_strategy=local, the undeclared file changes. What does the next build do?
- Reruns the action
- Nothing: the file isn't part of the action key, so the stale output is kept
- Fails
- Cleans the cache
Why do the course sessions report processwrapper-sandbox rather than linux-sandbox?
- Bazel 9 removed linux-sandbox
- The container doesn't permit the Linux namespaces linux-sandbox needs, so Bazel falls back
- It is faster
- It was set in MODULE.bazel