Skip to content

Commit

Permalink
Don't register import deps checker actions if --experimental_import_d…
Browse files Browse the repository at this point in the history
…eps_checker is off

Fixes bazelbuild#8340

Closes bazelbuild#8341.

PiperOrigin-RevId: 248883983
  • Loading branch information
jin authored and irengrig committed Jun 18, 2019
1 parent 649dfcc commit a664be4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
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.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;

/**
* An implementation for the aar_import rule.
Expand Down Expand Up @@ -147,29 +149,35 @@ public ConfiguredTarget create(RuleContext ruleContext)
/* bothDeps = */ targets);
javaSemantics.checkRule(ruleContext, common);

Artifact jdepsArtifact = createAarArtifact(ruleContext, "jdeps.proto");

common.setJavaCompilationArtifacts(
new JavaCompilationArtifacts.Builder()
.addRuntimeJar(mergedJar)
.addCompileTimeJarAsFullJar(mergedJar)
// Allow direct dependents to compile against un-merged R classes
.addCompileTimeJarAsFullJar(
ruleContext.getImplicitOutputArtifact(
AndroidRuleClasses.ANDROID_RESOURCES_CLASS_JAR))
.setCompileTimeDependencies(jdepsArtifact)
.build());

JavaConfiguration javaConfig = ruleContext.getFragment(JavaConfiguration.class);
ImportDepsCheckActionBuilder.newBuilder()
.bootclasspath(getBootclasspath(ruleContext))
.declareDeps(getCompileTimeJarsFromCollection(targets, /*isDirect=*/ true))
.transitiveDeps(getCompileTimeJarsFromCollection(targets, /*isDirect=*/ false))
.checkJars(NestedSetBuilder.<Artifact>stableOrder().add(mergedJar).build())
.importDepsCheckingLevel(javaConfig.getImportDepsCheckingLevel())
.jdepsOutputArtifact(jdepsArtifact)
.ruleLabel(ruleContext.getLabel())
.buildAndRegister(ruleContext);
JavaCompilationArtifacts.Builder javaCompilationArtifactsBuilder =
new JavaCompilationArtifacts.Builder();

javaCompilationArtifactsBuilder
.addRuntimeJar(mergedJar)
.addCompileTimeJarAsFullJar(mergedJar)
// Allow direct dependents to compile against un-merged R classes
.addCompileTimeJarAsFullJar(
ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_CLASS_JAR));

Artifact jdepsArtifact = null;
// Don't register import deps checking actions if the level is off. Since it's off, the
// check isn't useful anyway, so don't waste resources running it.
if (javaConfig.getImportDepsCheckingLevel() != ImportDepsCheckingLevel.OFF) {
jdepsArtifact = createAarArtifact(ruleContext, "jdeps.proto");
javaCompilationArtifactsBuilder.setCompileTimeDependencies(jdepsArtifact);
ImportDepsCheckActionBuilder.newBuilder()
.bootclasspath(getBootclasspath(ruleContext))
.declareDeps(getCompileTimeJarsFromCollection(targets, /*isDirect=*/ true))
.transitiveDeps(getCompileTimeJarsFromCollection(targets, /*isDirect=*/ false))
.checkJars(NestedSetBuilder.<Artifact>stableOrder().add(mergedJar).build())
.importDepsCheckingLevel(javaConfig.getImportDepsCheckingLevel())
.jdepsOutputArtifact(jdepsArtifact)
.ruleLabel(ruleContext.getLabel())
.buildAndRegister(ruleContext);
}

common.setJavaCompilationArtifacts(javaCompilationArtifactsBuilder.build());

// We pass jdepsArtifact to create the action of extracting ANDROID_MANIFEST. Note that
// this action does not need jdepsArtifact. The only reason is that we need to check the
Expand Down Expand Up @@ -251,7 +259,7 @@ private static Action[] createSingleFileExtractorActions(
RuleContext ruleContext,
Artifact aar,
String filename,
Artifact jdepsOutputArtifact,
@Nullable Artifact jdepsOutputArtifact,
Artifact outputArtifact) {
SpawnAction.Builder builder =
new SpawnAction.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,14 @@ public void testDepsCheckerActionExistsForLevelWarning() throws Exception {
}

@Test
public void testDepsCheckerActionExistsForLevelOff() throws Exception {
checkDepsCheckerActionExistsForLevel(ImportDepsCheckingLevel.OFF, "silence");
public void testDepsCheckerActionDoesNotExistsForLevelOff() throws Exception {
useConfiguration("--experimental_import_deps_checking=off");
ConfiguredTarget aarImportTarget = getConfiguredTarget("//a:bar");
OutputGroupInfo outputGroupInfo = aarImportTarget.get(OutputGroupInfo.SKYLARK_CONSTRUCTOR);
NestedSet<Artifact> outputGroup =
outputGroupInfo.getOutputGroup(OutputGroupInfo.HIDDEN_TOP_LEVEL);
assertThat(outputGroup).hasSize(1);
assertThat(ActionsTestUtil.getFirstArtifactEndingWith(outputGroup, "jdeps.proto")).isNull();
}

private void checkDepsCheckerActionExistsForLevel(
Expand Down Expand Up @@ -459,6 +465,7 @@ public void testExportsPropagatesResources() throws Exception {

@Test
public void testJavaCompilationArgsProvider() throws Exception {
useConfiguration("--experimental_import_deps_checking=ERROR");
ConfiguredTarget aarImportTarget = getConfiguredTarget("//a:bar");

JavaCompilationArgsProvider provider =
Expand Down

0 comments on commit a664be4

Please sign in to comment.