Skip to content

Commit 518609c

Browse files
authored
Disable repo_contents_cache when in-workspace for Bazel 9 (#48186)
### What does this PR do? Extends the CI argument injection in `tools/bazel*` beyond just `--config=ci` to `--repo_contents_cache=` (disabled, matching Bazel 8.4+ default) when using an in-workspace cache path (i.e. on Linux ephemeral runners). ### Motivation When `XDG_CACHE_HOME` is not explicitly set to a persistent directory (i.e. on Linux ephemeral CI runners), the cache lands under the repo-scoped `.cache` directory (both GitLab and GitHub Actions want in-workspace cache paths), leading to: ``` ERROR: The repo contents cache [/path/to/repos/v1/contents] is inside the main repo [/path/to/datadog-agent]. This can cause spurious failures. Disable the repo contents cache with `--repo_contents_cache=`, or specify `--repo_contents_cache=<path outside the main repo>`. ``` `--repo_contents_cache` stores a workspace-independent copy of fetched external repository trees under `{--repository_cache}/contents` and defaults to `"null"` (enabled) as of Bazel 9.0. It had been disabled by default (bazelbuild/bazel#26802) in Bazel 8.4+ precisely because of the issues described in bazelbuild/bazel#26384. The present change therefore keeps it disabled in this very case **until Bazel figures out a way to honor the `.cache` exclusion in `.bazelignore`**. Co-authored-by: regis.desgroppes <regis.desgroppes@datadoghq.com>
1 parent b052e08 commit 518609c

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

tools/bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ if [ -n "${XDG_CACHE_HOME:-}" ]; then
3232
fi
3333

3434
# "--startup cmd ..." -> "--startup cmd --config=ci ..."
35-
if [[ $# -gt 0 && -n "${CI:-}" && -z "${GITHUB_ACTIONS:-}" ]]; then
35+
if [[ $# -gt 0 && -n "${CI:-}" ]]; then
3636
args=()
3737
cmd=
3838
for arg in "$@"; do
3939
args+=("$arg")
4040
if [[ -z "$cmd" && "$arg" != -* ]]; then
4141
cmd=$arg
42-
args+=(--config=ci)
42+
[ -z "${GITHUB_ACTIONS:-}" ] && args+=(--config=ci)
43+
# https://github.com/bazelbuild/bazel/issues/26384
44+
[ "${XDG_CACHE_HOME:-}" = "$(dirname "$(dirname "$0")")/.cache" ] && args+=(--repo_contents_cache=)
4345
fi
4446
done
4547
set -- "${args[@]}"

tools/bazel.bat

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if defined XDG_CACHE_HOME (
3838
set "bazel_home=%XDG_CACHE_HOME%\bazel"
3939
set bazel_home_startup_option="--output_user_root=!bazel_home!"
4040
) else (
41-
set "XDG_CACHE_HOME=%~dp0..\.cache"
41+
for %%i in ("%~dp0..\.cache") do set "XDG_CACHE_HOME=%%~fi"
4242
)
4343

4444
:: Check legacy max path length of 260 characters got lifted, or fail with instructions
@@ -55,7 +55,15 @@ if not exist "!more_than_260_chars!" (
5555
)
5656

5757
set "args=%*"
58-
if defined args if defined CI if not defined GITHUB_ACTIONS call :inject_ci_args
58+
if defined args if defined CI (
59+
set "ci_args="
60+
if not defined GITHUB_ACTIONS set "ci_args=--config=ci"
61+
:: https://github.com/bazelbuild/bazel/issues/26384
62+
for %%i in ("%~dp0..\.cache") do if "!XDG_CACHE_HOME!" == "%%~fi" (
63+
if defined ci_args (set "ci_args=!ci_args! --repo_contents_cache=") else set "ci_args=--repo_contents_cache="
64+
)
65+
if defined ci_args call :inject_ci_args
66+
)
5967
"%BAZEL_REAL%" !bazel_home_startup_option! !args!
6068
exit /b !errorlevel!
6169

@@ -73,7 +81,7 @@ for /f "tokens=1* delims= " %%i in ("!next_args!") do (
7381
) else (
7482
if defined startup_args set "startup_args=!startup_args:~1! "
7583
set "cmd=%%i"
76-
set "args=!startup_args!!cmd! --config=ci %%j"
84+
set "args=!startup_args!!cmd! !ci_args! %%j"
7785
)
7886
)
7987
if not defined cmd if defined next_args goto :parse_next_arg

0 commit comments

Comments
 (0)