@@ -179,7 +179,7 @@ def py_executable_base_impl(ctx, *, semantics, is_test, inherited_environment =
179
179
180
180
imports = collect_imports (ctx , semantics )
181
181
182
- runtime_details = _get_runtime_details (ctx , semantics )
182
+ runtime_details = _get_runtime_details (ctx , semantics , is_test )
183
183
if ctx .configuration .coverage_enabled :
184
184
extra_deps = semantics .get_coverage_deps (ctx , runtime_details )
185
185
else :
@@ -254,7 +254,6 @@ def py_executable_base_impl(ctx, *, semantics, is_test, inherited_environment =
254
254
inherited_environment = inherited_environment ,
255
255
semantics = semantics ,
256
256
output_groups = exec_result .output_groups ,
257
- is_test = is_test ,
258
257
)
259
258
260
259
def _get_build_info (ctx , cc_toolchain ):
@@ -279,7 +278,7 @@ def _declare_executable_file(ctx):
279
278
280
279
return executable
281
280
282
- def _get_runtime_details (ctx , semantics ):
281
+ def _get_runtime_details (ctx , semantics , is_test ):
283
282
"""Gets various information about the Python runtime to use.
284
283
285
284
While most information comes from the toolchain, various legacy and
@@ -288,6 +287,7 @@ def _get_runtime_details(ctx, semantics):
288
287
Args:
289
288
ctx: Rule ctx
290
289
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
291
291
292
292
Returns:
293
293
A struct; see inline-field comments of the return value for details.
@@ -316,6 +316,7 @@ def _get_runtime_details(ctx, semantics):
316
316
if not effective_runtime :
317
317
fail ("Unable to find Python runtime" )
318
318
319
+ extra_test_env = {}
319
320
if effective_runtime :
320
321
direct = [] # List of files
321
322
transitive = [] # List of depsets
@@ -328,6 +329,12 @@ def _get_runtime_details(ctx, semantics):
328
329
direct .append (effective_runtime .coverage_tool )
329
330
if effective_runtime .coverage_files :
330
331
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 ())
331
338
runtime_files = depset (direct = direct , transitive = transitive )
332
339
else :
333
340
runtime_files = depset ()
@@ -359,6 +366,9 @@ def _get_runtime_details(ctx, semantics):
359
366
# be included. For in-build runtimes, this shold include the interpreter
360
367
# and any supporting files.
361
368
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 ,
362
372
)
363
373
364
374
def _maybe_get_runtime_from_ctx (ctx ):
@@ -819,8 +829,7 @@ def _create_providers(
819
829
inherited_environment ,
820
830
runtime_details ,
821
831
output_groups ,
822
- semantics ,
823
- is_test ):
832
+ semantics ):
824
833
"""Creates the providers an executable should return.
825
834
826
835
Args:
@@ -848,21 +857,12 @@ def _create_providers(
848
857
runtime_details: struct of runtime information; see _get_runtime_details()
849
858
output_groups: dict[str, depset[File]]; used to create OutputGroupInfo
850
859
semantics: BinarySemantics struct; see create_binary_semantics()
851
- is_test: bool; True if the rule is a test rule,
852
860
853
861
Returns:
854
862
A list of modern providers.
855
863
"""
856
864
857
865
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
866
867
867
providers = [
868
868
DefaultInfo (
@@ -878,7 +878,7 @@ def _create_providers(
878
878
),
879
879
),
880
880
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 ),
882
882
PyExecutableInfo (
883
883
main = main_py ,
884
884
runfiles_without_exe = runfiles_details .runfiles_without_exe ,
0 commit comments