Skip to content

bug: Coverage Report Shows no valid records found with bootstrap_impl=script #2572

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

Closed
ewianda opened this issue Jan 22, 2025 · 4 comments · Fixed by #2574
Closed

bug: Coverage Report Shows no valid records found with bootstrap_impl=script #2572

ewianda opened this issue Jan 22, 2025 · 4 comments · Fixed by #2574

Comments

@ewianda
Copy link
Contributor

ewianda commented Jan 22, 2025

🐞 bug report

Affected Rule

py_test

Is this a regression?

Yes, bug was introduced in #2409

Description

No coverage report is generated for --@rules_python//python/config_settings:bootstrap_impl=script

🔬 Minimal Reproduction

Steps to reproduce the issue:

  1. Clone the repository:

    git clone git@github.com:bazelbuild/rules_python.git
    cd rules_python/examples/bzlmod
  2. Run the following command to clean and generate a coverage report:

    bazelisk clean && \
    bazelisk coverage //tests:version_default_test --combined_report=lcov \
        --@rules_python//python/config_settings:bootstrap_impl=script \
        --cache_test_results=no --instrument_test_targets && \
    lcov --list bazel-out/_coverage/_coverage_report.dat
  3. Error Observed:

    lcov: ERROR: no valid records found in tracefile bazel-out/_coverage/_coverage_report.dat
    (use "lcov --ignore-errors empty ..." to bypass this error)
  4. Checkout a specific commit regression:

    git checkout 4a55ef4c2363f7ffc1ec9ba511fba7b2547ccefc
    bazelisk clean && \
    bazelisk coverage //tests:version_default_test --combined_report=lcov \
        --@rules_python//python/config_settings:bootstrap_impl=script \
        --cache_test_results=no --instrument_test_targets && \
    lcov --list bazel-out/_coverage/_coverage_report.dat
  5. Output Observed:

                        |Lines       |Functions  |Branches
    Filename            |Rate     Num|Rate    Num|Rate     Num
    ============================================================
    [tests/]
    version_test.py     | 0.0%      5|    -     0|    -      0
    ============================================================
                    Total:| 0.0%      5|    -     0|    -      0
    

🔥 Exception or Error

When running the initial command, the following error is observed:

lcov: ERROR: no valid records found in tracefile bazel-out/_coverage/_coverage_report.dat
(use "lcov --ignore-errors empty ..." to bypass this error)
@ewianda ewianda changed the title bug: Coverage Report Shows Invalid Records with bootstrap_impl=script bug: Coverage Report Shows no valid records found with bootstrap_impl=script Jan 22, 2025
@rickeylev
Copy link
Collaborator

Thanks for the report! I think there's two issues.

First, py_executable.bzl isn't expanding the %coverage_tool% string in the site init hook. That's easy to fix. (wip)

However, after fixing that, the coverage report is still empty. Not sure why that would be.

No idea to test this, either. Only thing I can think of is an integration test where a shell script checks the lcov file.

@ewianda
Copy link
Contributor Author

ewianda commented Jan 22, 2025

Just checked out main...rickeylev:rules_python:fix.bootstrap.coverage and it seems to be working

@ewianda
Copy link
Contributor Author

ewianda commented Jan 22, 2025

~/projects/rules_python/examples/bzlmod (main *=)
$ git diff
diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl
index da7127e0..6330f1a2 100644
--- a/python/private/py_executable.bzl
+++ b/python/private/py_executable.bzl
@@ -557,6 +557,17 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
     site_init = ctx.actions.declare_file("{}/_bazel_site_init.py".format(site_packages))
     computed_subs = ctx.actions.template_dict()
     computed_subs.add_joined("%imports%", imports, join_with = ":", map_each = _map_each_identity)
+
+    if (ctx.configuration.coverage_enabled and
+        runtime and
+        runtime.coverage_tool):
+        coverage_tool_runfiles_path = "{}/{}".format(
+            ctx.workspace_name,
+            runtime.coverage_tool.short_path,
+        )
+    else:
+        coverage_tool_runfiles_path = ""
+
     ctx.actions.expand_template(
         template = runtime.site_init_template,
         output = site_init,
@@ -564,6 +575,7 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
             "%import_all%": "True" if ctx.fragments.bazel_py.python_import_all_repositories else "False",
             "%site_init_runfiles_path%": "{}/{}".format(ctx.workspace_name, site_init.short_path),
             "%workspace_name%": ctx.workspace_name,
+            "%coverage_tool%": coverage_tool_runfiles_path,
         },
         computed_substitutions = computed_subs,
     )
