Skip to content

Commit

Permalink
Add env variable to rule transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ewianda committed Oct 31, 2024
1 parent 8973b71 commit 9e4665c
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 59 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
# To update these lines, execute
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
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
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
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
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

test --test_output=errors

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ A brief description of the categories of changes:

{#v0-0-0-added}
### Added
* (toolchain) Using testing toolchain to configure py_test coverage.
This opens the potential to configure differnt test runners.
([#2246](https://github.com/bazelbuild/rules_python/pull/2246)).
* (publish) The requirements file for the `twine` publishing rules have been
updated to have a new convention: `requirements_darwin.txt`,
`requirements_linux.txt`, `requirements_windows.txt` for each respective OS
Expand Down
20 changes: 0 additions & 20 deletions examples/bzlmod/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/bzlmod/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ py_test(
main = "coverage_rc_is_set_test.py",
)

py_test_3_9(
name = "coverage_rc_is_set_3_9_test",
srcs = ["coverage_rc_is_set_test.py"],
main = "coverage_rc_is_set_test.py",
)

py_test_3_11(
name = "coverage_rc_is_set_3_11_test",
srcs = ["coverage_rc_is_set_test.py"],
main = "coverage_rc_is_set_test.py",
)

py_test_3_9(
name = "my_lib_3_9_test",
srcs = ["my_lib_test.py"],
Expand Down
2 changes: 1 addition & 1 deletion examples/bzlmod/tests/coverage_rc_is_set_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_coverage_rc_file_exists(self):
coverage_rc_path = os.environ.get("COVERAGE_RC")
self.assertTrue(
os.path.isfile(coverage_rc_path),
"COVERAGE_RC does not point to a valid file",
f"COVERAGE_RC does not point to a valid file, {coverage_rc_path}",
)

# Read the content of the file and assert it matches the expected content
Expand Down
4 changes: 4 additions & 0 deletions python/config_settings/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def _transition_py_impl(ctx):
for k, v in ctx.attr.env.items():
env[k] = ctx.expand_location(v)

# Add the environment from the target if it has RunEnvironmentInfo.
# RunEnvironmentInfo contains environment variables configured by exec_group toolchain
if RunEnvironmentInfo in target:
env.update(target[RunEnvironmentInfo].environment)
providers = [
DefaultInfo(
executable = executable,
Expand Down
6 changes: 0 additions & 6 deletions python/extensions/python_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ python_test.configure(
use_repo(python_test, "py_test_toolchain")
register_toolchains("@py_test_toolchain//:all")
```
:::{seealso}
For more in-depth documentation see the {obj}`python.toolchain`.
:::
::::
"""

load("//python/private:python_test.bzl", _python_test = "python_test")
Expand Down
1 change: 1 addition & 0 deletions python/private/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ def _create_providers(
runtime_details: struct of runtime information; see _get_runtime_details()
output_groups: dict[str, depset[File]]; used to create OutputGroupInfo
semantics: BinarySemantics struct; see create_binary_semantics()
is_test: bool; True if the rule is a test rule,
Returns:
A list of modern providers.
Expand Down
27 changes: 19 additions & 8 deletions python/private/py_test_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ load(
"PY_TEST_TOOLCHAIN_TYPE",
)

PytestProvider = provider(
PyTestProviderInfo = provider(
doc = "Information about the pytest toolchain",
fields = [
"coverage_rc",
],
Expand All @@ -31,7 +32,7 @@ PytestProvider = provider(
def _py_test_toolchain_impl(ctx):
return [
platform_common.ToolchainInfo(
py_test_info = PytestProvider(
py_test_info = PyTestProviderInfo(
coverage_rc = ctx.attr.coverage_rc,
),
),
Expand All @@ -56,6 +57,10 @@ py_test_toolchain_macro(
def py_test_toolchain_macro(*, name, coverage_rc, toolchain_type):
"""
Macro to create a py_test_toolchain rule and a native toolchain rule.
name: The name of the toolchain.
coverage_rc: The coverage rc file.
toolchain_type: The toolchain type.
"""
py_test_toolchain(
name = "{}_toolchain".format(name),
Expand All @@ -73,7 +78,7 @@ def _toolchains_repo_impl(repository_ctx):
kwargs = dict(
name = repository_ctx.name,
coverage_rc = str(repository_ctx.attr.coverage_rc),
toolchain_type = repository_ctx.attr.toolchain_type,
toolchain_type = str(repository_ctx.attr.toolchain_type),
)

build_content = _TOOLCHAIN_TEMPLATE.format(
Expand All @@ -88,21 +93,27 @@ py_test_toolchain_repo = repository_rule(
_toolchains_repo_impl,
doc = "Generates a toolchain hub repository",
attrs = {
"toolchain_type": attr.string(doc = "Toolchain type", mandatory = True),
"coverage_rc": attr.label(
allow_single_file = True,
doc = "The coverage rc file",
mandatory = True,
),
"toolchain_type": attr.label(doc = "Toolchain type", mandatory = True),
},
)

def register_py_test_toolchain(coverage_rc, register_toolchains = True):
# Need to create a repository rule for this to work.
def register_py_test_toolchain(name, coverage_rc, register_toolchains = True):
""" Register the py_test_toolchain and native toolchain rules.
name: The name of the toolchain.
coverage_rc: The coverage rc file.
register_toolchains: Whether to register the toolchains.
"""
py_test_toolchain_repo(
name = "py_test_toolchain",
name = name,
coverage_rc = coverage_rc,
toolchain_type = str(PY_TEST_TOOLCHAIN_TYPE),
toolchain_type = PY_TEST_TOOLCHAIN_TYPE,
)
if register_toolchains:
native.toolchain(name = "py_test_toolchain")
1 change: 0 additions & 1 deletion python/private/py_toolchain_suite.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ load(
":toolchain_types.bzl",
"EXEC_TOOLS_TOOLCHAIN_TYPE",
"PY_CC_TOOLCHAIN_TYPE",
"PY_TEST_TOOLCHAIN_TYPE",
"TARGET_TOOLCHAIN_TYPE",
)

Expand Down
28 changes: 7 additions & 21 deletions python/private/python_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

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

load("@bazel_features//:features.bzl", "bazel_features")
load("//python/private:py_test_toolchain.bzl", "register_py_test_toolchain")
load(":text_util.bzl", "render")

def _python_test_impl(module_ctx):
"""Implementation of the `coverage` extension.
Expand All @@ -26,35 +26,21 @@ def _python_test_impl(module_ctx):
for mod in module_ctx.modules:
for tag in mod.tags.configure:
register_py_test_toolchain(
name = "py_test_toolchain",
coverage_rc = tag.coveragerc,
register_toolchains = False,
)
if bazel_features.external_deps.extension_metadata_has_reproducible:
return module_ctx.extension_metadata(reproducible = True)
else:
return None

configure = tag_class(
doc = """Tag class used to register Python toolchains.""",
attrs = {
# TODO: Add testrunner and potentially coverage_tool
"coveragerc": attr.label(
doc = """Tag class used to register Python toolchains.
:::{topic} Toolchains in the Root Module
:::{tip}
In order to use a different name than the above, you can use the following `MODULE.bazel`
syntax:
```starlark
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
is_default = True,
python_version = "3.11",
)
use_repo(python, my_python_name = "python_3_11")
```
Then the python interpreter will be available as `my_python_name`.
:::
""",
doc = """Tag class used to register Python toolchains.""",
mandatory = True,
),
},
Expand Down

0 comments on commit 9e4665c

Please sign in to comment.