@@ -234,16 +234,28 @@ def _wd_test_impl(ctx):
234234 if default_runfiles :
235235 runfiles = runfiles .merge (default_runfiles )
236236
237+ # Create runtime_objects_list.txt for LLVM coverage.
238+ # This file tells collect_cc_coverage.sh where to find the instrumented binary
239+ # so llvm-cov can export coverage data in LCOV format.
240+ runtime_objects_list = ctx .actions .declare_file (ctx .label .name + "runtime_objects_list.txt" )
241+ workerd_file = ctx .file .workerd
242+ ctx .actions .write (
243+ output = runtime_objects_list ,
244+ content = workerd_file .path + "\n " ,
245+ is_executable = False ,
246+ )
247+
237248 # IMPORTANT: The workerd binary must be listed in dependency_attributes
238249 # to ensure its transitive dependencies (all the C++ source files) are
239250 # included in the coverage instrumentation. Without this, coverage data
240251 # won't be collected for the actual workerd implementation code.
252+ # The runtime_objects_list.txt and workerd binary are included in metadata_files
253+ # so that collect_cc_coverage.sh can find them for LLVM LCOV generation.
241254 instrumented_files_info = coverage_common .instrumented_files_info (
242255 ctx ,
243256 source_attributes = ["src" , "data" ],
244257 dependency_attributes = ["workerd" , "sidecar" , "sidecar_supervisor" ],
245- # Include all file types that might contain testable code
246- extensions = ["cc" , "c++" , "cpp" , "cxx" , "c" , "h" , "hh" , "hpp" , "hxx" , "inc" , "js" , "ts" , "mjs" , "wd-test" , "capnp" ],
258+ metadata_files = [runtime_objects_list , workerd_file ],
247259 )
248260 environment = dict (ctx .attr .env )
249261 if ctx .attr .python_snapshot_test :
@@ -282,11 +294,14 @@ _wd_test = rule(
282294 ),
283295 # Source file
284296 "src" : attr .label (allow_single_file = True ),
285- # The workerd executable is used to run all tests
297+ # The workerd executable is used to run all tests.
298+ # Using cfg = "target" instead of "exec" ensures workerd is built with coverage
299+ # instrumentation when running `bazel coverage`. With cfg = "exec", the binary
300+ # would be built in exec configuration which doesn't include coverage flags.
286301 "workerd" : attr .label (
287302 allow_single_file = True ,
288303 executable = True ,
289- cfg = "exec " ,
304+ cfg = "target " ,
290305 default = "//src/workerd/server:workerd_cross" ,
291306 ),
292307 # A list of files that this test requires to be present in order to run.
0 commit comments