diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java index 8ae3fad9cf94f2..fe3256ff11f51a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java @@ -777,7 +777,9 @@ private void addArgsAndJarsToAttributes( attributes.addDirectJars(directJars); } - attributes.merge(args); + boolean pruneTransitiveDeps = ruleContext.getFragment(JavaConfiguration.class) + .experimentalPruneTransitiveDeps(); + attributes.merge(args, pruneTransitiveDeps); } private void addLibrariesToAttributesInternal(Iterable deps) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java index 66eb125d51fac9..84c3e1eb01803c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java @@ -113,6 +113,7 @@ public enum ImportDepsCheckingLevel { private final boolean multiReleaseDeployJars; private final boolean disallowJavaImportExports; private final boolean disallowJavaImportEmptyJars; + private final boolean experimentalPruneTransitiveDeps; // TODO(dmarting): remove once we have a proper solution for #2539 private final boolean useLegacyBazelJavaTest; @@ -156,6 +157,7 @@ public JavaConfiguration(BuildOptions buildOptions) throws InvalidConfigurationE this.multiReleaseDeployJars = javaOptions.multiReleaseDeployJars; this.disallowJavaImportExports = javaOptions.disallowJavaImportExports; this.disallowJavaImportEmptyJars = javaOptions.disallowJavaImportEmptyJars; + this.experimentalPruneTransitiveDeps = javaOptions.experimentalPruneTransitiveDeps; Map optimizers = javaOptions.bytecodeOptimizers; if (optimizers.size() > 1) { @@ -466,4 +468,8 @@ public boolean experimentalEnableJspecify() { public boolean requireJavaPluginInfo() { return requireJavaPluginInfo; } + + public boolean experimentalPruneTransitiveDeps() { + return experimentalPruneTransitiveDeps; + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java index a4d1d0556d3529..375aa35e1ab7eb 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java @@ -113,9 +113,11 @@ public ConfiguredTarget create(RuleContext ruleContext) ruleContext.getUniqueDirectoryArtifact( "_java_import", "jdeps.proto", ruleContext.getBinOrGenfilesDirectory()); JavaCompilationArgsProvider provider = JavaCompilationArgsProvider.legacyFromTargets(targets); + boolean pruneTransitiveDeps = ruleContext.getFragment(JavaConfiguration.class) + .experimentalPruneTransitiveDeps(); JavaTargetAttributes attributes = new JavaTargetAttributes.Builder(semantics) - .merge(provider) + .merge(provider, pruneTransitiveDeps) .addDirectJars(provider.getDirectCompileTimeJars()) .build(); ImportDepsCheckActionBuilder.newBuilder() diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java index 41bba1b73f433d..d25d069d2e32a0 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java @@ -24,6 +24,7 @@ import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.config.CoreOptionConverters.StrictDepsMode; +import com.google.devtools.build.lib.collect.nestedset.NestedSet; 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.JavaConfiguration.JavaClasspathMode; @@ -404,7 +405,14 @@ private void addDepsToAttributes(JavaTargetAttributes.Builder attributes) { attributes.addDirectJars(mergedDeps.getDirectCompileTimeJars()); } - attributes.addCompileTimeClassPathEntries(mergedDeps.getTransitiveCompileTimeJars()); + boolean pruneTransitiveDeps = ruleContext.getFragment(JavaConfiguration.class) + .experimentalPruneTransitiveDeps(); + + NestedSet localCompileTimeDeps = pruneTransitiveDeps + ? mergedDeps.getDirectCompileTimeJars() + : mergedDeps.getTransitiveCompileTimeJars(); + + attributes.addCompileTimeClassPathEntries(localCompileTimeDeps); attributes.addRuntimeClassPathEntries(mergedRuntimeDeps.getRuntimeJars()); attributes.addRuntimeClassPathEntries(mergedDeps.getRuntimeJars()); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java index 9b4dc16f56fc89..553b332be3adc8 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java @@ -621,6 +621,21 @@ public ImportDepsCheckingLevelConverter() { effectTags = {OptionEffectTag.UNKNOWN}, help = "Enable experimental jspecify integration.") public boolean experimentalEnableJspecify; + + @Option( + name = "experimental_prune_transitive_deps", + defaultValue = "false", + documentationCategory = OptionDocumentationCategory.BUILD_TIME_OPTIMIZATION, + effectTags = { + OptionEffectTag.LOADING_AND_ANALYSIS, + OptionEffectTag.EXECUTION, + OptionEffectTag.AFFECTS_OUTPUTS}, + metadataTags = {OptionMetadataTag.EXPERIMENTAL}, + help = + "If enabled, compilation is performed against only direct dependencies. Transitive deps " + + "required for compilation must be explicitly added") + public boolean experimentalPruneTransitiveDeps; + @Override public FragmentOptions getHost() { // Note validation actions don't run in host config, so no need copying flags related to that. diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java index fedee3356aa4ef..9b219c4fcf3cd1 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java @@ -129,9 +129,10 @@ public Builder addSourceFiles(Iterable sourceFiles) { } @CanIgnoreReturnValue - public Builder merge(JavaCompilationArgsProvider context) { - Preconditions.checkArgument(!built); - addCompileTimeClassPathEntries(context.getTransitiveCompileTimeJars()); + public Builder merge(JavaCompilationArgsProvider context, boolean pruneTransitiveDeps) { + addCompileTimeClassPathEntries( + pruneTransitiveDeps ? context.getDirectCompileTimeJars() + : context.getTransitiveCompileTimeJars()); addRuntimeClassPathEntries(context.getRuntimeJars()); return this; }