Skip to content

Commit cd33fc7

Browse files
committed
assign coverage_setup instead of mutable global; check coverage_dir env var
1 parent 4ae444d commit cd33fc7

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

python/private/site_init_template.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
# Runfiles-relative path to the coverage tool entry point, if any.
2828
_COVERAGE_TOOL = "%coverage_tool%"
2929

30-
# True if coverage was requested and successfully setup
31-
COVERAGE_SETUP = False
32-
3330

3431
def _is_verbose():
3532
return bool(os.environ.get("RULES_PYTHON_BOOTSTRAP_VERBOSE"))
@@ -128,7 +125,6 @@ def _search_path(name):
128125

129126

130127
def _setup_sys_path():
131-
global COVERAGE_SETUP
132128
seen = set(sys.path)
133129
python_path_entries = []
134130

@@ -161,6 +157,7 @@ def _maybe_add_path(path):
161157
# for something, though it could be another program executing this one or
162158
# one executed by this one (e.g. an extension module).
163159
# NOTE: Coverage is added last to allow user dependencies to override it.
160+
coverage_setup = False
164161
if os.environ.get("COVERAGE_DIR"):
165162
cov_tool = _COVERAGE_TOOL
166163
if cov_tool:
@@ -185,15 +182,15 @@ def _maybe_add_path(path):
185182
# the runfiles directory, which must not be replaced.
186183
# CoverageScript.do_execute() undoes this sys.path[0] setting.
187184
_maybe_add_path(coverage_dir)
188-
COVERAGE_SETUP = True
185+
coverage_setup = True
189186
else:
190187
_print_verbose_coverage(
191188
"Coverage was enabled, but python coverage tool was not configured."
192189
+ "To enable coverage, consult the docs at "
193190
+ "https://rules-python.readthedocs.io/en/latest/coverage.html"
194191
)
195192

196-
return None
193+
return coverage_setup
197194

198195

199-
_setup_sys_path()
196+
COVERAGE_SETUP = _setup_sys_path()

python/private/stage2_bootstrap_template.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,13 @@ def main():
391391
# user import accidentally triggering the site-packages logic above.
392392
sys.path[0:0] = prepend_path_entries
393393

394-
# todo: also check COVERAGE_DIR env var
395-
import _bazel_site_init
394+
if os.environ.get("COVERAGE_DIR"):
395+
import _bazel_site_init
396+
coverage_enabled = _bazel_site_init.COVERAGE_SETUP
397+
else:
398+
coverage_enabled = False
396399

397-
with _maybe_collect_coverage(enable=_bazel_site_init.COVERAGE_SETUP):
400+
with _maybe_collect_coverage(enable=coverage_enabled):
398401
# The first arg is this bootstrap, so drop that for the re-invocation.
399402
_run_py(main_filename, args=sys.argv[1:])
400403
sys.exit(0)

0 commit comments

Comments
 (0)