diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/ResourceJarActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/java/ResourceJarActionBuilder.java index 29332622a1a30b..a549af3665b236 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/ResourceJarActionBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/ResourceJarActionBuilder.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType; import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode; @@ -119,6 +120,17 @@ public void build(JavaSemantics semantics, RuleContext ruleContext) { if (!classpathResources.isEmpty()) { command.addExecPaths("--classpath_resources", classpathResources); } + // TODO(b/37444705): remove this logic and always call useParameterFile once the bug is fixed + // Most resource jar actions are very small and expanding the argument list for + // ParamFileHelper#getParamsFileMaybe is expensive, so avoid doing that work if + // we definitely don't need a params file. + // This heuristic could be much more aggressive, but we don't ever want to skip + // the params file in situations where it is required for --min_param_file_size. + if (sizeGreaterThanOrEqual( + Iterables.concat(messages, resources.values(), resourceJars, classpathResources), 10) + || ruleContext.getConfiguration().getMinParamFileSize() < 10000) { + builder.useParameterFile(ParameterFileType.SHELL_QUOTED); + } ruleContext.registerAction( builder .addOutput(outputJar) @@ -126,13 +138,16 @@ public void build(JavaSemantics semantics, RuleContext ruleContext) { .addInputs(resources.values()) .addTransitiveInputs(resourceJars) .addInputs(classpathResources) - .useParameterFile(ParameterFileType.SHELL_QUOTED) .setCommandLine(command.build()) .setProgressMessage("Building Java resource jar") .setMnemonic("JavaResourceJar") .build(ruleContext)); } + boolean sizeGreaterThanOrEqual(Iterable elements, int size) { + return Iterables.size(Iterables.limit(elements, size)) == size; + } + private static void addAsResourcePrefixedExecPath( PathFragment resourcePath, Artifact artifact, CustomCommandLine.Builder builder) { PathFragment execPath = artifact.getExecPath();