Skip to content

Commit

Permalink
Fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
rsalvador committed Aug 2, 2024
1 parent 9126e4b commit fd6ee8b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -125,7 +127,13 @@ public BlazeJavacResult run(JavaLibraryBuildRequest build) throws Exception {
try {
result = compileJavaLibrary(build);
if (result.isOk()) {
buildJar(build);
StringWriter errOutput = new StringWriter();
PrintWriter errWriter = new PrintWriter(errOutput);
buildJar(build, errWriter);
String output = errOutput.toString().trim();
if (output.length() > 0) {
result = BlazeJavacResult.createFullResult(result.status(), result.diagnostics(), output, result.statistics());
}
nativeHeaderOutput(build);
}
if (!build.getProcessors().isEmpty()) {
Expand All @@ -145,7 +153,7 @@ public BlazeJavacResult run(JavaLibraryBuildRequest build) throws Exception {
return result;
}

public void buildJar(JavaLibraryBuildRequest build) throws IOException {
public void buildJar(JavaLibraryBuildRequest build, PrintWriter errWriter) throws IOException {
Files.createDirectories(build.getOutputJar().getParent());
JarCreator jar = new JarCreator(build.getOutputJar());
JacocoInstrumentationProcessor processor = null;
Expand All @@ -156,7 +164,7 @@ public void buildJar(JavaLibraryBuildRequest build) throws IOException {
jar.setJarOwner(build.getTargetLabel(), build.getInjectingRuleKind());
processor = build.getJacocoInstrumentationProcessor();
if (processor != null) {
processor.processRequest(build, processor.isNewCoverageImplementation() ? jar : null);
processor.processRequest(build, processor.isNewCoverageImplementation() ? jar : null, errWriter);
}
} finally {
jar.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -30,6 +31,7 @@
import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator;

import com.google.common.base.Throwables;
import com.google.common.io.MoreFiles;
import com.google.common.io.RecursiveDeleteOption;
import com.google.devtools.build.buildjar.InvalidCommandLineException;
Expand Down Expand Up @@ -73,7 +75,7 @@ public boolean isNewCoverageImplementation() {
* Instruments classes using Jacoco and keeps copies of uninstrumented class files in
* jacocoMetadataDir, to be zipped up in the output file jacocoMetadataOutput.
*/
public void processRequest(JavaLibraryBuildRequest build, JarCreator jar) throws IOException {
public void processRequest(JavaLibraryBuildRequest build, JarCreator jar, PrintWriter errWriter) throws IOException {
// Use a directory for coverage metadata that is unique to each built jar. Avoids
// multiple threads performing read/write/delete actions on the instrumented classes directory.
instrumentedClassesDirectory = getMetadataDirRelativeToJar(build.getOutputJar());
Expand All @@ -84,7 +86,7 @@ public void processRequest(JavaLibraryBuildRequest build, JarCreator jar) throws
jar.setNormalize(true);
jar.setCompression(build.compressJar());
Instrumenter instr = new Instrumenter(new OfflineInstrumentationAccessGenerator());
instrumentRecursively(instr, build.getClassDir());
instrumentRecursively(instr, build.getClassDir(), errWriter);
jar.addDirectory(instrumentedClassesDirectory);
if (isNewCoverageImplementation) {
jar.addEntry(coverageInformation, coverageInformation);
Expand All @@ -109,7 +111,7 @@ private static Path getMetadataDirRelativeToJar(Path outputJar) {
/**
* Runs Jacoco instrumentation processor over all .class files recursively, starting with root.
*/
private void instrumentRecursively(Instrumenter instr, Path root) throws IOException {
private void instrumentRecursively(Instrumenter instr, Path root, PrintWriter errWriter) throws IOException {
Files.walkFileTree(
root,
new SimpleFileVisitor<Path>() {
Expand Down Expand Up @@ -149,7 +151,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throw e; // Bubble up to the outer broader safety catch block for logging.
}
} catch (Exception e) {
System.err.printf("WARNING: %s was not instrumented: %s%n\n", file, e.getMessage());
errWriter.printf("WARNING: %s was not instrumented: %s\n", file, Throwables.getRootCause(e).toString());
e.printStackTrace();
}
return FileVisitResult.CONTINUE;
}
Expand Down

0 comments on commit fd6ee8b

Please sign in to comment.