Enable coverage for executing a java binary with --experimental_java_coverage.
When using --experimental_java_coverage coverage was not collected after building a java deploy jar in coverage mode and executing the corresponding binary (see Example 1 below). The identified issues:
1) JacocoCoverageRunner did not know it has to use the new implementation. It decides whether to use the new implementation based on the suffix of JACOCO_METADATA_JAR, which is unset in this case. To solve this we now set an environment variable in the java_stub_template to mark if the new implementation is used or not.
2) JarInputStream could not see the entries in the deploy jar. I fetched the entries directly from the JarFile instead.
Example 1)
bazel build --collect_code_coverage --experimental_java_coverage //java/main:main_deploy.jar
JAVA_COVERAGE_FILE=java_coverage.dat bazel-bin/java/main/main --singlejar
RELNOTES: None.
PiperOrigin-RevId: 234617047
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java
index f627e85..399f7af 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java
@@ -383,6 +383,10 @@
"export JACOCO_JAVA_RUNFILES_ROOT=${JAVA_RUNFILES}/" + workspacePrefix)
);
arguments.add(
+ Substitution.of(
+ JavaSemantics.JAVA_COVERAGE_NEW_IMPLEMENTATION_PLACEHOLDER,
+ "export JAVA_COVERAGE_NEW_IMPLEMENTATION=YES"));
+ arguments.add(
Substitution.of("%java_start_class%", ShellEscaper.escapeString(javaStartClass)));
} else {
arguments.add(Substitution.of(JavaSemantics.JACOCO_METADATA_PLACEHOLDER,
@@ -390,6 +394,10 @@
? "export JACOCO_METADATA_JAR=" + path : ""));
arguments.add(Substitution.of(JavaSemantics.JACOCO_MAIN_CLASS_PLACEHOLDER, ""));
arguments.add(Substitution.of(JavaSemantics.JACOCO_JAVA_RUNFILES_ROOT_PLACEHOLDER, ""));
+ arguments.add(
+ Substitution.of(
+ JavaSemantics.JAVA_COVERAGE_NEW_IMPLEMENTATION_PLACEHOLDER,
+ "export JAVA_COVERAGE_NEW_IMPLEMENTATION=NO"));
}
arguments.add(Substitution.of("%java_start_class%",
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt
index e394eef..27e5763 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt
@@ -259,6 +259,7 @@
%set_jacoco_metadata%
%set_jacoco_main_class%
%set_jacoco_java_runfiles_root%
+%set_java_coverage_new_implementation%
if [[ -n "$JVM_DEBUG_PORT" ]]; then
JVM_DEBUG_SUSPEND=${DEFAULT_JVM_DEBUG_SUSPEND:-"y"}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java
index b596ba4..674d658 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java
@@ -188,6 +188,7 @@
String JACOCO_METADATA_PLACEHOLDER = "%set_jacoco_metadata%";
String JACOCO_MAIN_CLASS_PLACEHOLDER = "%set_jacoco_main_class%";
String JACOCO_JAVA_RUNFILES_ROOT_PLACEHOLDER = "%set_jacoco_java_runfiles_root%";
+ String JAVA_COVERAGE_NEW_IMPLEMENTATION_PLACEHOLDER = "%set_java_coverage_new_implementation%";
/** Substitution for exporting the jars needed for jacoco coverage. */
class ComputedJacocoSubstitution extends ComputedSubstitution {