Skip to content

Commit 2bc2287

Browse files
committed
fix: make coverage work with bootstrap=script
1 parent 188598a commit 2bc2287

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

python/private/py_executable.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,25 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
557557
site_init = ctx.actions.declare_file("{}/_bazel_site_init.py".format(site_packages))
558558
computed_subs = ctx.actions.template_dict()
559559
computed_subs.add_joined("%imports%", imports, join_with = ":", map_each = _map_each_identity)
560+
561+
if (ctx.configuration.coverage_enabled and
562+
runtime and
563+
runtime.coverage_tool):
564+
coverage_tool_runfiles_path = "{}/{}".format(
565+
ctx.workspace_name,
566+
runtime.coverage_tool.short_path,
567+
)
568+
else:
569+
coverage_tool_runfiles_path = ""
570+
560571
ctx.actions.expand_template(
561572
template = runtime.site_init_template,
562573
output = site_init,
563574
substitutions = {
564575
"%import_all%": "True" if ctx.fragments.bazel_py.python_import_all_repositories else "False",
565576
"%site_init_runfiles_path%": "{}/{}".format(ctx.workspace_name, site_init.short_path),
566577
"%workspace_name%": ctx.workspace_name,
578+
"%coverage_tool%": coverage_tool_runfiles_path,
567579
},
568580
computed_substitutions = computed_subs,
569581
)

python/private/site_init_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def _maybe_add_path(path):
185185
coverage_setup = True
186186
else:
187187
_print_verbose_coverage(
188-
"Coverage was enabled, but python coverage tool was not configured."
188+
"Coverage was enabled, but python coverage tool was not configured. "
189189
+ "To enable coverage, consult the docs at "
190190
+ "https://rules-python.readthedocs.io/en/latest/coverage.html"
191191
)

python/private/stage2_bootstrap_template.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def print_verbose(*args, mapping=None, values=None):
106106

107107
def print_verbose_coverage(*args):
108108
"""Print output if VERBOSE_COVERAGE is non-empty in the environment."""
109-
if os.environ.get("VERBOSE_COVERAGE"):
110-
print(*args, file=sys.stderr, flush=True)
109+
if is_verbose_coverage():
110+
print("bootstrap: coverage:", *args, file=sys.stderr, flush=True)
111111

112112

113113
def is_verbose_coverage():
@@ -398,6 +398,8 @@ def main():
398398
else:
399399
coverage_enabled = False
400400

401+
print_verbose_coverage("coverage_enabled:", coverage_enabled)
402+
401403
with _maybe_collect_coverage(enable=coverage_enabled):
402404
# The first arg is this bootstrap, so drop that for the re-invocation.
403405
_run_py(main_filename, args=sys.argv[1:])

0 commit comments

Comments
 (0)