The Action Environment
Files are not the only inputs of an action. Environment variables (PATH, LANG, CC, your own) change what tools do, so they must either be fixed or part of the cache key. Bazel's rule: an action sees only the environment variables it was explicitly given.
PATH and --incompatible_strict_action_env
PATH is the classic leak: a developer with /opt/homebrew/bin first on their PATH gets a different python3 or sed inside actions than CI does. With --incompatible_strict_action_env, actions get a fixed PATH=/bin:/usr/bin:/usr/local/bin regardless of your shell. It is on by default in Bazel 9 (bazel help build shows default: "true"); older Bazel versions needed it in .bazelrc.
mkdir ws && cd ws
echo 'module(name = "env_demo")' > MODULE.bazel
mkdir env
cat > env/BUILD.bazel <<'EOF'
genrule(
name = "env",
outs = ["env.txt"],
cmd = "echo PATH=$PATH SHOP_REGION=${SHOP_REGION:-unset} > $@",
)
EOF
export PATH=/home/learner/bin:$PATH SHOP_REGION=us
echo "shell PATH=$PATH"
bazel help build 2>/dev/null | grep -A0 'incompatible_strict_action_env'
echo "== default (strict)"
bazel build //env 2>/dev/null && cat bazel-bin/env/env.txt
echo "== --noincompatible_strict_action_env"
bazel build --noincompatible_strict_action_env //env 2>/dev/null && cat bazel-bin/env/env.txt
echo "== --action_env=SHOP_REGION (inherit) and --action_env=SHOP_REGION=eu (fixed)"
bazel build --action_env=SHOP_REGION //env 2>/dev/null && cat bazel-bin/env/env.txt
bazel build --action_env=SHOP_REGION=eu //env 2>/dev/null && cat bazel-bin/env/env.txtshell PATH=/home/learner/bin:/usr/local/bin:/usr/bin:/bin
--[no]incompatible_strict_action_env (a boolean; default: "true")
== default (strict)
PATH=/bin:/usr/bin:/usr/local/bin SHOP_REGION=unset
== --noincompatible_strict_action_env
PATH=/opt/bazelisk/downloads/sha256/7f775b54bbec487efaf0f49ccda1d2ed02cd34f582edfdd6aeeec76e39ef585c/bin:/home/learner/bin:/usr/local/bin:/usr/bin:/bin SHOP_REGION=unset
== --action_env=SHOP_REGION (inherit) and --action_env=SHOP_REGION=eu (fixed)
PATH=/bin:/usr/bin:/usr/local/bin SHOP_REGION=us
PATH=/bin:/usr/bin:/usr/local/bin SHOP_REGION=eu
- In strict mode the action's
PATHis fixed andSHOP_REGIONisn't passed at all, whatever the shell says. - Without strict mode, the client's
PATHleaks in (here including the directory Bazelisk prepends for the Bazel binary) (and becomes part of every action key, so two developers with differentPATHs never share cache hits). --action_env=NAMEpasses the variable's current value from the client;--action_env=NAME=valuesets it. Either way it becomes part of the key of every action, so changing it rebuilds everything. Prefer fixed values in.bazelrc, and prefer passing data as files or rule attributes.
Note: Each change of
--action_envalso prints "Build option --action_env has changed, discarding analysis cache": the configuration changed, so Bazel re-analyzes. Flags that flip back and forth between commands (an IDE using different flags from your terminal) cost time for the same reason.
Variables for tests and repository rules
| Flag | Reaches |
|---|---|
--action_env=X |
build actions (and tests) |
--test_env=X / --test_env=X=v |
tests only, so build actions keep their cache keys |
--repo_env=X |
repository rules and module extensions (e.g. CC for cc_configure, proxy settings); doesn't touch action keys |
--host_action_env=X |
actions in the exec configuration |
Stamping: the controlled exception
Release binaries want a version, commit hash or build time embedded. Putting those in normal inputs would make every commit rebuild everything. Bazel's answer is stamping: a --workspace_status_command script (a path relative to the workspace root) prints key/value pairs, and only targets that opt in (with --stamp) see them.
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_go", version = "0.65.0")' > MODULE.bazel
cat >> MODULE.bazel <<'EOF'
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.27.3")
EOF
git init -q && git commit -q --allow-empty -m "init"
mkdir tools && cat > tools/status.sh <<'EOF'
#!/bin/bash
echo "STABLE_GIT_COMMIT $(git rev-parse --short HEAD)"
echo "STABLE_RELEASE 2.4.0"
EOF
chmod +x tools/status.sh
echo 'build --workspace_status_command=tools/status.sh' > .bazelrc
mkdir app
cat > app/BUILD.bazel <<'EOF'
load("@rules_go//go:def.bzl", "go_binary")
go_binary(
name = "app",
srcs = ["main.go"],
x_defs = {"main.release": "{STABLE_RELEASE}", "main.commit": "{STABLE_GIT_COMMIT}"},
)
EOF
printf 'package main\n\nimport "fmt"\n\nvar release, commit = "dev", "none"\n\nfunc main() { fmt.Println("release", release, "commit", commit) }\n' > app/main.go
bazel run //app 2>/dev/null
bazel run --stamp //app 2>/dev/null
grep STABLE bazel-out/stable-status.txtrelease dev commit none
release 2.4.0 commit 0b76b89
STABLE_GIT_COMMIT 0b76b89
STABLE_RELEASE 2.4.0
Without --stamp the placeholders stay out of the binary, so developer builds remain cacheable; with --stamp the values from stable-status.txt are linked in. Keys starting with STABLE_ cause a relink of stamped targets when they change; other keys go to volatile-status.txt (Bazel adds BUILD_TIMESTAMP there) and don't trigger rebuilds on their own. Stamp only release builds (build:release --stamp).
A hermeticity checklist
| Leak | Fix |
|---|---|
tools from the host (python3, sed, compilers) |
hermetic toolchains (rules_python's interpreter, a downloaded Go SDK, toolchains_llvm), strict action env |
| environment variables | --incompatible_strict_action_env (default), avoid --action_env |
| time and randomness in outputs | SOURCE_DATE_EPOCH-style fixed timestamps, sorted file lists, -frandom-seed (Bazel's C++ rules set it) |
| absolute paths in outputs | relative paths from the execution root; -fdebug-prefix-map |
| network access during actions | linux-sandbox with --sandbox_default_allow_network=false; fetch in repository rules instead |
| undeclared files | the sandbox (previous lesson) |
Build Fundamentals covers why hermeticity matters in general; Bazel is the tool that enforces most of it for you.
Test yourself
What PATH does an action see by default in Bazel 9?
- The user's shell PATH
- A fixed /bin:/usr/bin:/usr/local/bin, because --incompatible_strict_action_env defaults to true
- An empty PATH
- The PATH from MODULE.bazel
Why is --test_env better than --action_env for a variable only tests need?
- It is shorter
- It doesn't change the cache keys of build actions, so nothing gets rebuilt
- It is encrypted
- It works offline
When does a target see values like {STABLE_GIT_COMMIT}?
- Always
- Only when built with --stamp (and the rule supports stamping)
- Only in tests
- Only with remote execution