Skip to content

Commit

Permalink
Add toggle for ijar in java_import
Browse files Browse the repository at this point in the history
Problem: java_import has been unusably broken for years for JARs with Kotlin interfaces, since ijar strips out important information. This has caused multiple dependent projects (includng official Bazel ones) to abandon java_import in favor of rolling their own versions, which themselves contain issues that are getting fixed in java_import. Fragmentation is bad, and fragmentation of bugs and fixes is worse.
For more, see
https://github.com/bazelbuild/rules_jvm_external/blob/master/private/rules/jvm_import.bzl
bazelbuild#4549
bazelbuild#14966
bazel-contrib/rules_jvm_external#672

Temporary solution: Until such time as ijar is fixed for Kotlin, this adds a toggle that enables/disables ijar on java_import. This should unblock use of java_import for libraries that might contain Kotlin, so implementations can reunify.

It also restores java_import to a state where it works correctly by default. Per the user manual, ijars are a build performance optimization to allow caching of actions that use JARs whose implementations change frequenly. Imported (externally compiled) JARs shouldn't be changing very often, meaning that the build performance cost of disabling ijars for these prebuilt JARs should be relatively low. Therefore, the ijar toggle is off by default, so that the build is correct by default. But ijar is still available though the toggle, just in case someone is importing a Java-interface-only JAR that they change all the time.
  • Loading branch information
cpsauer committed Mar 5, 2022
1 parent e33b54e commit 2002932
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.JavaOutput;
import com.google.devtools.build.lib.packages.Type;
import java.util.LinkedHashSet;
import java.util.Set;

Expand Down Expand Up @@ -202,7 +203,8 @@ private ImmutableList<Artifact> processWithIjarIfNeeded(
RuleContext ruleContext,
ImmutableMap.Builder<Artifact, Artifact> compilationToRuntimeJarMap) {
ImmutableList.Builder<Artifact> interfaceJarsBuilder = ImmutableList.builder();
boolean useIjar = ruleContext.getFragment(JavaConfiguration.class).getUseIjars();
boolean useIjar = ruleContext.getFragment(JavaConfiguration.class).getUseIjars()
&& ruleContext.attributes().get("use_ijar", Type.BOOLEAN);
for (Artifact jar : jars) {
Artifact interfaceJar =
useIjar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment envi
for IDE plug-ins or <code>tools.jar</code> for anything running on
a standard JDK.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("use_ijar", BOOLEAN).value(false))
/* <!-- #BLAZE_RULE($java_import_base).ATTRIBUTE(use_ijar) -->
If you import an oft-changing JAR that doesn't have a Kotlin interface,
you can enable this as a caching optimization.
Temporarily available until ijar is updated to properly handle Kotlin.
For more, see https://github.com/bazelbuild/bazel/issues/4549.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("neverlink", BOOLEAN).value(false))
/* <!-- #BLAZE_RULE($java_import_base).ATTRIBUTE(constraints) -->
Extra constraints imposed on this rule as a Java library.
Expand Down

0 comments on commit 2002932

Please sign in to comment.