From 484f47cc3ad99345ac21db09d06edfb78b759818 Mon Sep 17 00:00:00 2001 From: "tbhb.dev Agent" Date: Mon, 23 Nov 2026 04:26:57 -0500 Subject: [PATCH] ci: name the cause when fetch.sh finds no workspace Generated-by: LLM harness=codex;model=openai:gpt-6.2-sol Generated-by: LLM effort=high;harness=codex:0.171.0;model=openai:gpt-6.2-sol;session=sha-256-64:a659371582e2e1e3;surface=exec --- .github/actions/setup-bazel-ci/fetch.sh | 46 ++++++++++++++++++ BUILD.bazel | 1 + repo/tests/BUILD.bazel | 15 ++++++ repo/tests/cache_fetch_test.sh | 1 + repo/tests/fetch_workspace_test.sh | 62 +++++++++++++++++++++++++ 5 files changed, 125 insertions(+) create mode 100755 repo/tests/fetch_workspace_test.sh diff --git a/.github/actions/setup-bazel-ci/fetch.sh b/.github/actions/setup-bazel-ci/fetch.sh index 44383900..a440aae8 100755 --- a/.github/actions/setup-bazel-ci/fetch.sh +++ b/.github/actions/setup-bazel-ci/fetch.sh @@ -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. @@ -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. @@ -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") diff --git a/BUILD.bazel b/BUILD.bazel index 3af156f9..33312bbc 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -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", diff --git a/repo/tests/BUILD.bazel b/repo/tests/BUILD.bazel index 0eec977d..d30f15e5 100644 --- a/repo/tests/BUILD.bazel +++ b/repo/tests/BUILD.bazel @@ -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", diff --git a/repo/tests/cache_fetch_test.sh b/repo/tests/cache_fetch_test.sh index 6f7175d0..6cc9bb34 100755 --- a/repo/tests/cache_fetch_test.sh +++ b/repo/tests/cache_fetch_test.sh @@ -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 diff --git a/repo/tests/fetch_workspace_test.sh b/repo/tests/fetch_workspace_test.sh new file mode 100755 index 00000000..57619b20 --- /dev/null +++ b/repo/tests/fetch_workspace_test.sh @@ -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' ]]