Skip to content

Commit f35db76

Browse files
committed
chore(semantics): Remove determine_main
Determining the main using suffix matching was always a goofy idea. The underlying rules have long required that main be a label. Rip out determine_main and provide minimal default semantics for main as part of the existing wrapper macros. Those too should go away, but for now we have them. Replaces #641 with thanks to Keith.
1 parent bbed6aa commit f35db76

File tree

4 files changed

+11
-120
lines changed

4 files changed

+11
-120
lines changed

py/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ bzl_library(
2828
visibility = ["//visibility:public"],
2929
deps = [
3030
"//py/private:py_binary",
31-
"//py/private:py_executable",
3231
"//py/private:py_image_layer",
3332
"//py/private:py_library",
3433
"//py/private:py_pex_binary",

py/defs.bzl

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ python.toolchain(python_version = "3.9", is_default = True)
3737

3838
load("@aspect_bazel_lib//lib:utils.bzl", "propagate_common_rule_attributes")
3939
load("//py/private:py_binary.bzl", _py_binary = "py_binary", _py_test = "py_test")
40-
load("//py/private:py_executable.bzl", "determine_main")
4140
load("//py/private:py_image_layer.bzl", _py_image_layer = "py_image_layer")
4241
load("//py/private:py_library.bzl", _py_library = "py_library")
4342
load("//py/private:py_pex_binary.bzl", _py_pex_binary = "py_pex_binary")
@@ -66,23 +65,12 @@ def _py_binary_or_test(name, rule, srcs, main, data = [], deps = [], resolutions
6665
exec_properties = kwargs.pop("exec_properties", {})
6766
non_test_exec_properties = {k: v for k, v in exec_properties.items() if not k.startswith("test.")}
6867

69-
# Compatibility with rules_python, see docs in py_executable.bzl
70-
main_target = "{}.find_main".format(name)
71-
determine_main(
72-
name = main_target,
73-
target_name = name,
74-
main = main,
75-
srcs = srcs,
76-
exec_properties = non_test_exec_properties,
77-
**propagate_common_rule_attributes(kwargs)
78-
)
79-
8068
package_collisions = kwargs.pop("package_collisions", None)
8169

8270
rule(
8371
name = name,
8472
srcs = srcs,
85-
main = main_target,
73+
main = main,
8674
data = data,
8775
deps = deps,
8876
resolutions = resolutions,
@@ -113,10 +101,8 @@ def py_binary(name, srcs = [], main = None, **kwargs):
113101
Args:
114102
name: Name of the rule.
115103
srcs: Python source files.
116-
main: Entry point.
117-
Like rules_python, this is treated as a suffix of a file that should appear among the srcs.
118-
If absent, then `[name].py` is tried. As a final fallback, if the srcs has a single file,
119-
that is used as the main.
104+
main: The label of an entrypoint file which will be used as the binary's entrypoint.
105+
If srcs is length 1, main is implied to be the only src.
120106
**kwargs: additional named parameters to `py_binary_rule`.
121107
"""
122108

@@ -126,6 +112,11 @@ def py_binary(name, srcs = [], main = None, **kwargs):
126112
if resolutions:
127113
resolutions = resolutions.to_label_keyed_dict()
128114

115+
if not main and len(srcs) == 1:
116+
main = srcs[0]
117+
elif main and main not in srcs:
118+
srcs = srcs + [main]
119+
129120
_py_binary_or_test(
130121
name = name,
131122
rule = _py_binary,
@@ -141,10 +132,8 @@ def py_test(name, srcs = [], main = None, pytest_main = False, **kwargs):
141132
Args:
142133
name: Name of the rule.
143134
srcs: Python source files.
144-
main: Entry point.
145-
Like rules_python, this is treated as a suffix of a file that should appear among the srcs.
146-
If absent, then `[name].py` is tried. As a final fallback, if the srcs has a single file,
147-
that is used as the main.
135+
main: The label of an entrypoint file which will be used as the test runner.
136+
Must exist as a listed src.
148137
pytest_main: If set, generate a [py_pytest_main](#py_pytest_main) script and use it as the main.
149138
The deps should include the pytest package (as well as the coverage package if desired).
150139
**kwargs: additional named parameters to `py_binary_rule`.
@@ -160,7 +149,7 @@ def py_test(name, srcs = [], main = None, pytest_main = False, **kwargs):
160149
resolutions = resolutions.to_label_keyed_dict()
161150

162151
deps = kwargs.pop("deps", [])
163-
if pytest_main:
152+
if pytest_main or not main:
164153
if main:
165154
fail("When pytest_main is set, the main attribute should not be set.")
166155
pytest_main_target = name + ".pytest_main"

py/private/BUILD.bazel

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ bzl_library(
9292
srcs = ["providers.bzl"],
9393
)
9494

95-
bzl_library(
96-
name = "py_executable",
97-
srcs = ["py_executable.bzl"],
98-
)
99-
10095
bzl_library(
10196
name = "transitions",
10297
srcs = ["transitions.bzl"],

py/private/py_executable.bzl

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)