Skip to content

Commit 02d40e0

Browse files
committed
Add env variable to rule transitions
1 parent 9ebd564 commit 02d40e0

File tree

8 files changed

+42
-30
lines changed

8 files changed

+42
-30
lines changed

.bazelrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, execute
66
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
7+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/python/private,gazelle/pythonconfig,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
8+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/python/private,gazelle/pythonconfig,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
99

1010
test --test_output=errors
1111

examples/bzlmod/MODULE.bazel.lock

-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/bzlmod/tests/BUILD.bazel

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ py_test(
5454
main = "coverage_rc_is_set_test.py",
5555
)
5656

57+
py_test_3_9(
58+
name = "coverage_rc_is_set_3_9_test",
59+
srcs = ["coverage_rc_is_set_test.py"],
60+
main = "coverage_rc_is_set_test.py",
61+
)
62+
63+
py_test_3_11(
64+
name = "coverage_rc_is_set_3_11_test",
65+
srcs = ["coverage_rc_is_set_test.py"],
66+
main = "coverage_rc_is_set_test.py",
67+
)
68+
5769
py_test_3_9(
5870
name = "my_lib_3_9_test",
5971
srcs = ["my_lib_test.py"],

python/config_settings/transition.bzl

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ def _transition_py_impl(ctx):
8181
for k, v in ctx.attr.env.items():
8282
env[k] = ctx.expand_location(v)
8383

84+
# Add the environment from the target if it has RunEnvironmentInfo.
85+
# RunEnvironmentInfo contains environment variables configured by exec_group toolchain
86+
if RunEnvironmentInfo in target:
87+
env.update(target[RunEnvironmentInfo].environment)
8488
providers = [
8589
DefaultInfo(
8690
executable = executable,

python/private/py_executable.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ def _create_providers(
848848
runtime_details: struct of runtime information; see _get_runtime_details()
849849
output_groups: dict[str, depset[File]]; used to create OutputGroupInfo
850850
semantics: BinarySemantics struct; see create_binary_semantics()
851+
is_test: bool; True if the rule is a test rule,
851852
852853
Returns:
853854
A list of modern providers.

python/private/py_test_toolchain.bzl

+17-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ load(
2222
"PY_TEST_TOOLCHAIN_TYPE",
2323
)
2424

25-
PytestProvider = provider(
25+
PyTestProviderInfo = provider(
26+
doc = "Information about the pytest toolchain",
2627
fields = [
2728
"coverage_rc",
2829
],
@@ -31,7 +32,7 @@ PytestProvider = provider(
3132
def _py_test_toolchain_impl(ctx):
3233
return [
3334
platform_common.ToolchainInfo(
34-
py_test_info = PytestProvider(
35+
py_test_info = PyTestProviderInfo(
3536
coverage_rc = ctx.attr.coverage_rc,
3637
),
3738
),
@@ -56,6 +57,10 @@ py_test_toolchain_macro(
5657
def py_test_toolchain_macro(*, name, coverage_rc, toolchain_type):
5758
"""
5859
Macro to create a py_test_toolchain rule and a native toolchain rule.
60+
61+
name: The name of the toolchain.
62+
coverage_rc: The coverage rc file.
63+
toolchain_type: The toolchain type.
5964
"""
6065
py_test_toolchain(
6166
name = "{}_toolchain".format(name),
@@ -88,19 +93,25 @@ py_test_toolchain_repo = repository_rule(
8893
_toolchains_repo_impl,
8994
doc = "Generates a toolchain hub repository",
9095
attrs = {
91-
"toolchain_type": attr.string(doc = "Toolchain type", mandatory = True),
9296
"coverage_rc": attr.label(
9397
allow_single_file = True,
9498
doc = "The coverage rc file",
9599
mandatory = True,
96100
),
101+
"toolchain_type": attr.string(doc = "Toolchain type", mandatory = True),
97102
},
98103
)
99104

100-
def register_py_test_toolchain(coverage_rc, register_toolchains = True):
101-
# Need to create a repository rule for this to work.
105+
def register_py_test_toolchain(name, coverage_rc, register_toolchains = True):
106+
""" Register the py_test_toolchain and native toolchain rules.
107+
108+
name: The name of the toolchain.
109+
coverage_rc: The coverage rc file.
110+
register_toolchains: Whether to register the toolchains.
111+
112+
"""
102113
py_test_toolchain_repo(
103-
name = "py_test_toolchain",
114+
name = name,
104115
coverage_rc = coverage_rc,
105116
toolchain_type = str(PY_TEST_TOOLCHAIN_TYPE),
106117
)

python/private/py_toolchain_suite.bzl

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ load(
2020
":toolchain_types.bzl",
2121
"EXEC_TOOLS_TOOLCHAIN_TYPE",
2222
"PY_CC_TOOLCHAIN_TYPE",
23-
"PY_TEST_TOOLCHAIN_TYPE",
2423
"TARGET_TOOLCHAIN_TYPE",
2524
)
2625

python/private/python_test.bzl

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
"Python test toolchain module extensions for use with bzlmod."
1616

17+
load("@bazel_features//:features.bzl", "bazel_features")
1718
load("//python/private:py_test_toolchain.bzl", "register_py_test_toolchain")
18-
load(":text_util.bzl", "render")
1919

2020
def _python_test_impl(module_ctx):
2121
"""Implementation of the `coverage` extension.
@@ -26,9 +26,14 @@ def _python_test_impl(module_ctx):
2626
for mod in module_ctx.modules:
2727
for tag in mod.tags.configure:
2828
register_py_test_toolchain(
29+
name = "py_test_toolchain",
2930
coverage_rc = tag.coveragerc,
3031
register_toolchains = False,
3132
)
33+
if bazel_features.external_deps.extension_metadata_has_reproducible:
34+
return module_ctx.extension_metadata(reproducible = True)
35+
else:
36+
return None
3237

3338
configure = tag_class(
3439
doc = """Tag class used to register Python toolchains.""",

0 commit comments

Comments
 (0)