Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.3.0] Adds a flag to enable coverage for generated source files #18664

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,11 @@ public boolean shouldInstrumentTestTargets() {
return options.instrumentTestTargets;
}

/** Returns a boolean of whether to collect code coverage for generated files or not. */
public boolean shouldCollectCodeCoverageForGeneratedFiles() {
return options.collectCodeCoverageForGeneratedFiles;
}

/**
* Returns a new, unordered mapping of names to values of "Make" variables defined by this
* configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ public ExecConfigurationDistinguisherSchemeConverter() {
+ " not be specified directly - 'bazel coverage' command should be used instead.")
public boolean collectCodeCoverage;

@Option(
name = "experimental_collect_code_coverage_for_generated_files",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS},
help =
"If specified, Bazel will also generate collect coverage information for generated"
+ " files.")
public boolean collectCodeCoverageForGeneratedFiles;

@Option(
name = "build_runfile_manifests",
defaultValue = "true",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ public static InstrumentedFilesInfo collect(
for (TransitiveInfoCollection dep :
getPrerequisitesForAttributes(ruleContext, spec.sourceAttributes)) {
for (Artifact artifact : dep.getProvider(FileProvider.class).getFilesToBuild().toList()) {
if (artifact.isSourceArtifact() &&
spec.instrumentedFileTypes.matches(artifact.getFilename())) {
if (shouldIncludeArtifact(ruleContext.getConfiguration(), artifact)
&& spec.instrumentedFileTypes.matches(artifact.getFilename())) {
localSourcesBuilder.add(artifact);
}
}
Expand Down Expand Up @@ -262,6 +262,14 @@ public static boolean shouldIncludeLocalSources(
&& config.getInstrumentationFilter().isIncluded(label.toString()));
}

/**
* Return whether the artifact should be collected based on the origin of the artifact and the
* --experimental_collect_code_coverage_for_generated_files config setting.
*/
public static boolean shouldIncludeArtifact(BuildConfigurationValue config, Artifact artifact) {
return artifact.isSourceArtifact() || config.shouldCollectCodeCoverageForGeneratedFiles();
}

/**
* The set of file types and attributes to visit to collect instrumented files for a certain rule
* type. The class is intentionally immutable, so that a single instance is sufficient for all
Expand Down