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 (including 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 optionally disables ijar on java_import. This should unblock use of java_import for libraries that might contain Kotlin and allow implementations to reunify.
  • Loading branch information
cpsauer committed Mar 29, 2022
1 parent e33b54e commit 6e2c9b2
Show file tree
Hide file tree
Showing 2 changed files with 11 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("safe_for_kotlin_interfaces", Type.BOOLEAN);
for (Artifact jar : jars) {
Artifact interfaceJar =
useIjar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment envi
The list of JAR files provided to Java targets that depend on this target.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("jars", LABEL_LIST).mandatory().allowedFileTypes(JavaSemantics.JAR))
/* <!-- #BLAZE_RULE($java_import_base).ATTRIBUTE(safe_for_kotlin_interfaces) -->
If your JARs contain a Kotlin interface, you need to enable this or use
kt_jvm_import instead.
Otherwise, a caching, performance optimization (ijar) breaks compilation.
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("safe_for_kotlin_interfaces", BOOLEAN).value(false))
/* <!-- #BLAZE_RULE($java_import_base).ATTRIBUTE(srcjar) -->
A JAR file that contains source code for the compiled JAR files.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
Expand Down

0 comments on commit 6e2c9b2

Please sign in to comment.