fix: self-heal //external query on bzlmod-only workspaces#420
Merged
tinder-maxwellelliott merged 1 commit intoJul 9, 2026
Merged
Conversation
`generate-hashes` and the `serve` warmup could hard-fail with:
ERROR: no such package 'external': //external package is not available
since the WORKSPACE file is deprecated, please migrate to Bzlmod.
(Bazel query failed, exit code 7)
`BazelClient.queryAllTargets` decides whether to query `//external:all-targets`
from `BazelModService.isBzlmodEnabled`, which is only "`bazel mod graph` exited
0". In restricted/cloud deploys that probe can false-negative (it can't resolve
the module graph), so `//external:all-targets` is included and Bazel rejects it
because the package is unavailable in bzlmod-only mode. For `serve` this broke
both warmup and cold on-demand requests for such workspaces.
Fix reacts to ground truth instead of the probe:
- `BazelQueryService.runQuery` tees + captures stderr (switching `stderr` from
`Redirect.PRINT` to `Redirect.Consume`) and, when a query fails because
`//external` is unavailable, throws a typed `ExternalPackageUnavailableException`
instead of an opaque "exit code 7".
- `BazelClient` catches it, drops `//external:all-targets`, and retries once. It
latches a `@Volatile externalPackageUnavailable` flag so later revisions served
by a long-running `serve` skip `//external` up front instead of failing and
retrying every time.
This cannot misfire for genuine WORKSPACE mode: there the query succeeds, so no
retry happens and `//external` targets are still hashed.
Adds BazelClientTest cases for the non-cquery retry, the cquery external-repo
drop, and the subsequent-call latch.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
generate-hashesand theservewarmup could hard-fail on a Bzlmod-only workspace with:BazelClient.queryAllTargetsdecides whether to query//external:all-targetsfromBazelModService.isBzlmodEnabled, whose only signal is "bazel mod graphexited 0". In restricted/cloud deploy environments that probe can false-negative (it can't resolve the module graph / lockfile), so//external:all-targetsgets included and Bazel rejects it because the package is unavailable in bzlmod-only mode (e.g. Bazel 8/9 with--enable_workspace=false).For
servethis was worse than the warmup warning implies — a cold on-demand request hits the same crash, so the service was effectively broken for such workspaces.Fix
React to ground truth instead of the probe:
BazelQueryService.runQuerynow tees + captures Bazel's stderr (switchingstderrfromRedirect.PRINTtoRedirect.Consume) and, when a query fails specifically because//externalis unavailable (no such package 'external'///external package is not available), throws a typedExternalPackageUnavailableExceptioninstead of an opaque "exit code 7".BazelClientcatches it, drops//external:all-targets, and retries once. It latches a@Volatile externalPackageUnavailableflag so later revisions served by a long-runningserveprocess skip//externalup front rather than failing-then-retrying every time.This cannot misfire for genuine WORKSPACE mode: there the query succeeds, so no retry happens and
//externaltargets are still hashed as before. It also fixesgenerate-hashes, which shares the same query path.Testing
BazelClientTest— added 3 cases: non-cquery retry-without-external, cquery external-repo drop, and the subsequent-call latch. All pass; existing cases unchanged.BazelModServiceTest,HashServiceTest,ImpactedTargetsServiceTest,ServeCommandTest— pass.E2ETest— the 4 local failures (testUseCqueryWith*,testBzlmodTransitiveDepsCquery,testFineGrainedHashExternalRepo) are pre-existing and environmental; confirmed identical on baseline with this change stashed (Unable to run coursier: Unable to locate a Java Runtime— the local Bazel sandbox has no JDK forrules_jvm_external).Notes for reviewers
getModuleGraphJson(), disabling MODULE.bazel-change detection in that environment. Could be a follow-up (e.g. strengtheningisBzlmodEnabled), but it's independent of this crash fix.Redirect.Consumestill forwards Bazel's stderr live (line-buffered), so query progress/errors remain visible.🤖 Generated with Claude Code