Fix FATAL crash in bazel mod on dependency cycles (#31084)
Fixes #29437
### Description
When `bazel mod` encounters a dependency cycle (such as when an `archive_override` specifies a file overlay referencing the main repo, or module extensions form a cyclic dependency), Bazel previously crashed with an internal error:
```text
FATAL: bazel crashed due to an internal error. Printing stack trace:
java.lang.UnsupportedOperationException: unexpected access of cycle details
at com.google.devtools.build.skyframe.CycleInfo$CycleInfoNoDetails.getTopKey(CycleInfo.java:124)
at com.google.devtools.build.lib.bazel.commands.ModCommand.execInternal(ModCommand.java:301)
```
#### Root Cause
1. `ModCommand` evaluated the Skyframe keys using `skyframeExecutor.prepareAndGet(keys.build(), evaluationContext)`, which unconditionally forces `setStoreExactCycles(false)` (originally added for `SkyQueryEnvironment`).
2. When `storeExactCycles` is `false`, `SimpleCycleDetector` produces `CycleInfoNoDetails`. Calling `getTopKey()` or `getCycle()` on `CycleInfoNoDetails` unconditionally throws `UnsupportedOperationException: unexpected access of cycle details`.
3. `ModCommand` called `cycleInfo.getFirst().getTopKey()` without checking `hasCycleDetails()`, turning what should have been a clean error report into a fatal crash of the Bazel daemon.
4. Additionally, `CyclesReporter.reportCycles` called `cycleInfo.getTopKey()` and `cycleInfo.getCycle()` on each cycle without verifying `cycleInfo.hasCycleDetails()`.
#### Solution
1. In `ModCommand.java`, switch from `skyframeExecutor.prepareAndGet` to `skyframeExecutor.evaluate` with `keepGoing = true`. This preserves `storeExactCycles = true`, enabling `CyclesReporter` (with `BzlmodRepoCycleReporter` and `BzlLoadCycleReporter`) to cleanly display the exact circular dependency chain.
2. Defensively guard `new CyclesReporter(...).reportCycles(...)` in `ModCommand.java` with `cycleInfo.getFirst().hasCycleDetails()` so that even if cycle details are absent, `getTopKey()` is never called on a `CycleInfoNoDetails`.
3. In `CyclesReporter.java`, skip any `cycleInfo` where `!cycleInfo.hasCycleDetails()` to prevent crashes across all callers.
4. Added a unit test in `CyclesReporterTest` and an integration test in `mod_command_test.py`.
### Checklist
- [x] I have added tests for the new use cases (if any).
- [ ] I have updated the documentation (if applicable).
Closes #31084
COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/bazel/pull/31084 from jdymitarai:fix-mod-command-cycle-crash-29437 d0485fafb8d2cc740c3c5a8488342989ab3b7d61
PiperOrigin-RevId: 981241744
Change-Id: I120b90752c2c5f874ad4f7cbff54dee09783d6be
{Fast, Correct} - Choose two
Build and test software of any size, quickly and reliably.
Speed up your builds and tests: Bazel rebuilds only what is necessary. With advanced local and distributed caching, optimized dependency analysis and parallel execution, you get fast and incremental builds.
One tool, multiple languages: Build and test Java, C++, Android, iOS, Go, and a wide variety of other language platforms. Bazel runs on Windows, macOS, and Linux.
Scalable: Bazel helps you scale your organization, codebase, and continuous integration solution. It handles codebases of any size, in multiple repositories or a huge monorepo.
Extensible to your needs: Easily add support for new languages and platforms with Bazel's familiar extension language. Share and re-use language rules written by the growing Bazel community.
To report a security issue, please email security@bazel.build with a description of the issue, the steps you took to create the issue, affected versions, and, if known, mitigations for the issue. Our vulnerability management team will respond within 3 working days of your email. If the issue is confirmed as a vulnerability, we will open a Security Advisory. This project follows a 90 day disclosure timeline.
See CONTRIBUTING.md