Skip to content

Commit 092ef1c

Browse files
iancha1992Rasrack
andauthored
Adds a flag to enable coverage for generated source files (#18664)
Closes #11350. RELNOTES: Add flag --experimental_collect_code_coverage_for_generated_files. PiperOrigin-RevId: 539648731 Change-Id: I352de7a74c522db6fbe5e10a21268914d1e39d58 Co-authored-by: Rasrack <[email protected]>
1 parent eafdf2e commit 092ef1c

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,11 @@ public boolean shouldInstrumentTestTargets() {
536536
return options.instrumentTestTargets;
537537
}
538538

539+
/** Returns a boolean of whether to collect code coverage for generated files or not. */
540+
public boolean shouldCollectCodeCoverageForGeneratedFiles() {
541+
return options.collectCodeCoverageForGeneratedFiles;
542+
}
543+
539544
/**
540545
* Returns a new, unordered mapping of names to values of "Make" variables defined by this
541546
* configuration.

src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,16 @@ public ExecConfigurationDistinguisherSchemeConverter() {
425425
+ " not be specified directly - 'bazel coverage' command should be used instead.")
426426
public boolean collectCodeCoverage;
427427

428+
@Option(
429+
name = "experimental_collect_code_coverage_for_generated_files",
430+
defaultValue = "false",
431+
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
432+
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS},
433+
help =
434+
"If specified, Bazel will also generate collect coverage information for generated"
435+
+ " files.")
436+
public boolean collectCodeCoverageForGeneratedFiles;
437+
428438
@Option(
429439
name = "build_runfile_manifests",
430440
defaultValue = "true",

src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ public static InstrumentedFilesInfo collect(
213213
for (TransitiveInfoCollection dep :
214214
getPrerequisitesForAttributes(ruleContext, spec.sourceAttributes)) {
215215
for (Artifact artifact : dep.getProvider(FileProvider.class).getFilesToBuild().toList()) {
216-
if (artifact.isSourceArtifact() &&
217-
spec.instrumentedFileTypes.matches(artifact.getFilename())) {
216+
if (shouldIncludeArtifact(ruleContext.getConfiguration(), artifact)
217+
&& spec.instrumentedFileTypes.matches(artifact.getFilename())) {
218218
localSourcesBuilder.add(artifact);
219219
}
220220
}
@@ -262,6 +262,14 @@ public static boolean shouldIncludeLocalSources(
262262
&& config.getInstrumentationFilter().isIncluded(label.toString()));
263263
}
264264

265+
/**
266+
* Return whether the artifact should be collected based on the origin of the artifact and the
267+
* --experimental_collect_code_coverage_for_generated_files config setting.
268+
*/
269+
public static boolean shouldIncludeArtifact(BuildConfigurationValue config, Artifact artifact) {
270+
return artifact.isSourceArtifact() || config.shouldCollectCodeCoverageForGeneratedFiles();
271+
}
272+
265273
/**
266274
* The set of file types and attributes to visit to collect instrumented files for a certain rule
267275
* type. The class is intentionally immutable, so that a single instance is sufficient for all

0 commit comments

Comments
 (0)