Skip to content

Commit 024218e

Browse files
committed
better debugging
1 parent 2bc2287 commit 024218e

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

python/private/py_executable.bzl

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -557,25 +557,14 @@ 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-
571560
ctx.actions.expand_template(
572561
template = runtime.site_init_template,
573562
output = site_init,
574563
substitutions = {
564+
"%coverage_tool%": _get_coverage_tool_runfiles_path(ctx, runtime),
575565
"%import_all%": "True" if ctx.fragments.bazel_py.python_import_all_repositories else "False",
576566
"%site_init_runfiles_path%": "{}/{}".format(ctx.workspace_name, site_init.short_path),
577567
"%workspace_name%": ctx.workspace_name,
578-
"%coverage_tool%": coverage_tool_runfiles_path,
579568
},
580569
computed_substitutions = computed_subs,
581570
)
@@ -590,6 +579,17 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
590579
def _map_each_identity(v):
591580
return v
592581

582+
def _get_coverage_tool_runfiles_path(ctx, runtime):
583+
if (ctx.configuration.coverage_enabled and
584+
runtime and
585+
runtime.coverage_tool):
586+
return "{}/{}".format(
587+
ctx.workspace_name,
588+
runtime.coverage_tool.short_path,
589+
)
590+
else:
591+
return ""
592+
593593
def _create_stage2_bootstrap(
594594
ctx,
595595
*,
@@ -605,23 +605,14 @@ def _create_stage2_bootstrap(
605605
sibling = output_sibling,
606606
)
607607
runtime = runtime_details.effective_runtime
608-
if (ctx.configuration.coverage_enabled and
609-
runtime and
610-
runtime.coverage_tool):
611-
coverage_tool_runfiles_path = "{}/{}".format(
612-
ctx.workspace_name,
613-
runtime.coverage_tool.short_path,
614-
)
615-
else:
616-
coverage_tool_runfiles_path = ""
617608

618609
template = runtime.stage2_bootstrap_template
619610

620611
ctx.actions.expand_template(
621612
template = template,
622613
output = output,
623614
substitutions = {
624-
"%coverage_tool%": coverage_tool_runfiles_path,
615+
"%coverage_tool%": _get_coverage_tool_runfiles_path(ctx, runtime),
625616
"%import_all%": "True" if ctx.fragments.bazel_py.python_import_all_repositories else "False",
626617
"%imports%": ":".join(imports.to_list()),
627618
"%main%": "{}/{}".format(ctx.workspace_name, main_py.short_path),

python/private/site_init_template.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def _maybe_add_path(path):
163163
if cov_tool:
164164
_print_verbose_coverage(f"Using toolchain coverage_tool {cov_tool}")
165165
elif cov_tool := os.environ.get("PYTHON_COVERAGE"):
166-
_print_verbose_coverage(f"PYTHON_COVERAGE: {cov_tool}")
166+
_print_verbose_coverage(f"Using env var coverage: PYTHON_COVERAGE={cov_tool}")
167167

168168
if cov_tool:
169169
if os.path.isabs(cov_tool):
@@ -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 the coverage tool was not found or valid. "
189189
+ "To enable coverage, consult the docs at "
190190
+ "https://rules-python.readthedocs.io/en/latest/coverage.html"
191191
)
@@ -194,3 +194,4 @@ def _maybe_add_path(path):
194194

195195

196196
COVERAGE_SETUP = _setup_sys_path()
197+
_print_verbose("DONE")

python/private/stage2_bootstrap_template.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def print_verbose(*args, mapping=None, values=None):
107107
def print_verbose_coverage(*args):
108108
"""Print output if VERBOSE_COVERAGE is non-empty in the environment."""
109109
if is_verbose_coverage():
110-
print("bootstrap: coverage:", *args, file=sys.stderr, flush=True)
110+
print("bootstrap: stage 2: coverage:", *args, file=sys.stderr, flush=True)
111111

112112

113113
def is_verbose_coverage():
@@ -271,6 +271,7 @@ def _run_py(main_filename, *, args, cwd=None):
271271

272272
@contextlib.contextmanager
273273
def _maybe_collect_coverage(enable):
274+
print_verbose_coverage("enabled:", enable)
274275
if not enable:
275276
yield
276277
return
@@ -283,7 +284,9 @@ def _maybe_collect_coverage(enable):
283284
unique_id = uuid.uuid4()
284285

285286
# We need for coveragepy to use relative paths. This can only be configured
287+
# using an rc file.
286288
rcfile_name = os.path.join(coverage_dir, ".coveragerc_{}".format(unique_id))
289+
print_verbose_coverage("coveragerc file:", rcfile_name)
287290
with open(rcfile_name, "w") as rcfile:
288291
rcfile.write(
289292
"""[run]
@@ -318,6 +321,7 @@ def _maybe_collect_coverage(enable):
318321
finally:
319322
cov.stop()
320323
lcov_path = os.path.join(coverage_dir, "pylcov.dat")
324+
print_verbose_coverage("generating lcov from:", lcov_path)
321325
cov.lcov_report(
322326
outfile=lcov_path,
323327
# Ignore errors because sometimes instrumented files aren't
@@ -398,8 +402,6 @@ def main():
398402
else:
399403
coverage_enabled = False
400404

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

0 commit comments

Comments
 (0)