Skip to content

Commit bbed6aa

Browse files
arrdemkeith
andauthored
Fix absolute label use in main attribute of py_test (#582)
Replaces #463 from @keith. We should get this in as part of our next break. In rules_python you can specify a file from another package like: ```bzl main = "//path/to:file.py", ``` This didn't work in rules_py since it was taking the main as a string to manipulate. This appears closer to me to the upstream logic that was mirrored, so maybe this was changed for another reason --- ### Changes are visible to end-users: yes - Searched for relevant documentation and updated as needed: yes/no - Breaking change (forces users to change their own code or config): yes/no - Suggested release notes appear below: yes/no ### Test plan - [x] New test cases added Co-authored-by: Keith Smiley <[email protected]>
1 parent dc2ed38 commit bbed6aa

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

examples/pytest/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,21 @@ py_test(
3838
shard_count = 2,
3939
deps = ["@pypi//pytest"],
4040
)
41+
42+
py_test(
43+
name = "absolute_main_test",
44+
srcs = [
45+
"__init__.py",
46+
"main_test.py",
47+
],
48+
main = "//examples/pytest:main_test.py",
49+
)
50+
51+
py_test(
52+
name = "main_with_colon_test",
53+
srcs = [
54+
"__init__.py",
55+
"main_test.py",
56+
],
57+
main = ":main_test.py",
58+
)

examples/pytest/__init__.py

Whitespace-only changes.

examples/pytest/main_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("loaded as main!")

py/private/py_executable.bzl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ def _determine_main(ctx):
2525
Artifact; the main file. If one can't be found, an error is raised.
2626
"""
2727
if ctx.attr.main:
28-
# Deviation from rules_python: allow a leading colon, e.g. `main = ":my_target"`
29-
proposed_main = ctx.attr.main.removeprefix(":")
28+
proposed_main = ctx.attr.main.label.name
3029
if not proposed_main.endswith(".py"):
3130
fail("main {} must end in '.py'".format(proposed_main))
3231
else:
@@ -87,7 +86,7 @@ determine_main = rule(
8786
implementation = _determine_main_impl,
8887
attrs = {
8988
"target_name": attr.string(mandatory = True, doc = "The name of the py_binary or py_test we are finding a main for"),
90-
"main": attr.string(doc = "Hint the user supplied as the main"),
89+
"main": attr.label(doc = "Hint the user supplied as the main", allow_single_file = True),
9190
"srcs": attr.label_list(allow_files = True),
9291
},
9392
)

0 commit comments

Comments
 (0)