From e3a65fd3d0babbe57a69ec551bc1dbdedacc6ce3 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Mon, 31 Oct 2022 11:50:57 -0700 Subject: [PATCH] Make PyRuntimeConfiguredTargetTest pass with Starlark py_runtime impl. Various fixes from the rushed initial implementation * ctx.attr.files is a list of targets, not a single target * interpreter_path defaults to an empty string when not specified, while it must be None for PyRuntimeInfo to accept the non-hermetic case. * The files arg must be None for the non-hermetic case (an empty depset is not accepted by PyRuntimeInfo). * Missing the `.py` part when access the fragment to get the default Python version. * Default `python_version` to `_INTERNAL_SENTINEL` to match existing behavior (otherwise it defaults to empty string) Work towards #15897 PiperOrigin-RevId: 485109694 Change-Id: Icfbbf606acb8074c12e7dba9b3ec39e6ac524bc5 --- .../common/python/py_runtime_rule.bzl | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/starlark/builtins_bzl/common/python/py_runtime_rule.bzl b/src/main/starlark/builtins_bzl/common/python/py_runtime_rule.bzl index 19aff9059d6f98..77c94586e08a53 100644 --- a/src/main/starlark/builtins_bzl/common/python/py_runtime_rule.bzl +++ b/src/main/starlark/builtins_bzl/common/python/py_runtime_rule.bzl @@ -18,13 +18,17 @@ load(":common/paths.bzl", "paths") _PyRuntimeInfo = _builtins.toplevel.PyRuntimeInfo def _py_runtime_impl(ctx): - interpreter_path = ctx.attr.interpreter_path + interpreter_path = ctx.attr.interpreter_path or None # Convert empty string to None interpreter = ctx.file.interpreter if (interpreter_path and interpreter) or (not interpreter_path and not interpreter): fail("exactly one of the 'interpreter' or 'interpreter_path' attributes must be specified") + runtime_files = depset(transitive = [ + t[DefaultInfo].files + for t in ctx.attr.files + ]) + hermetic = bool(interpreter) - runtime_files = ctx.attr.files[DefaultInfo].files if not hermetic: if runtime_files: fail("if 'interpreter_path' is given then 'files' must be empty") @@ -62,11 +66,13 @@ def _py_runtime_impl(ctx): "Python runtime mechanism (`--incompatible_use_python_toolchains=false`).", ) else: - python_version = ctx.fragments.default_python_version + python_version = ctx.fragments.py.default_python_version return [ _PyRuntimeInfo( - files = runtime_files, + interpreter_path = interpreter_path or None, + interpreter = interpreter, + files = runtime_files if hermetic else None, coverage_tool = coverage_tool, coverage_files = coverage_files, python_version = python_version, @@ -152,6 +158,7 @@ the `run` and `lcov` subcommands. """, ), "python_version": attr.string( + default = "_INTERNAL_SENTINEL", values = ["PY2", "PY3", "_INTERNAL_SENTINEL"], doc = """ Whether this runtime is for Python major version 2 or 3. Valid values are `"PY2"`