Skip to content

Commit de0204d

Browse files
committed
Move toolchain logic into get runtime details
1 parent 3060b83 commit de0204d

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

python/private/py_executable.bzl

+15-15
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def py_executable_base_impl(ctx, *, semantics, is_test, inherited_environment =
179179

180180
imports = collect_imports(ctx, semantics)
181181

182-
runtime_details = _get_runtime_details(ctx, semantics)
182+
runtime_details = _get_runtime_details(ctx, semantics, is_test)
183183
if ctx.configuration.coverage_enabled:
184184
extra_deps = semantics.get_coverage_deps(ctx, runtime_details)
185185
else:
@@ -254,7 +254,6 @@ def py_executable_base_impl(ctx, *, semantics, is_test, inherited_environment =
254254
inherited_environment = inherited_environment,
255255
semantics = semantics,
256256
output_groups = exec_result.output_groups,
257-
is_test = is_test,
258257
)
259258

260259
def _get_build_info(ctx, cc_toolchain):
@@ -279,7 +278,7 @@ def _declare_executable_file(ctx):
279278

280279
return executable
281280

282-
def _get_runtime_details(ctx, semantics):
281+
def _get_runtime_details(ctx, semantics, is_test):
283282
"""Gets various information about the Python runtime to use.
284283
285284
While most information comes from the toolchain, various legacy and
@@ -288,6 +287,7 @@ def _get_runtime_details(ctx, semantics):
288287
Args:
289288
ctx: Rule ctx
290289
semantics: A `BinarySemantics` struct; see `create_binary_semantics_struct`
290+
is_test: bool; True if the rule is a test rule (has `test=True`), False if not
291291
292292
Returns:
293293
A struct; see inline-field comments of the return value for details.
@@ -316,6 +316,7 @@ def _get_runtime_details(ctx, semantics):
316316
if not effective_runtime:
317317
fail("Unable to find Python runtime")
318318

319+
extra_test_env = {}
319320
if effective_runtime:
320321
direct = [] # List of files
321322
transitive = [] # List of depsets
@@ -328,6 +329,12 @@ def _get_runtime_details(ctx, semantics):
328329
direct.append(effective_runtime.coverage_tool)
329330
if effective_runtime.coverage_files:
330331
transitive.append(effective_runtime.coverage_files)
332+
if is_test:
333+
py_test_toolchain = ctx.exec_groups["test"].toolchains[PY_TEST_TOOLCHAIN_TYPE]
334+
if py_test_toolchain:
335+
coverage_rc = py_test_toolchain.py_test_info.coverage_rc
336+
extra_test_env = {"COVERAGE_RC": coverage_rc.files.to_list()[0].short_path}
337+
direct.extend(coverage_rc.files.to_list())
331338
runtime_files = depset(direct = direct, transitive = transitive)
332339
else:
333340
runtime_files = depset()
@@ -359,6 +366,9 @@ def _get_runtime_details(ctx, semantics):
359366
# be included. For in-build runtimes, this shold include the interpreter
360367
# and any supporting files.
361368
runfiles = ctx.runfiles(transitive_files = runtime_files),
369+
# extra_test_env: dict[str, str]; Additional environment variables to
370+
# set when running the test.
371+
extra_test_env = extra_test_env,
362372
)
363373

364374
def _maybe_get_runtime_from_ctx(ctx):
@@ -819,8 +829,7 @@ def _create_providers(
819829
inherited_environment,
820830
runtime_details,
821831
output_groups,
822-
semantics,
823-
is_test):
832+
semantics):
824833
"""Creates the providers an executable should return.
825834
826835
Args:
@@ -848,21 +857,12 @@ def _create_providers(
848857
runtime_details: struct of runtime information; see _get_runtime_details()
849858
output_groups: dict[str, depset[File]]; used to create OutputGroupInfo
850859
semantics: BinarySemantics struct; see create_binary_semantics()
851-
is_test: bool; True if the rule is a test rule,
852860
853861
Returns:
854862
A list of modern providers.
855863
"""
856864

857865
default_runfiles = runfiles_details.default_runfiles
858-
extra_test_env = {}
859-
860-
if is_test:
861-
py_test_toolchain = ctx.exec_groups["test"].toolchains[PY_TEST_TOOLCHAIN_TYPE]
862-
if py_test_toolchain:
863-
coverage_rc = py_test_toolchain.py_test_info.coverage_rc
864-
extra_test_env = {"COVERAGE_RC": coverage_rc.files.to_list()[0].path}
865-
default_runfiles = default_runfiles.merge(ctx.runfiles(files = coverage_rc.files.to_list()))
866866

867867
providers = [
868868
DefaultInfo(
@@ -878,7 +878,7 @@ def _create_providers(
878878
),
879879
),
880880
create_instrumented_files_info(ctx),
881-
_create_run_environment_info(ctx, inherited_environment, extra_test_env),
881+
_create_run_environment_info(ctx, inherited_environment, runtime_details.extra_test_env),
882882
PyExecutableInfo(
883883
main = main_py,
884884
runfiles_without_exe = runfiles_details.runfiles_without_exe,

python/private/stage2_bootstrap_template.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def _maybe_collect_coverage(enable):
346346

347347
# We need for coveragepy to use relative paths. This can only be configured
348348
if os.environ.get("COVERAGE_RC"):
349-
rcfile_name = os.path.abspath(os.environ["COVERAGE_RC"])
349+
rcfile_name = (os.environ["COVERAGE_RC"])
350350
assert (
351351
os.path.exists(rcfile_name) == True
352352
), f"Coverage rc {rcfile_name} file does not exist"

0 commit comments

Comments
 (0)