Skip to content

Commit

Permalink
Move toolchain logic into get runtime details
Browse files Browse the repository at this point in the history
  • Loading branch information
ewianda committed Nov 5, 2024
1 parent 3060b83 commit de0204d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
30 changes: 15 additions & 15 deletions python/private/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def py_executable_base_impl(ctx, *, semantics, is_test, inherited_environment =

imports = collect_imports(ctx, semantics)

runtime_details = _get_runtime_details(ctx, semantics)
runtime_details = _get_runtime_details(ctx, semantics, is_test)
if ctx.configuration.coverage_enabled:
extra_deps = semantics.get_coverage_deps(ctx, runtime_details)
else:
Expand Down Expand Up @@ -254,7 +254,6 @@ def py_executable_base_impl(ctx, *, semantics, is_test, inherited_environment =
inherited_environment = inherited_environment,
semantics = semantics,
output_groups = exec_result.output_groups,
is_test = is_test,
)

def _get_build_info(ctx, cc_toolchain):
Expand All @@ -279,7 +278,7 @@ def _declare_executable_file(ctx):

return executable

def _get_runtime_details(ctx, semantics):
def _get_runtime_details(ctx, semantics, is_test):
"""Gets various information about the Python runtime to use.
While most information comes from the toolchain, various legacy and
Expand All @@ -288,6 +287,7 @@ def _get_runtime_details(ctx, semantics):
Args:
ctx: Rule ctx
semantics: A `BinarySemantics` struct; see `create_binary_semantics_struct`
is_test: bool; True if the rule is a test rule (has `test=True`), False if not
Returns:
A struct; see inline-field comments of the return value for details.
Expand Down Expand Up @@ -316,6 +316,7 @@ def _get_runtime_details(ctx, semantics):
if not effective_runtime:
fail("Unable to find Python runtime")

extra_test_env = {}
if effective_runtime:
direct = [] # List of files
transitive = [] # List of depsets
Expand All @@ -328,6 +329,12 @@ def _get_runtime_details(ctx, semantics):
direct.append(effective_runtime.coverage_tool)
if effective_runtime.coverage_files:
transitive.append(effective_runtime.coverage_files)
if is_test:
py_test_toolchain = ctx.exec_groups["test"].toolchains[PY_TEST_TOOLCHAIN_TYPE]
if py_test_toolchain:
coverage_rc = py_test_toolchain.py_test_info.coverage_rc
extra_test_env = {"COVERAGE_RC": coverage_rc.files.to_list()[0].short_path}
direct.extend(coverage_rc.files.to_list())
runtime_files = depset(direct = direct, transitive = transitive)
else:
runtime_files = depset()
Expand Down Expand Up @@ -359,6 +366,9 @@ def _get_runtime_details(ctx, semantics):
# be included. For in-build runtimes, this shold include the interpreter
# and any supporting files.
runfiles = ctx.runfiles(transitive_files = runtime_files),
# extra_test_env: dict[str, str]; Additional environment variables to
# set when running the test.
extra_test_env = extra_test_env,
)

def _maybe_get_runtime_from_ctx(ctx):
Expand Down Expand Up @@ -819,8 +829,7 @@ def _create_providers(
inherited_environment,
runtime_details,
output_groups,
semantics,
is_test):
semantics):
"""Creates the providers an executable should return.
Args:
Expand Down Expand Up @@ -848,21 +857,12 @@ def _create_providers(
runtime_details: struct of runtime information; see _get_runtime_details()
output_groups: dict[str, depset[File]]; used to create OutputGroupInfo
semantics: BinarySemantics struct; see create_binary_semantics()
is_test: bool; True if the rule is a test rule,
Returns:
A list of modern providers.
"""

default_runfiles = runfiles_details.default_runfiles
extra_test_env = {}

if is_test:
py_test_toolchain = ctx.exec_groups["test"].toolchains[PY_TEST_TOOLCHAIN_TYPE]
if py_test_toolchain:
coverage_rc = py_test_toolchain.py_test_info.coverage_rc
extra_test_env = {"COVERAGE_RC": coverage_rc.files.to_list()[0].path}
default_runfiles = default_runfiles.merge(ctx.runfiles(files = coverage_rc.files.to_list()))

providers = [
DefaultInfo(
Expand All @@ -878,7 +878,7 @@ def _create_providers(
),
),
create_instrumented_files_info(ctx),
_create_run_environment_info(ctx, inherited_environment, extra_test_env),
_create_run_environment_info(ctx, inherited_environment, runtime_details.extra_test_env),
PyExecutableInfo(
main = main_py,
runfiles_without_exe = runfiles_details.runfiles_without_exe,
Expand Down
2 changes: 1 addition & 1 deletion python/private/stage2_bootstrap_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def _maybe_collect_coverage(enable):

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

0 comments on commit de0204d

Please sign in to comment.