Skip to content

fix(rules): make the srcs trully optional #2768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Unreleased changes template.
* (pypi) The PyPI extension will no longer write the lock file entries as the
extension has been marked reproducible.
Fixes [#2434](https://github.com/bazel-contrib/rules_python/issues/2434).
* (rules) {attr}`py_binary.srcs` and {attr}`py_test.srcs` is no longer mandatory when
`main_module` is specified (for `--bootstrap_impl=script`)

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

Expand Down
3 changes: 2 additions & 1 deletion python/private/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,8 @@ def _create_stage1_bootstrap(
)
template = runtime.bootstrap_template
subs["%shebang%"] = runtime.stub_shebang
elif not ctx.files.srcs:
fail("mandatory 'srcs' files have not been provided")
else:
if (ctx.configuration.coverage_enabled and
runtime and
Expand Down Expand Up @@ -1888,7 +1890,6 @@ def create_executable_rule_builder(implementation, **kwargs):
),
**kwargs
)
builder.attrs.get("srcs").set_mandatory(True)
return builder

def cc_configure_features(
Expand Down
72 changes: 48 additions & 24 deletions tests/base_rules/py_executable_base_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable
load("//tests/base_rules:base_tests.bzl", "create_base_tests")
load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util")
load("//tests/support:py_executable_info_subject.bzl", "PyExecutableInfoSubject")
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "WINDOWS_X86_64")
load("//tests/support:support.bzl", "BOOTSTRAP_IMPL", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "WINDOWS_X86_64")

_tests = []

Expand Down Expand Up @@ -342,6 +342,53 @@ def _test_name_cannot_end_in_py_impl(env, target):
matching.str_matches("name must not end in*.py"),
)

def _test_main_module_bootstrap_system_python(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
main_module = "dummy",
)
analysis_test(
name = name,
impl = _test_main_module_bootstrap_system_python_impl,
target = name + "_subject",
config_settings = {
BOOTSTRAP_IMPL: "system_python",
"//command_line_option:platforms": [LINUX_X86_64],
},
expect_failure = True,
)

def _test_main_module_bootstrap_system_python_impl(env, target):
env.expect.that_target(target).failures().contains_predicate(
matching.str_matches("mandatory*srcs"),
)

_tests.append(_test_main_module_bootstrap_system_python)

def _test_main_module_bootstrap_script(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
main_module = "dummy",
)
analysis_test(
name = name,
impl = _test_main_module_bootstrap_script_impl,
target = name + "_subject",
config_settings = {
BOOTSTRAP_IMPL: "script",
"//command_line_option:platforms": [LINUX_X86_64],
},
)

def _test_main_module_bootstrap_script_impl(env, target):
env.expect.that_target(target).default_outputs().contains(
"{package}/{test_name}_subject",
)

_tests.append(_test_main_module_bootstrap_script)

def _test_py_runtime_info_provided(name, config):
rt_util.helper_target(
config.rule,
Expand All @@ -365,29 +412,6 @@ def _test_py_runtime_info_provided_impl(env, target):

_tests.append(_test_py_runtime_info_provided)

# Can't test this -- mandatory validation happens before analysis test
# can intercept it
# TODO(#1069): Once re-implemented in Starlark, modify rule logic to make this
# testable.
# def _test_srcs_is_mandatory(name, config):
# rt_util.helper_target(
# config.rule,
# name = name + "_subject",
# )
# analysis_test(
# name = name,
# impl = _test_srcs_is_mandatory,
# target = name + "_subject",
# expect_failure = True,
# )
#
# _tests.append(_test_srcs_is_mandatory)
#
# def _test_srcs_is_mandatory_impl(env, target):
# env.expect.that_target(target).failures().contains_predicate(
# matching.str_matches("mandatory*srcs"),
# )

# =====
# You were gonna add a test at the end, weren't you?
# Nope. Please keep them sorted; put it in its alphabetical location.
Expand Down
1 change: 1 addition & 0 deletions tests/support/support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ CROSSTOOL_TOP = Label("//tests/support/cc_toolchains:cc_toolchain_suite")
# str() around Label() is necessary because rules_testing's config_settings
# doesn't accept yet Label objects.
ADD_SRCS_TO_RUNFILES = str(Label("//python/config_settings:add_srcs_to_runfiles"))
BOOTSTRAP_IMPL = str(Label("//python/config_settings:bootstrap_impl"))
EXEC_TOOLS_TOOLCHAIN = str(Label("//python/config_settings:exec_tools_toolchain"))
PRECOMPILE = str(Label("//python/config_settings:precompile"))
PRECOMPILE_SOURCE_RETENTION = str(Label("//python/config_settings:precompile_source_retention"))
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy