Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/actions/setup-bazel-ci/fetch.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0
# Copyright Tony Burns
# cspell:ignore endgroup

# The list includes optional local targets from several workspaces. Query
# the local graph first, then fetch every listed target that it declares.
Expand All @@ -26,9 +27,50 @@ download_pattern+='|connect timed out' # Connection could not be established
download_pattern+='|Read timed out' # Transfer stalled while reading.
download_pattern+='|Unknown host' # DNS lookup failed.

workspace_diagnostic() {
local bazel_command=${1:-} module=missing repo=missing git_checkout cause
if [[ -f MODULE.bazel ]]; then
module=present
fi
if [[ -e REPO.bazel ]]; then
repo=present
fi
if [[ -z $bazel_command || $module == missing ]]; then
cause="no MODULE.bazel in $workspace: the checkout is empty, stale or in the wrong directory"
else
cause="Bazel $bazel_command could not find a workspace from $workspace (MODULE.bazel exists)"
fi
{
printf '%s\n' '::group::Bazel workspace diagnostic' "workspace: $workspace"
printf 'pwd: '
pwd || true
printf '%s\n' "MODULE.bazel: $module" "REPO.bazel: $repo"
printf 'Bazel command: %s\n' "${bazel_command:-none (workspace check failed before Bazel ran)}"
printf '%s\n' 'ls -la:'
# shellcheck disable=SC2012
ls -la -- "$workspace" | head -40 || true
if git_checkout=$(git rev-parse --is-inside-work-tree 2>/dev/null) && [[ $git_checkout == true ]]; then
printf '%s\n' 'git rev-parse HEAD:'
git rev-parse HEAD || true
printf '%s\n' 'git status --short:'
git status --short | head -20 || true
fi
printf '%s\n' '::endgroup::'

cause=${cause//%/%25}
cause=${cause//$'\r'/%0D}
cause=${cause//$'\n'/%0A}
printf '::error::%s\n' "$cause"
} >&2
}

retry() {
local delay=${RETRY_DELAY:-30} now url downloads error
until "$@" 2>&1 | tee "$out"; do
if grep -qF 'must be invoked from within a workspace' "$out"; then
workspace_diagnostic "$2"
return 1
fi
if ! downloads=$(grep -E "$download_pattern" "$out"); then
# Repository rule failures put the cause after a Starlark traceback.
# Keep it with the first error, including Bazel's timestamped form.
Expand Down Expand Up @@ -68,6 +110,10 @@ for workspace in "${workspaces[@]}"; do
visited+="$workspace"$'\n'
(
cd "$workspace"
if [[ ! -f MODULE.bazel ]]; then
workspace_diagnostic
exit 1
fi
if [[ $TARGETS == @default ]]; then
retry bazelisk mod dump_repo_mapping ''
mapping=$(cat "$out")
Expand Down
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ SHELL_SCRIPTS = [
"repo/tests/codeowners_errors_test.sh",
"repo/tests/mktemp_test.sh",
"repo/tests/cpu_quota_test.sh",
"repo/tests/fetch_workspace_test.sh",
"repo/tests/mutation_nightly_test.sh",
"repo/tests/retry_budget_test.sh",
"testing/private/expect_failure.sh.tpl",
Expand Down
15 changes: 15 additions & 0 deletions repo/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ sh_test(
env = {"SCRIPT_RLOCATIONPATH": "$(rlocationpath //:.github/actions/setup-bazel-ci/fetch.sh)"},
)

sh_test(
name = "fetch_workspace_test",
srcs = ["fetch_workspace_test.sh"],
data = [
"//:.github/actions/setup-bazel-ci/fetch.sh",
"//:.github/actions/setup-bazel-ci/fetch-targets.txt",
],
env = {"SCRIPT_RLOCATIONPATH": "$(rlocationpath //:.github/actions/setup-bazel-ci/fetch.sh)"},
# Tech debt (AGENTS.md): validate the Bash subprocess on windows-2025 and remove the exclusion.
target_compatible_with = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

# Git supplies the complete source list, including the nested Bazel modules.
sh_test(
name = "mktemp_test",
Expand Down
1 change: 1 addition & 0 deletions repo/tests/cache_fetch_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ if command -v cygpath >/dev/null; then
scratch=$(cygpath -u "$scratch")
fi
mkdir -p "$scratch/bin" "$scratch/root/go" "$scratch/root/typescript"
touch "$scratch/root/MODULE.bazel" "$scratch/root/go/MODULE.bazel" "$scratch/root/typescript/MODULE.bazel"
cat >"$scratch/bin/bazelisk" <<'MOCK'
#!/usr/bin/env bash
set -euo pipefail
Expand Down
62 changes: 62 additions & 0 deletions repo/tests/fetch_workspace_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0
# Copyright Tony Burns
# cspell:ignore endgroup

set -euo pipefail
script=${TEST_SRCDIR:?}/${SCRIPT_RLOCATIONPATH:?}
scratch=${TEST_TMPDIR:?}/fetch-workspace
mkdir -p "$scratch/bin" "$scratch/workspace"
cat >"$scratch/bin/bazelisk" <<'MOCK'
#!/usr/bin/env bash
set -euo pipefail
if [[ $1 == "$FAILING_COMMAND" ]]; then
printf '%s\n' "$BAZEL_ERROR" >&2
exit 7
fi
if [[ $1 == mod ]]; then
echo '{}'
fi
MOCK
chmod +x "$scratch/bin/bazelisk"
export PATH="$scratch/bin:$PATH" TARGETS=@default BUDGET=0 WORKSPACES=workspace
export FAILING_COMMAND=mod BAZEL_ERROR='ERROR: must be invoked from within a workspace'
cd "$scratch"

check_failure() {
local status=0
bash "$script" >stdout 2>stderr || status=$?
if [[ $status != 1 ]]; then
printf 'Expected exit status 1, got %s\n' "$status" >&2
cat stdout stderr >&2
exit 1
fi
}

check_failure
[[ ! -s stdout ]]
grep -qxF '::group::Bazel workspace diagnostic' stderr
grep -qxF "workspace: $PWD/workspace" stderr
grep -qxF "pwd: $PWD/workspace" stderr
grep -qxF 'MODULE.bazel: missing' stderr
grep -qxF 'REPO.bazel: missing' stderr
grep -qxF 'Bazel command: none (workspace check failed before Bazel ran)' stderr
grep -qxF '::endgroup::' stderr
annotation=$(grep '^::error::' stderr)
[[ $annotation == "::error::no MODULE.bazel in $PWD/workspace: the checkout is empty, stale or in the wrong directory" ]]

touch workspace/MODULE.bazel workspace/REPO.bazel
for command in mod query fetch; do
FAILING_COMMAND=$command check_failure
grep -qxF 'MODULE.bazel: present' stderr
grep -qxF 'REPO.bazel: present' stderr
grep -q ' REPO\.bazel$' stderr
grep -qxF "Bazel command: $command" stderr
annotation=$(grep '^::error::' stderr)
[[ $annotation == "::error::Bazel $command could not find a workspace from $PWD/workspace (MODULE.bazel exists)" ]]
done

BAZEL_ERROR='ERROR: some other failure' check_failure
[[ ! -s stderr ]]
output=$(cat stdout)
[[ $output == $'ERROR: some other failure\n::error::ERROR: some other failure' ]]
Loading