Skip to content

Commit 84c9c12

Browse files
rickeylevcopybara-github
authored andcommitted
Add is_singleton_depset to optimize how py_runtime finds the coverage tool.
Work towards #15897 PiperOrigin-RevId: 485161913 Change-Id: I65128d9371a3e2124b2a82143530d88ba23e49b7
1 parent 2405238 commit 84c9c12

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/main/java/com/google/devtools/build/lib/rules/python/PyBuiltins.java

+18
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,29 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.rules.python;
1515

16+
import com.google.devtools.build.lib.collect.nestedset.Depset;
17+
import net.starlark.java.annot.Param;
1618
import net.starlark.java.annot.StarlarkBuiltin;
19+
import net.starlark.java.annot.StarlarkMethod;
1720
import net.starlark.java.eval.StarlarkValue;
1821

1922
/** Bridge to allow builtins bzl code to call Java code. */
2023
@StarlarkBuiltin(name = "py_builtins", documented = false)
2124
public abstract class PyBuiltins implements StarlarkValue {
2225
public static final String NAME = "py_builtins";
26+
27+
@StarlarkMethod(
28+
name = "is_singleton_depset",
29+
doc = "Efficiently checks if the depset is a singleton.",
30+
parameters = {
31+
@Param(
32+
name = "value",
33+
positional = true,
34+
named = false,
35+
defaultValue = "unbound",
36+
doc = "depset to check for being a singleton")
37+
})
38+
public boolean isSingletonDepset(Depset depset) {
39+
return depset.getSet().isSingleton();
40+
}
2341
}

src/main/starlark/builtins_bzl/common/python/py_internal.bzl

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"""PYTHON RULE IMPLEMENTATION ONLY: Do not use outside of the rule implementations and their tests.
1515
1616
Various builtin Starlark defined objects exposed for non-builtin Starlark.
17+
18+
These may change at any time and are closely coupled to the rule implementation.
1719
"""
1820

1921
# This replaces the Java-defined name using exports.bzl toplevels mapping.

src/main/starlark/builtins_bzl/common/python/py_runtime_rule.bzl

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ load(":common/paths.bzl", "paths")
1717

1818
_PyRuntimeInfo = _builtins.toplevel.PyRuntimeInfo
1919

20+
_py_builtins = _builtins.internal.py_builtins
21+
2022
def _py_runtime_impl(ctx):
2123
interpreter_path = ctx.attr.interpreter_path or None # Convert empty string to None
2224
interpreter = ctx.file.interpreter
@@ -38,11 +40,8 @@ def _py_runtime_impl(ctx):
3840
if ctx.attr.coverage_tool:
3941
coverage_di = ctx.attr.coverage_tool[DefaultInfo]
4042

41-
# TODO(b/254866025): Use a Java helper to call NestedSet.isSingleton
42-
# instead of always flattening to a list
43-
coverage_di_files = coverage_di.files.to_list()
44-
if len(coverage_di_files) == 1:
45-
coverage_tool = coverage_di_files[0]
43+
if _py_builtins.is_singleton_depset(coverage_di.files):
44+
coverage_tool = coverage_di.files.to_list()[0]
4645
elif coverage_di.files_to_run and coverage_di.files_to_run.executable:
4746
coverage_tool = coverage_di.files_to_run.executable
4847
else:

0 commit comments

Comments
 (0)