From 092ef1c5f8c58368b3e83c66218ba8dbc00a2c56 Mon Sep 17 00:00:00 2001 From: "Ian (Hee) Cha" Date: Tue, 20 Jun 2023 11:16:22 -0700 Subject: [PATCH] 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 --- .../lib/analysis/config/BuildConfigurationValue.java | 5 +++++ .../build/lib/analysis/config/CoreOptions.java | 10 ++++++++++ .../analysis/test/InstrumentedFilesCollector.java | 12 ++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java index a39d84f01f27f1..16b508a4bbde74 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java @@ -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. diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java b/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java index 4e175abf8c6efb..6ba9cae6f71f55 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java @@ -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", diff --git a/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java b/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java index 7dca3df13a8663..8a7dac95af91be 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java @@ -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); } } @@ -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