Skip to content

Commit b603de0

Browse files
committed
Add --starlark_coverage_report
If the new flag `--starlark_coverage_report` is set to a non-empty value, the Starlark coverage recorder will be enabled for all `.bzl` files under the output base executed during the Blaze command. At the end of the command, the collected coverage data will be emitted to the file path given as the flag argument in LCOV format.
1 parent 90607e1 commit b603de0

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java

+19
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import java.io.FileOutputStream;
6868
import java.io.IOException;
6969
import java.io.OutputStream;
70+
import java.io.PrintWriter;
7071
import java.time.Duration;
7172
import java.util.ArrayList;
7273
import java.util.List;
@@ -371,6 +372,12 @@ private BlazeCommandResult execExclusively(
371372
message, FailureDetails.Command.Code.STARLARK_CPU_PROFILING_INITIALIZATION_FAILURE);
372373
}
373374
}
375+
if (!commonOptions.starlarkCoverageReport.isEmpty()) {
376+
// Record coverage for all `.bzl` files, excluding the built-ins, which don't have paths that
377+
// could be resolved when a human-readable coverage report is generated.
378+
Starlark.startCoverageCollection(
379+
path -> !path.startsWith("/virtual_builtins_bzl") && path.endsWith(".bzl"));
380+
}
374381

375382
BlazeCommandResult result =
376383
createDetailedCommandResult(
@@ -603,6 +610,18 @@ private BlazeCommandResult execExclusively(
603610
}
604611
}
605612
}
613+
if (!commonOptions.starlarkCoverageReport.isEmpty()) {
614+
FileOutputStream out;
615+
try {
616+
out = new FileOutputStream(commonOptions.starlarkCoverageReport);
617+
} catch (IOException ex) {
618+
String message = "Starlark coverage recorder: " + ex.getMessage();
619+
outErr.printErrLn(message);
620+
return createDetailedCommandResult(
621+
message, FailureDetails.Command.Code.STARLARK_COVERAGE_REPORT_DUMP_FAILURE);
622+
}
623+
Starlark.dumpCoverage(new PrintWriter(out));
624+
}
606625

607626
needToCallAfterCommand = false;
608627
return runtime.afterCommand(env, result);

src/main/java/com/google/devtools/build/lib/runtime/CommonCommandOptions.java

+9
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,15 @@ public String getTypeDescription() {
293293
help = "Writes into the specified file a pprof profile of CPU usage by all Starlark threads.")
294294
public String starlarkCpuProfile;
295295

296+
@Option(
297+
name = "starlark_coverage_report",
298+
defaultValue = "",
299+
documentationCategory = OptionDocumentationCategory.LOGGING,
300+
effectTags = {OptionEffectTag.BAZEL_MONITORING},
301+
help = "Writes into the specified file an LCOV coverage report for all Starlark files "
302+
+ "executed for the requested Bazel command.")
303+
public String starlarkCoverageReport;
304+
296305
@Option(
297306
name = "record_full_profiler_data",
298307
defaultValue = "false",

src/main/protobuf/failure_details.proto

+1
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ message Command {
533533
NOT_IN_WORKSPACE = 12 [(metadata) = { exit_code: 2 }];
534534
SPACES_IN_WORKSPACE_PATH = 13 [(metadata) = { exit_code: 36 }];
535535
IN_OUTPUT_DIRECTORY = 14 [(metadata) = { exit_code: 2 }];
536+
STARLARK_COVERAGE_REPORT_DUMP_FAILURE = 15 [(metadata) = { exit_code: 36 }];
536537
}
537538

538539
Code code = 1;

0 commit comments

Comments
 (0)