Skip to content

Commit 8b1da00

Browse files
committed
fix(rules): make the srcs trully optional
With this PR we mark the srcs attribute as optional as we can leverage the `main_module` to just run things from the deps. Fixes bazel-contrib#2765
1 parent 34e433b commit 8b1da00

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ Unreleased changes template.
7676
* (pypi) The PyPI extension will no longer write the lock file entries as the
7777
extension has been marked reproducible.
7878
Fixes [#2434](https://github.com/bazel-contrib/rules_python/issues/2434).
79+
* (rules) {attr}`py_binary.srcs` and {attr}`py_test.srcs` is no longer mandatory when
80+
`main_module` is specified.
7981

8082
[20250317]: https://github.com/astral-sh/python-build-standalone/releases/tag/20250317
8183

python/private/py_executable.bzl

+3-2
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ def _create_stage1_bootstrap(
786786
)
787787
template = runtime.bootstrap_template
788788
subs["%shebang%"] = runtime.stub_shebang
789-
else:
789+
elif main_py:
790790
if (ctx.configuration.coverage_enabled and
791791
runtime and
792792
runtime.coverage_tool):
@@ -807,6 +807,8 @@ def _create_stage1_bootstrap(
807807
subs["%import_all%"] = ("True" if ctx.fragments.bazel_py.python_import_all_repositories else "False")
808808
subs["%imports%"] = ":".join(imports.to_list())
809809
subs["%main%"] = "{}/{}".format(ctx.workspace_name, main_py.short_path)
810+
else:
811+
fail("'main' or 'srcs' must be specified")
810812

811813
ctx.actions.expand_template(
812814
template = template,
@@ -1888,7 +1890,6 @@ def create_executable_rule_builder(implementation, **kwargs):
18881890
),
18891891
**kwargs
18901892
)
1891-
builder.attrs.get("srcs").set_mandatory(True)
18921893
return builder
18931894

18941895
def cc_configure_features(

tests/base_rules/py_executable_base_tests.bzl

+45-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable
2424
load("//tests/base_rules:base_tests.bzl", "create_base_tests")
2525
load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util")
2626
load("//tests/support:py_executable_info_subject.bzl", "PyExecutableInfoSubject")
27-
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "WINDOWS_X86_64")
27+
load("//tests/support:support.bzl", "BOOTSTRAP_IMPL", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "WINDOWS_X86_64")
2828

2929
_tests = []
3030

@@ -365,6 +365,50 @@ def _test_py_runtime_info_provided_impl(env, target):
365365

366366
_tests.append(_test_py_runtime_info_provided)
367367

368+
def _test_no_srcs_script_bootstrap(name, config):
369+
rt_util.helper_target(
370+
config.rule,
371+
name = name + "_subject",
372+
main_module = "dummy",
373+
)
374+
analysis_test(
375+
name = name,
376+
impl = _test_no_srcs_script_bootstrap_impl,
377+
target = name + "_subject",
378+
config_settings = {
379+
BOOTSTRAP_IMPL: "script",
380+
"//command_line_option:platforms": [LINUX_X86_64],
381+
},
382+
)
383+
384+
def _test_no_srcs_script_bootstrap_impl(env, target):
385+
env.expect.that_target(target).default_outputs().contains(
386+
"{package}/{test_name}_subject",
387+
)
388+
389+
_tests.append(_test_no_srcs_script_bootstrap)
390+
391+
def _test_no_srcs(name, config):
392+
rt_util.helper_target(
393+
config.rule,
394+
name = name + "_subject",
395+
main_module = "dummy",
396+
)
397+
analysis_test(
398+
name = name,
399+
impl = _test_no_srcs_impl,
400+
target = name + "_subject",
401+
config_settings = {
402+
"//command_line_option:platforms": [LINUX_X86_64],
403+
},
404+
expect_failure = True,
405+
)
406+
407+
def _test_no_srcs_impl(_, __):
408+
pass
409+
410+
_tests.append(_test_no_srcs)
411+
368412
# Can't test this -- mandatory validation happens before analysis test
369413
# can intercept it
370414
# TODO(#1069): Once re-implemented in Starlark, modify rule logic to make this

tests/support/support.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ CROSSTOOL_TOP = Label("//tests/support/cc_toolchains:cc_toolchain_suite")
3535
# str() around Label() is necessary because rules_testing's config_settings
3636
# doesn't accept yet Label objects.
3737
ADD_SRCS_TO_RUNFILES = str(Label("//python/config_settings:add_srcs_to_runfiles"))
38+
BOOTSTRAP_IMPL = str(Label("//python/config_settings:bootstrap_impl"))
3839
EXEC_TOOLS_TOOLCHAIN = str(Label("//python/config_settings:exec_tools_toolchain"))
3940
PRECOMPILE = str(Label("//python/config_settings:precompile"))
4041
PRECOMPILE_SOURCE_RETENTION = str(Label("//python/config_settings:precompile_source_retention"))

0 commit comments

Comments
 (0)