From 131205b0cfeef0af6c3d51a823c6348ba1b81ce9 Mon Sep 17 00:00:00 2001 From: Philipp Stephani Date: Mon, 18 Aug 2025 09:42:02 +0200 Subject: [PATCH 1/3] fix(coverage): Disable certain coverage warnings. These warnings appear if there are no Python source files in the instrumented directories, cf. https://github.com/bazel-contrib/rules_python/issues/2762. Work towards #2762 --- python/private/py_executable.bzl | 1 + python/private/stage2_bootstrap_template.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index ea00eed17b..4b770fead3 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -670,6 +670,7 @@ def _create_stage2_bootstrap( "%venv_rel_site_packages%": venv.venv_site_packages, "%venv_root%": venv.venv_root, "%workspace_name%": ctx.workspace_name, + "%coverage_instrumented%": str(int(ctx.configuration.coverage_enabled and ctx.coverage_instrumented())), }, is_executable = True, ) diff --git a/python/private/stage2_bootstrap_template.py b/python/private/stage2_bootstrap_template.py index 4d98b03846..e3e303b3b1 100644 --- a/python/private/stage2_bootstrap_template.py +++ b/python/private/stage2_bootstrap_template.py @@ -41,6 +41,9 @@ # string otherwise. VENV_SITE_PACKAGES = "%venv_rel_site_packages%" +# Whether we should generate coverage data. +COVERAGE_INSTRUMENTED = "%coverage_instrumented%" == "1" + # ===== Template substitutions end ===== @@ -319,11 +322,14 @@ def _maybe_collect_coverage(enable): # We need for coveragepy to use relative paths. This can only be configured # using an rc file. rcfile_name = os.path.join(coverage_dir, ".coveragerc_{}".format(unique_id)) + disable_warnings = ('disable_warnings = module-not-imported, no-data-collected' + if COVERAGE_INSTRUMENTED else '') print_verbose_coverage("coveragerc file:", rcfile_name) with open(rcfile_name, "w") as rcfile: rcfile.write( f"""[run] relative_files = True +{disable_warnings} source = \t{source} """ From 71e2a673a19b27a20872adf2c93eb50d2ddb4d15 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 28 Dec 2025 13:14:36 +0900 Subject: [PATCH 2/3] buildifier --- python/private/py_executable.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index 4b770fead3..b761017045 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -661,6 +661,7 @@ def _create_stage2_bootstrap( template = template, output = output, substitutions = { + "%coverage_instrumented%": str(int(ctx.configuration.coverage_enabled and ctx.coverage_instrumented())), "%coverage_tool%": _get_coverage_tool_runfiles_path(ctx, runtime), "%import_all%": "True" if read_possibly_native_flag(ctx, "python_import_all_repositories") else "False", "%imports%": ":".join(imports.to_list()), @@ -670,7 +671,6 @@ def _create_stage2_bootstrap( "%venv_rel_site_packages%": venv.venv_site_packages, "%venv_root%": venv.venv_root, "%workspace_name%": ctx.workspace_name, - "%coverage_instrumented%": str(int(ctx.configuration.coverage_enabled and ctx.coverage_instrumented())), }, is_executable = True, ) From d223f079940f2dc1b50830f7b6590d22444e378e Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 28 Dec 2025 13:16:52 +0900 Subject: [PATCH 3/3] add a changelog entry --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e92b3c2737..6b858c7d89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,7 +62,8 @@ END_UNRELEASED_TEMPLATE {#v0-0-0-fixed} ### Fixed -* Nothing fixed. +* (tests) No more coverage warnings are being printed if there are no sources. + ([#2762](https://github.com/bazel-contrib/rules_python/issues/2762)) {#v0-0-0-added} ### Added