Skip to content

Commit 14d9dec

Browse files
committed
Add toggle for ijar in java_import
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 #4549 #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 [1]. 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 the build is correct by default. But ijar is still made available though the toggle, just in case someone is importing a Java-interface-only JAR that they change all the time. [1] https://docs.bazel.build/versions/main/user-manual.html#flag--use_ijars
1 parent e33b54e commit 14d9dec

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
3333
import com.google.devtools.build.lib.collect.nestedset.Order;
3434
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.JavaOutput;
35+
import com.google.devtools.build.lib.packages.Type;
3536
import java.util.LinkedHashSet;
3637
import java.util.Set;
3738

@@ -202,7 +203,8 @@ private ImmutableList<Artifact> processWithIjarIfNeeded(
202203
RuleContext ruleContext,
203204
ImmutableMap.Builder<Artifact, Artifact> compilationToRuntimeJarMap) {
204205
ImmutableList.Builder<Artifact> interfaceJarsBuilder = ImmutableList.builder();
205-
boolean useIjar = ruleContext.getFragment(JavaConfiguration.class).getUseIjars();
206+
boolean useIjar = ruleContext.getFragment(JavaConfiguration.class).getUseIjars()
207+
&& ruleContext.attributes().get("use_ijar", Type.BOOLEAN);
206208
for (Artifact jar : jars) {
207209
Artifact interfaceJar =
208210
useIjar

src/main/java/com/google/devtools/build/lib/rules/java/JavaImportBaseRule.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment envi
5454
for IDE plug-ins or <code>tools.jar</code> for anything running on
5555
a standard JDK.
5656
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
57+
.add(attr("use_ijar", BOOLEAN).value(false))
58+
/* <!-- #BLAZE_RULE($java_import_base).ATTRIBUTE(use_ijar) -->
59+
If you import an oft-changing JAR that doesn't have a Kotlin interface,
60+
you can enable this as a caching optimization.
61+
Temporarily available until ijar is updated to properly handle Kotlin.
62+
For more, see https://github.com/bazelbuild/bazel/issues/4549.
63+
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
5764
.add(attr("neverlink", BOOLEAN).value(false))
5865
/* <!-- #BLAZE_RULE($java_import_base).ATTRIBUTE(constraints) -->
5966
Extra constraints imposed on this rule as a Java library.

src/test/java/com/google/devtools/build/lib/view/java/JavaImportConfiguredTargetTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ public final void writeBuildFile() throws Exception {
5555
scratch.file(
5656
"java/jarlib/BUILD",
5757
"java_import(name = 'libraryjar',",
58-
" jars = ['library.jar'])",
58+
" jars = ['library.jar'],",
59+
" use_ijar = True)",
5960
"java_import(name = 'libraryjar_with_srcjar',",
6061
" jars = ['library.jar'],",
61-
" srcjar = 'library.srcjar')");
62+
" srcjar = 'library.srcjar',",
63+
" use_ijar = True)");
6264
}
6365

6466
@Test
@@ -130,11 +132,14 @@ public void testDeps() throws Exception {
130132
" jars = ['import.jar'],",
131133
" deps = ['//java/jarlib2:depjar'],",
132134
" exports = ['//java/jarlib2:exportjar'],",
135+
" use_ijar = True",
133136
")",
134137
"java_import(name = 'depjar',",
135-
" jars = ['depjar.jar'])",
138+
" jars = ['depjar.jar'],",
139+
" use_ijar = True)",
136140
"java_import(name = 'exportjar',",
137-
" jars = ['exportjar.jar'])");
141+
" jars = ['exportjar.jar'],",
142+
" use_ijar = True)");
138143

139144
ConfiguredTarget importJar = getConfiguredTarget("//java/jarlib2:import-jar");
140145

@@ -210,7 +215,8 @@ public void testFromGenrule() throws Exception {
210215
"java_import(name = 'library-jar',",
211216
" jars = [':generated_jar'],",
212217
" srcjar = ':generated_src_jar',",
213-
" exports = ['//java/jarlib:libraryjar'])");
218+
" exports = ['//java/jarlib:libraryjar'],",
219+
" use_ijar = True)");
214220
ConfiguredTarget jarLib = getConfiguredTarget("//java/genrules:library-jar");
215221

216222
JavaCompilationArgsProvider compilationArgs =
@@ -441,7 +447,8 @@ public void testTransitiveDependencies() throws Exception {
441447
" deps = ['//java/jarlib:libraryjar'])",
442448
"java_import(name = 'library2-jar',",
443449
" jars = ['library2.jar'],",
444-
" exports = [':lib'])",
450+
" exports = [':lib'],",
451+
" use_ijar = True)",
445452
"java_library(name = 'javalib2',",
446453
" srcs = ['Other.java'],",
447454
" deps = [':library2-jar'])");
@@ -464,6 +471,7 @@ public void testRuntimeDepsAreNotOnClasspath() throws Exception {
464471
" name = 'import_dep',",
465472
" jars = ['import_compile.jar'],",
466473
" runtime_deps = ['import_runtime.jar'],",
474+
" use_ijar = True",
467475
")",
468476
"java_library(",
469477
" name = 'library_dep',",

0 commit comments

Comments
 (0)