@@ -37,7 +37,6 @@ python.toolchain(python_version = "3.9", is_default = True)
3737
3838load ("@aspect_bazel_lib//lib:utils.bzl" , "propagate_common_rule_attributes" )
3939load ("//py/private:py_binary.bzl" , _py_binary = "py_binary" , _py_test = "py_test" )
40- load ("//py/private:py_executable.bzl" , "determine_main" )
4140load ("//py/private:py_image_layer.bzl" , _py_image_layer = "py_image_layer" )
4241load ("//py/private:py_library.bzl" , _py_library = "py_library" )
4342load ("//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"
0 commit comments