@@ -940,7 +940,7 @@ def py_executable_base_impl(ctx, *, semantics, is_test, inherited_environment =
940940
941941 imports = collect_imports (ctx , semantics )
942942
943- runtime_details = _get_runtime_details (ctx , semantics )
943+ runtime_details = _get_runtime_details (ctx , semantics , is_test )
944944 if ctx .configuration .coverage_enabled :
945945 extra_deps = semantics .get_coverage_deps (ctx , runtime_details )
946946 else :
@@ -1016,7 +1016,6 @@ def py_executable_base_impl(ctx, *, semantics, is_test, inherited_environment =
10161016 inherited_environment = inherited_environment ,
10171017 semantics = semantics ,
10181018 output_groups = exec_result .output_groups ,
1019- is_test = is_test ,
10201019 )
10211020
10221021def _get_build_info (ctx , cc_toolchain ):
@@ -1041,7 +1040,7 @@ def _declare_executable_file(ctx):
10411040
10421041 return executable
10431042
1044- def _get_runtime_details (ctx , semantics ):
1043+ def _get_runtime_details (ctx , semantics , is_test ):
10451044 """Gets various information about the Python runtime to use.
10461045
10471046 While most information comes from the toolchain, various legacy and
@@ -1050,6 +1049,7 @@ def _get_runtime_details(ctx, semantics):
10501049 Args:
10511050 ctx: Rule ctx
10521051 semantics: A `BinarySemantics` struct; see `create_binary_semantics_struct`
1052+ is_test: bool; True if the rule is a test rule (has `test=True`), False if not
10531053
10541054 Returns:
10551055 A struct; see inline-field comments of the return value for details.
@@ -1078,6 +1078,7 @@ def _get_runtime_details(ctx, semantics):
10781078 if not effective_runtime :
10791079 fail ("Unable to find Python runtime" )
10801080
1081+ extra_test_env = {}
10811082 if effective_runtime :
10821083 direct = [] # List of files
10831084 transitive = [] # List of depsets
@@ -1090,6 +1091,12 @@ def _get_runtime_details(ctx, semantics):
10901091 direct .append (effective_runtime .coverage_tool )
10911092 if effective_runtime .coverage_files :
10921093 transitive .append (effective_runtime .coverage_files )
1094+ if is_test :
1095+ py_test_toolchain = ctx .exec_groups ["test" ].toolchains [PY_TEST_TOOLCHAIN_TYPE ]
1096+ if py_test_toolchain :
1097+ coverage_rc = py_test_toolchain .py_test_info .coverage_rc
1098+ extra_test_env = {"COVERAGE_RC" : coverage_rc .files .to_list ()[0 ].short_path }
1099+ direct .extend (coverage_rc .files .to_list ())
10931100 runtime_files = depset (direct = direct , transitive = transitive )
10941101 else :
10951102 runtime_files = depset ()
@@ -1121,6 +1128,9 @@ def _get_runtime_details(ctx, semantics):
11211128 # be included. For in-build runtimes, this shold include the interpreter
11221129 # and any supporting files.
11231130 runfiles = ctx .runfiles (transitive_files = runtime_files ),
1131+ # extra_test_env: dict[str, str]; Additional environment variables to
1132+ # set when running the test.
1133+ extra_test_env = extra_test_env ,
11241134 )
11251135
11261136def _maybe_get_runtime_from_ctx (ctx ):
@@ -1582,8 +1592,7 @@ def _create_providers(
15821592 inherited_environment ,
15831593 runtime_details ,
15841594 output_groups ,
1585- semantics ,
1586- is_test ):
1595+ semantics ):
15871596 """Creates the providers an executable should return.
15881597
15891598 Args:
@@ -1613,37 +1622,26 @@ def _create_providers(
16131622 runtime_details: struct of runtime information; see _get_runtime_details()
16141623 output_groups: dict[str, depset[File]]; used to create OutputGroupInfo
16151624 semantics: BinarySemantics struct; see create_binary_semantics()
1616- is_test: bool; True if the rule is a test rule,
16171625
16181626 Returns:
16191627 A list of modern providers.
16201628 """
16211629
1622- default_runfiles = runfiles_details .default_runfiles
1623- extra_test_env = {}
1624-
1625- if is_test :
1626- py_test_toolchain = ctx .exec_groups ["test" ].toolchains [PY_TEST_TOOLCHAIN_TYPE ]
1627- if py_test_toolchain :
1628- coverage_rc = py_test_toolchain .py_test_info .coverage_rc
1629- extra_test_env = {"COVERAGE_RC" : coverage_rc .files .to_list ()[0 ].path }
1630- default_runfiles = default_runfiles .merge (ctx .runfiles (files = coverage_rc .files .to_list ()))
1631-
16321630 providers = [
16331631 DefaultInfo (
16341632 executable = executable ,
16351633 files = default_outputs ,
16361634 default_runfiles = _py_builtins .make_runfiles_respect_legacy_external_runfiles (
16371635 ctx ,
1638- default_runfiles ,
1636+ runfiles_details . default_runfiles ,
16391637 ),
16401638 data_runfiles = _py_builtins .make_runfiles_respect_legacy_external_runfiles (
16411639 ctx ,
16421640 runfiles_details .data_runfiles ,
16431641 ),
16441642 ),
16451643 create_instrumented_files_info (ctx ),
1646- _create_run_environment_info (ctx , inherited_environment , extra_test_env ),
1644+ _create_run_environment_info (ctx , inherited_environment , runtime_details . extra_test_env ),
16471645 PyExecutableInfo (
16481646 main = main_py ,
16491647 runfiles_without_exe = runfiles_details .runfiles_without_exe ,
0 commit comments