Repository: bazelbuild/bazel
Issue: incompatible_use_remote_java_toolchain: use remote Java tools instead of the embedded deploy jars
Labels: incompatible-change

The option `--incompatible_use_remote_java_toolchain` will download the remote Java tools (archive of deploy jars) for the target configuration instead of using the corresponding embedded deploy jars. Enabling this flag only affects users of the Java rules.

# Migration Notes

There are no actions required from the users. No migration is needed.


Issue comments:
For this flag and the one in #7197 - what if users don't want to download this archive, or want to change the location/manner in which this archive is obtained? This would be important for CI systems with no internet connectivity, and orgs that want to audit/control build-time dependencies (both in terms of artifacts and systems). Will there be a flag moving forward that will disable this download?
@scottmin0r To not download the archive you have to set the flags `--java_toolchain` and `--host_java_toolchain` to point to local tools.
Specifically what should I pass for those flag values? As per [this comment](https://github.com/bazelbuild/bazel/issues/6656#issuecomment-461084131) I've tried:

(release 0.24.1)

```
build --javabase=@local_jdk//:jdk
build --host_javabase=@local_jdk//:jdk
build --java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8
build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8
```

but this still seems to try to download tools (with two different failure modes):

```
Analyzing: 1166 targets (68 packages loaded, 2267 targets configured)
Analyzing: 1166 targets (68 packages loaded, 2267 targets configured)
Analyzing: 1166 targets (68 packages loaded, 2267 targets configured)
Analyzing: 1166 targets (68 packages loaded, 2267 targets configured)
Analyzing: 1166 targets (68 packages loaded, 2267 targets configured)
ERROR: /home/kbuilder/.cache/bazel/_bazel_kbuilder/94efa1614cde89691caae16b65396550/external/bazel_tools/tools/jdk/BUILD:218:1: no such package '@remote_java_tools//': java.io.IOException: Error downloading [https://mirror.bazel.build/bazel_java_tools/java_tools_pkg-0.5.1.tar.gz] to /home/kbuilder/.cache/bazel/_bazel_kbuilder/94efa1614cde89691caae16b65396550/external/remote_java_tools/java_tools_pkg-0.5.1.tar.gz: Network is unreachable (connect failed) and referenced by '@bazel_tools//tools/jdk:JacocoCoverageRunner'
```

```
Analyzing: 56 targets (8 packages loaded, 587 targets configured)
Analyzing: 56 targets (8 packages loaded, 587 targets configured)
Analyzing: 56 targets (8 packages loaded, 587 targets configured)
Analyzing: 56 targets (8 packages loaded, 587 targets configured)
Analyzing: 56 targets (8 packages loaded, 587 targets configured)
Analyzing: 56 targets (8 packages loaded, 587 targets configured)
ERROR: /home/kbuilder/.cache/bazel/_bazel_kbuilder/1858cc0ae663ea49d21c8c6b3132f559/external/bazel_tools/tools/jdk/BUILD:153:1: no such package '@remote_java_tools//': java.io.IOException: thread interrupted and referenced by '@bazel_tools//tools/jdk:genclass'
```

Pull request review comments:

Patch that resolved the issue:
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java
index d4a72fdff59878..70e0c0679b2257 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java
@@ -586,7 +586,7 @@ public ImportDepsCheckingLevelConverter() {
 
   @Option(
       name = "incompatible_use_remote_java_toolchain",
-      defaultValue = "false",
+      defaultValue = "true",
       documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
       effectTags = {OptionEffectTag.UNKNOWN},
       metadataTags = {
@@ -600,7 +600,7 @@ public ImportDepsCheckingLevelConverter() {
 
   @Option(
       name = "incompatible_use_remote_host_java_toolchain",
-      defaultValue = "false",
+      defaultValue = "true",
       documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
       effectTags = {OptionEffectTag.UNKNOWN},
       metadataTags = {


Test patch:
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
index e58cd0e121b1f8..9f0557f2c4b487 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
@@ -108,6 +108,19 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
         "  genclass = ['GenClass_deploy.jar'],",
         "  ijar = ['ijar'],",
         ")",
+        "java_toolchain(",
+        "  name = 'remote_toolchain',",
+        "  source_version = '8',",
+        "  target_version = '8',",
+        "  bootclasspath = [':bootclasspath'],",
+        "  extclasspath = [':extclasspath'],",
+        "  javac = [':langtools'],",
+        "  javabuilder = ['JavaBuilder_deploy.jar'],",
+        "  header_compiler = ['turbine_deploy.jar'],",
+        "  singlejar = ['SingleJar_deploy.jar'],",
+        "  genclass = ['GenClass_deploy.jar'],",
+        "  ijar = ['ijar'],",
+        ")",
         "java_runtime(name = 'jdk', srcs = [])",
         "java_runtime(name = 'host_jdk', srcs = [])",
         "java_runtime(name = 'remote_jdk', srcs = [])",
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
index f86d67aa924f05..185c58acc6143b 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
@@ -1899,7 +1899,10 @@ public void javaToolchainFlag_default() throws Exception {
             configuredTarget.get(
                 new SkylarkKey(Label.parseAbsolute("//foo:rule.bzl", ImmutableMap.of()), "result"));
     Label javaToolchainLabel = ((Label) info.getValue("java_toolchain_label"));
-    assertThat(javaToolchainLabel.toString()).endsWith("jdk:toolchain");
+    assertThat(
+            javaToolchainLabel.toString().endsWith("jdk:remote_toolchain")
+                || javaToolchainLabel.toString().endsWith("jdk:toolchain"))
+        .isTrue();
   }
 
   @Test