@@ -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
260259def _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
364374def _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,37 +857,26 @@ 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
857- 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 ()))
866-
867865 providers = [
868866 DefaultInfo (
869867 executable = executable ,
870868 files = default_outputs ,
871869 default_runfiles = _py_builtins .make_runfiles_respect_legacy_external_runfiles (
872870 ctx ,
873- default_runfiles ,
871+ runfiles_details . default_runfiles ,
874872 ),
875873 data_runfiles = _py_builtins .make_runfiles_respect_legacy_external_runfiles (
876874 ctx ,
877875 runfiles_details .data_runfiles ,
878876 ),
879877 ),
880878 create_instrumented_files_info (ctx ),
881- _create_run_environment_info (ctx , inherited_environment , extra_test_env ),
879+ _create_run_environment_info (ctx , inherited_environment , runtime_details . extra_test_env ),
882880 PyExecutableInfo (
883881 main = main_py ,
884882 runfiles_without_exe = runfiles_details .runfiles_without_exe ,
0 commit comments