Skip to content

Commit 16eda26

Browse files
authored
Extend progressive cache to bazel-managed Go and pip (#47007)
### What does this PR do? Wire up `bazel`-managed Go and `pip` caches to `XDG_CACHE_HOME` in the `tools/bazel*` scripts, and add the corresponding paths to the progressive GitLab runner cache keyed on `.go-version` and `.python-version`. ### Motivation Extend #43274's XDG-as-single-cache-root design to Go and `pip`, whose XDG support has been steadlessly growing from "Partial" to "Supported": https://wiki.archlinux.org/title/XDG_Base_Directory. By landing in `$XDG_CACHE_HOME`, they inherit the progressive-cache policy from #46151: only `main` pushes to them, keeping growth bounded. Keying on language version files further contains growth by resetting the cache at version upgrade boundaries rather than accumulating superseded artifacts. ### Additional Notes No worry: the new cache paths are already excluded from omnibus source trees via `**/.cache/**/*` source filters. Coming soon: we might want to leverage upcoming `--strict_repo_env` (with `bazel` 8.6.0, #47011), for which we'll anyway have to list propagated environment variables. Near future: as the omnibus-bazel transition progresses, other caches (`cache_omnibus_ruby_deps`, `go_deps`, `go_tools_deps`, `go_tools_deps_arm64`, etc.) are expected to shrink until no longer applicable. Co-authored-by: regis.desgroppes <regis.desgroppes@datadoghq.com>
1 parent f3601e7 commit 16eda26

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

.gitlab/build/bazel/defs.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
policy: pull$BAZEL_CACHE_POLICY_SUFFIX
1717
when: on_success
1818
- &bazel_cache
19-
key: bazel-$CI_RUNNER_DESCRIPTION
19+
key:
20+
prefix: bazel-$CI_RUNNER_DESCRIPTION
21+
files: [ .go-version, .python-version ]
2022
paths:
2123
- .cache/bazel/*/cache
24+
- .cache/go-build
25+
- .cache/go/mod
2226
- .cache/pip
2327
policy: pull$BAZEL_CACHE_POLICY_SUFFIX
2428
when: on_success

tools/bazel

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@ if [ ! -d "${XDG_CACHE_HOME:-}" ]; then
1818
# shellcheck disable=SC2016
1919
>&2 echo ' docker run --env=XDG_CACHE_HOME=/cache --volume="$HOME/.cache:/cache" ...'
2020
fi
21-
elif [ -n "${CI:-}" ]; then
22-
exec "$BAZEL_REAL" ${1:+"$1" --config=ci} "${@:2}"
21+
fi
22+
23+
# Ensure `bazel` & managed toolchains honor `XDG_CACHE_HOME` as per https://wiki.archlinux.org/title/XDG_Base_Directory
24+
if [ -n "${XDG_CACHE_HOME:-}" ]; then
25+
if [[ "$XDG_CACHE_HOME" != /* ]]; then
26+
>&2 echo "🔴 XDG_CACHE_HOME ($XDG_CACHE_HOME) must denote an absolute path!"
27+
exit 2
28+
fi
29+
unset GOCACHE # https://pkg.go.dev/os#UserCacheDir
30+
export GOMODCACHE="$XDG_CACHE_HOME"/go/mod # https://wiki.archlinux.org/title/XDG_Base_Directory#Partial
31+
unset PIP_CACHE_DIR # https://pip.pypa.io/en/stable/topics/caching/#default-paths
32+
33+
[ -n "${CI:-}" ] && exec "$BAZEL_REAL" ${1:+"$1" --config=ci} "${@:2}"
2334
fi
2435

2536
exec "$BAZEL_REAL" "$@"

tools/bazel.bat

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,19 @@ if not exist "%XDG_CACHE_HOME%" (
2121
)
2222
)
2323

24-
:: Make `bazel` honor $XDG_CACHE_HOME if set as it does on POSIX OSes: https://github.com/bazelbuild/bazel/issues/27808
24+
:: Ensure `bazel` & managed toolchains honor `XDG_CACHE_HOME` if set: https://github.com/bazelbuild/bazel/issues/27808
2525
if defined XDG_CACHE_HOME (
26+
set "XDG_CACHE_HOME=!XDG_CACHE_HOME:/=\!"
27+
if "!XDG_CACHE_HOME:~1,2!" neq ":\" if "!XDG_CACHE_HOME:~0,2!" neq "\\" (
28+
>&2 echo 🔴 XDG_CACHE_HOME ^(!XDG_CACHE_HOME!^) must denote an absolute path!
29+
exit /b 2
30+
)
31+
:: https://pkg.go.dev/os#UserCacheDir
32+
set "GOCACHE=%XDG_CACHE_HOME%\go-build"
33+
:: https://wiki.archlinux.org/title/XDG_Base_Directory#Partial
34+
set "GOMODCACHE=%XDG_CACHE_HOME%\go\mod"
35+
:: https://pip.pypa.io/en/stable/topics/caching/#default-paths
36+
set "PIP_CACHE_DIR=%XDG_CACHE_HOME%\pip"
2637
set "bazel_home=%XDG_CACHE_HOME%\bazel"
2738
set bazel_home_startup_option="--output_user_root=!bazel_home!"
2839
) else (

0 commit comments

Comments
 (0)