Repository: bazelbuild/bazel
Issue: aar_import does not offer a srcjar option
Labels: type: feature request
P2
team-Android

### Description of the problem / feature request:

aar_import doesn't provide a facility to link a source-jar, which limits IDEs abilities to materialize the sources of imported .aar libraries. 

### Feature requests: what underlying problem are you trying to solve with this feature?

Improved IDE integration.

### Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Try to do a `aar_import` and there is no API to do this. 

### What operating system are you running Bazel on?

Mac OS X

### What's the output of `bazel info release`?

0.26.1

###  Have you found anything relevant by searching the web?

Looked around issues and fora, chatting about it on slack, but nothing seems to have been conclusively decided.

### Any other information, logs, or outputs that you want to share?

Investigating AarImport.java, it seems that it creates a JavaCommon which could include source artifacts, but does not, explicitly passing in an empty immutable list.  This seems entirely feasible, but the API isn't exposed and the bits aren't hooked up. 

Issue comments:
This is something we'll want to add, but we'll have to find time to finish this up
Amazing.  Thank you!


Pull request review comments:

Patch that resolved the issue:
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AarImport.java b/src/main/java/com/google/devtools/build/lib/rules/android/AarImport.java
index 624286b9727dc3..6fb4bd43aabd76 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AarImport.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AarImport.java
@@ -33,18 +33,8 @@
 import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
 import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidAaptVersion;
 import com.google.devtools.build.lib.rules.android.databinding.DataBinding;
-import com.google.devtools.build.lib.rules.java.ImportDepsCheckActionBuilder;
-import com.google.devtools.build.lib.rules.java.JavaCommon;
-import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider;
-import com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts;
-import com.google.devtools.build.lib.rules.java.JavaConfiguration;
+import com.google.devtools.build.lib.rules.java.*;
 import com.google.devtools.build.lib.rules.java.JavaConfiguration.ImportDepsCheckingLevel;
-import com.google.devtools.build.lib.rules.java.JavaInfo;
-import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider;
-import com.google.devtools.build.lib.rules.java.JavaRuntimeInfo;
-import com.google.devtools.build.lib.rules.java.JavaSemantics;
-import com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider;
-import com.google.devtools.build.lib.rules.java.JavaToolchainProvider;
 import com.google.devtools.build.lib.vfs.PathFragment;
 import javax.annotation.Nullable;
 
@@ -192,12 +182,37 @@ public ConfiguredTarget create(RuleContext ruleContext)
             /* isNeverLink = */ JavaCommon.isNeverLink(ruleContext),
             /* srcLessDepsExport = */ false);
 
+    // Wire up the source jar for the current target and transitively source jars from dependencies.
+    ImmutableList<Artifact> srcJars = ImmutableList.of();
+    Artifact srcJar = ruleContext.getPrerequisiteArtifact("srcjar", Mode.TARGET);
+    NestedSetBuilder<Artifact> transitiveJavaSourceJarBuilder = NestedSetBuilder.stableOrder();
+    if (srcJar != null) {
+      srcJars = ImmutableList.of(srcJar);
+      transitiveJavaSourceJarBuilder.add(srcJar);
+    }
+    for (JavaSourceJarsProvider other :
+            JavaInfo.getProvidersFromListOfTargets(
+                    JavaSourceJarsProvider.class,
+                    ruleContext.getPrerequisites("exports", Mode.TARGET))) {
+      transitiveJavaSourceJarBuilder.addTransitive(other.getTransitiveSourceJars());
+    }
+    NestedSet<Artifact> transitiveJavaSourceJars = transitiveJavaSourceJarBuilder.build();
+    JavaSourceJarsProvider javaSourceJarsProvider =
+            JavaSourceJarsProvider.create(transitiveJavaSourceJars, srcJars);
+    JavaSourceInfoProvider javaSourceInfoProvider =
+            new JavaSourceInfoProvider.Builder()
+                    .setJarFiles(ImmutableList.of(mergedJar))
+                    .setSourceJarsForJarFiles(srcJars)
+                    .build();
+
     JavaInfo.Builder javaInfoBuilder =
         JavaInfo.Builder.create()
             .setRuntimeJars(ImmutableList.of(mergedJar))
             .setJavaConstraints(ImmutableList.of("android"))
             .setNeverlink(JavaCommon.isNeverLink(ruleContext))
             .addProvider(JavaCompilationArgsProvider.class, javaCompilationArgsProvider)
+            .addProvider(JavaSourceJarsProvider.class, javaSourceJarsProvider)
+            .addProvider(JavaSourceInfoProvider.class, javaSourceInfoProvider)
             .addProvider(JavaRuleOutputJarsProvider.class, jarProviderBuilder.build());
 
     common.addTransitiveInfoProviders(
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AarImportBaseRule.java b/src/main/java/com/google/devtools/build/lib/rules/android/AarImportBaseRule.java
index 90eb552b5d4889..291b293498a79b 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AarImportBaseRule.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AarImportBaseRule.java
@@ -27,6 +27,7 @@
 import com.google.devtools.build.lib.rules.android.AndroidRuleClasses.AndroidBaseRule;
 import com.google.devtools.build.lib.rules.java.JavaConfiguration;
 import com.google.devtools.build.lib.rules.java.JavaInfo;
+import com.google.devtools.build.lib.rules.java.JavaSemantics;
 import com.google.devtools.build.lib.util.FileType;
 
 /** Rule definition for the aar_import rule. */
@@ -53,6 +54,13 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
                 .allowedRuleClasses("aar_import", "java_import")
                 .allowedFileTypes()
                 .validityPredicate(ANY_EDGE))
+        /* <!-- #BLAZE_RULE(aar_import).ATTRIBUTE(srcjar) -->
+        A JAR file that contains source code for the compiled JAR files in the AAR.
+        <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
+        .add(
+            attr("srcjar", LABEL)
+                .allowedFileTypes(JavaSemantics.SOURCE_JAR, JavaSemantics.JAR)
+                .direct_compile_time_input())
         .add(
             attr(AAR_EMBEDDED_JARS_EXTACTOR, LABEL)
                 .cfg(HostTransition.createFactory())


Test patch: