Skip to content

Commit 4dd871d

Browse files
committed
chore: update docs
1 parent b8a96eb commit 4dd871d

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

docs/rules.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

py/defs.bzl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ _a_struct_type = type(struct())
1818
_a_string_type = type("")
1919

2020
def _py_binary_or_test(name, rule, srcs, main, imports, deps = [], resolutions = {}, **kwargs):
21-
# Compatibility with rules_python, see docs on find_main
21+
# Compatibility with rules_python, see docs in py_executable.bzl
2222
main_target = "_{}.find_main".format(name)
2323
determine_main(
2424
name = main_target,
@@ -56,8 +56,10 @@ def py_binary(name, srcs = [], main = None, imports = ["."], resolutions = {}, *
5656
Args:
5757
name: name of the rule
5858
srcs: python source files
59-
main: the entry point. If absent, then the first entry in srcs is used. If srcs is non-empty,
60-
then this is treated as a suffix of a file that should appear among the srcs.
59+
main: the entry point.
60+
Like rules_python, this is treated as a suffix of a file that should appear among the srcs.
61+
If absent, then "[name].py" is tried. As a final fallback, if the srcs has a single file,
62+
that is used as the main.
6163
imports: List of import paths to add for this binary.
6264
resolutions: FIXME
6365
**kwargs: additional named parameters to the py_binary_rule
@@ -89,6 +91,8 @@ def py_binary(name, srcs = [], main = None, imports = ["."], resolutions = {}, *
8991

9092
def py_test(name, main = None, srcs = [], imports = ["."], **kwargs):
9193
"Identical to py_binary, but produces a target that can be used with `bazel test`."
94+
95+
# Ensure that any other targets we write will be testonly like the py_test target
9296
kwargs["testonly"] = True
9397
_py_binary_or_test(name = name, rule = _py_test, srcs = srcs, main = main, imports = imports, **kwargs)
9498

py/private/py_executable.bzl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ def _determine_main(ctx):
3636

3737
main_files = [src for src in ctx.files.srcs if _path_endswith(src.short_path, proposed_main)]
3838

39-
###
4039
# Deviation from logic in rules_python: rules_py is a bit more permissive.
41-
# Allow a srcs of length one to determine the main, if the target name didn't
40+
# Allow a srcs of length one to determine the main, if the target name didn't match anything.
4241
if not main_files and len(ctx.files.srcs) == 1:
4342
main_files = ctx.files.srcs
4443

45-
### End deviations
46-
4744
if not main_files:
4845
if ctx.attr.main:
4946
fail("could not find '{}' as specified by 'main' attribute".format(proposed_main))
@@ -70,7 +67,8 @@ def _determine_main(ctx):
7067

7168
# Adapts the function above, which we copied from rules_python, to be a standalone rule so we can
7269
# use it from a macro.
73-
# (We want our underlying py_binary rule to be simple: 'main' is a mandatory label)
70+
# This is because we want our underlying py_binary rule to be simple: 'main' is a mandatory label.
71+
# The default output of this rule will be used as that label.
7472
def _determine_main_impl(ctx):
7573
return DefaultInfo(files = depset([_determine_main(ctx)]))
7674

@@ -89,7 +87,7 @@ determine_main = rule(
8987
implementation = _determine_main_impl,
9088
attrs = {
9189
"target_name": attr.string(mandatory = True, doc = "The name of the py_binary or py_test we are finding a main for"),
92-
"main": attr.string("Hint the user supplied as the main"),
90+
"main": attr.string(doc = "Hint the user supplied as the main"),
9391
"srcs": attr.label_list(allow_files = True),
9492
},
9593
)

0 commit comments

Comments
 (0)