Skip to content

Commit

Permalink
Add --starlark_coverage_report
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fmeum committed May 31, 2022
1 parent d308a55 commit 4e8e8ed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -371,6 +372,12 @@ private BlazeCommandResult execExclusively(
message, FailureDetails.Command.Code.STARLARK_CPU_PROFILING_INITIALIZATION_FAILURE);
}
}
if (!commonOptions.starlarkCoverageReport.isEmpty()) {
// Record coverage for all `.bzl` files, excluding the built-ins, which don't have paths that
// could be resolved when a human-readable coverage report is generated.
Starlark.startCoverageCollection(
path -> !path.startsWith("/virtual_builtins_bzl") && path.endsWith(".bzl"));
}

BlazeCommandResult result =
createDetailedCommandResult(
Expand Down Expand Up @@ -603,6 +610,18 @@ private BlazeCommandResult execExclusively(
}
}
}
if (!commonOptions.starlarkCoverageReport.isEmpty()) {
FileOutputStream out;
try {
out = new FileOutputStream(commonOptions.starlarkCoverageReport);
} catch (IOException ex) {
String message = "Starlark coverage recorder: " + ex.getMessage();
outErr.printErrLn(message);
return createDetailedCommandResult(
message, FailureDetails.Command.Code.STARLARK_COVERAGE_REPORT_DUMP_FAILURE);
}
Starlark.dumpCoverage(new PrintWriter(out));
}

needToCallAfterCommand = false;
return runtime.afterCommand(env, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ public String getTypeDescription() {
help = "Writes into the specified file a pprof profile of CPU usage by all Starlark threads.")
public String starlarkCpuProfile;

@Option(
name = "starlark_coverage_report",
defaultValue = "",
documentationCategory = OptionDocumentationCategory.LOGGING,
effectTags = {OptionEffectTag.BAZEL_MONITORING},
help = "Writes into the specified file an lcov coverage report for all Starlark files.")
public String starlarkCoverageReport;

@Option(
name = "record_full_profiler_data",
defaultValue = "false",
Expand Down
1 change: 1 addition & 0 deletions src/main/protobuf/failure_details.proto
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ message Command {
NOT_IN_WORKSPACE = 12 [(metadata) = { exit_code: 2 }];
SPACES_IN_WORKSPACE_PATH = 13 [(metadata) = { exit_code: 36 }];
IN_OUTPUT_DIRECTORY = 14 [(metadata) = { exit_code: 2 }];
STARLARK_COVERAGE_REPORT_DUMP_FAILURE = 15 [(metadata) = { exit_code: 36 }];
}

Code code = 1;
Expand Down

0 comments on commit 4e8e8ed

Please sign in to comment.