Skip to content

Commit

Permalink
Make PyRuntimeConfiguredTargetTest pass with Starlark py_runtime impl.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rickeylev authored and copybara-github committed Oct 31, 2022
1 parent 3ef518f commit a8d0d50
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/main/starlark/builtins_bzl/common/python/py_runtime_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"`
Expand Down

0 comments on commit a8d0d50

Please sign in to comment.