~/projects/rules_python/examples/bzlmod (main *=)
$ bazelisk clean && bazelisk  coverage //tests:version_default_test --combined_report=lcov  --@rules_python//python/config_settings:bootstrap_impl=script   --cache_test_results=no  --instrument_test_targets && lcov --list bazel-out/_coverage/_coverage_report.dat


INFO: Using default value for --instrumentation_filter: "^//tests[/:]".
INFO: Override the above default with --instrumentation_filter
INFO: Analyzed target //tests:version_default_test (141 packages loaded, 5130 targets configured).
INFO: LCOV coverage report is located at /home/ewianda/.cache/bazel/_bazel_ewianda/229007d1af36718f54c73697ae3aff83/execroot/_main/bazel-out/_coverage/_coverage_report.dat
 and execpath is bazel-out/_coverage/_coverage_report.dat
INFO: From Coverage report generation:
Jan 22, 2025 12:52:47 AM com.google.devtools.coverageoutputgenerator.Main getTracefiles
INFO: Found 1 tracefiles.
Jan 22, 2025 12:52:47 AM com.google.devtools.coverageoutputgenerator.Main parseFilesSequentially
INFO: Parsing file bazel-out/k8-fastbuild/testlogs/tests/version_default_test/coverage.dat
Jan 22, 2025 12:52:47 AM com.google.devtools.coverageoutputgenerator.Main getGcovInfoFiles
INFO: No gcov info file found.
Jan 22, 2025 12:52:47 AM com.google.devtools.coverageoutputgenerator.Main getGcovJsonInfoFiles                                                                                                                                                                                                                            INFO: No gcov json file found.
Jan 22, 2025 12:52:47 AM com.google.devtools.coverageoutputgenerator.Main getProfdataFileOrNull
INFO: No .profdata file found.
INFO: Found 1 test target...
Target //tests:version_default_test up-to-date:
  bazel-bin/tests/version_default_test
INFO: Elapsed time: 6.438s, Critical Path: 2.69s
INFO: 22 processes: 16 internal, 5 linux-sandbox, 1 worker.
INFO: Build completed successfully, 22 total actions
//tests:version_default_test                                             PASSED in 0.5s
  /home/ewianda/.cache/bazel/_bazel_ewianda/229007d1af36718f54c73697ae3aff83/execroot/_main/bazel-out/k8-fastbuild/testlogs/tests/version_default_test/coverage.dat                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 Executed 1 out of 1 test: 1 test passes.
There were tests whose specified size is too big. Use the --test_verbose_timeout_warnings command line option to see which ones these are.
                      |Lines       |Functions  |Branches
Filename              |Rate     Num|Rate    Num|Rate     Num
============================================================
[tests/]
version_test.py       | 0.0%      5|    -     0|    -      0
============================================================
                Total:| 0.0%      5|    -     0|    -      0

@rickeylev
Copy link
Collaborator

Thanks for double checking! I forgot --instrument_test_targets. And I was setting some debug vars that might have interfered. Yes, it looks to work!

github-merge-queue bot pushed a commit that referenced this issue Jan 23, 2025
The script based bootstrap wasn't expanding the coverage template
variable, which prevented
coverage from activating. This was introduced when it was switched to
the venv layout.

To fix, expand the `%coverage_tool%` template variable as done
elsewhere.

Tested manually, per repro instructions in #2572. While I did devise a
way to mostly
test this without an integration test, it was thwarted by some other
bugs.

Along the way, improve some of the bootstrap debug output and fix a
comment.

Fixes #2572
ewianda pushed a commit to ewianda/rules_python that referenced this issue Jan 25, 2025
The script based bootstrap wasn't expanding the coverage template
variable, which prevented
coverage from activating. This was introduced when it was switched to
the venv layout.

To fix, expand the `%coverage_tool%` template variable as done
elsewhere.

Tested manually, per repro instructions in bazel-contrib#2572. While I did devise a
way to mostly
test this without an integration test, it was thwarted by some other
bugs.

Along the way, improve some of the bootstrap debug output and fix a
comment.

Fixes bazel-contrib#2572
ewianda pushed a commit to ewianda/rules_python that referenced this issue Jan 25, 2025
The script based bootstrap wasn't expanding the coverage template
variable, which prevented
coverage from activating. This was introduced when it was switched to
the venv layout.

To fix, expand the `%coverage_tool%` template variable as done
elsewhere.

Tested manually, per repro instructions in bazel-contrib#2572. While I did devise a
way to mostly
test this without an integration test, it was thwarted by some other
bugs.

Along the way, improve some of the bootstrap debug output and fix a
comment.

Fixes bazel-contrib#2572
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants