Skip to content

Commit 9e4665c

Browse files
committed
Add env variable to rule transitions
1 parent 8973b71 commit 9e4665c

11 files changed

+49
-59
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

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ A brief description of the categories of changes:
4848

4949
{#v0-0-0-added}
5050
### Added
51+
* (toolchain) Using testing toolchain to configure py_test coverage.
52+
This opens the potential to configure differnt test runners.
53+
([#2246](https://github.com/bazelbuild/rules_python/pull/2246)).
5154
* (publish) The requirements file for the `twine` publishing rules have been
5255
updated to have a new convention: `requirements_darwin.txt`,
5356
`requirements_linux.txt`, `requirements_windows.txt` for each respective OS

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"],

examples/bzlmod/tests/coverage_rc_is_set_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_coverage_rc_file_exists(self):
2222
coverage_rc_path = os.environ.get("COVERAGE_RC")
2323
self.assertTrue(
2424
os.path.isfile(coverage_rc_path),
25-
"COVERAGE_RC does not point to a valid file",
25+
f"COVERAGE_RC does not point to a valid file, {coverage_rc_path}",
2626
)
2727

2828
# Read the content of the file and assert it matches the expected content

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/extensions/python_test.bzl

-6
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ python_test.configure(
2626
use_repo(python_test, "py_test_toolchain")
2727
register_toolchains("@py_test_toolchain//:all")
2828
```
29-
30-
:::{seealso}
31-
For more in-depth documentation see the {obj}`python.toolchain`.
32-
:::
33-
::::
34-
3529
"""
3630

3731
load("//python/private:python_test.bzl", _python_test = "python_test")

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

+19-8
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),
@@ -73,7 +78,7 @@ def _toolchains_repo_impl(repository_ctx):
7378
kwargs = dict(
7479
name = repository_ctx.name,
7580
coverage_rc = str(repository_ctx.attr.coverage_rc),
76-
toolchain_type = repository_ctx.attr.toolchain_type,
81+
toolchain_type = str(repository_ctx.attr.toolchain_type),
7782
)
7883

7984
build_content = _TOOLCHAIN_TEMPLATE.format(
@@ -88,21 +93,27 @@ 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.label(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,
105-
toolchain_type = str(PY_TEST_TOOLCHAIN_TYPE),
116+
toolchain_type = PY_TEST_TOOLCHAIN_TYPE,
106117
)
107118
if register_toolchains:
108119
native.toolchain(name = "py_test_toolchain")

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

+7-21
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,35 +26,21 @@ 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.""",
3540
attrs = {
3641
# TODO: Add testrunner and potentially coverage_tool
3742
"coveragerc": attr.label(
38-
doc = """Tag class used to register Python toolchains.
39-
40-
:::{topic} Toolchains in the Root Module
41-
42-
:::{tip}
43-
In order to use a different name than the above, you can use the following `MODULE.bazel`
44-
syntax:
45-
```starlark
46-
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
47-
python.toolchain(
48-
is_default = True,
49-
python_version = "3.11",
50-
)
51-
52-
use_repo(python, my_python_name = "python_3_11")
53-
```
54-
55-
Then the python interpreter will be available as `my_python_name`.
56-
:::
57-
""",
43+
doc = """Tag class used to register Python toolchains.""",
5844
mandatory = True,
5945
),
6046
},

0 commit comments

Comments
 (0)