diff --git a/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java b/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java index 0a2661fde95dc7..51162dd1491951 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java @@ -340,10 +340,22 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException { starlarkRepositoryFunction.setWorkerExecutorService(repoFetchingWorkerThreadPool); break; case VIRTUAL: - throw new AbruptExitException( - detailedExitCode( - "using a virtual worker thread for repo fetching is not yet supported", - Code.BAD_DOWNLOADER_CONFIG)); + try { + // Since Google hasn't migrated to JDK 21 yet, we can't directly call + // Executors.newVirtualThreadPerTaskExecutor here. But a bit of reflection never hurt + // anyone... right? (OSS Bazel already ships with a bundled JDK 21) + starlarkRepositoryFunction.setWorkerExecutorService( + (ExecutorService) + Executors.class + .getDeclaredMethod("newVirtualThreadPerTaskExecutor") + .invoke(null)); + } catch (ReflectiveOperationException e) { + throw new AbruptExitException( + detailedExitCode( + "couldn't create virtual worker thread executor for repo fetching", + Code.BAD_DOWNLOADER_CONFIG), + e); + } } downloadManager.setDisableDownload(repoOptions.disableDownload); if (repoOptions.repositoryDownloaderRetries >= 0) { diff --git a/src/test/shell/bazel/starlark_repository_test.sh b/src/test/shell/bazel/starlark_repository_test.sh index d5008c47cd5dd4..8493fe92f86ca0 100755 --- a/src/test/shell/bazel/starlark_repository_test.sh +++ b/src/test/shell/bazel/starlark_repository_test.sh @@ -2384,6 +2384,12 @@ EOF bazel build @foo//:bar --experimental_worker_for_repo_fetching=platform >& $TEST_log \ || fail "Expected build to succeed" expect_log_n "hello world!" 1 + + # virtual worker thread, never restarts + bazel shutdown + bazel build @foo//:bar --experimental_worker_for_repo_fetching=virtual >& $TEST_log \ + || fail "Expected build to succeed" + expect_log_n "hello world!" 1 } function test_duplicate_value_in_environ() {