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 9ebd564 commit d7dcb29
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 30 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
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
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 toolcahin
if RunEnvironmentInfo in target:
env.update(target[RunEnvironmentInfo].environment)
providers = [
DefaultInfo(
executable = executable,
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
23 changes: 17 additions & 6 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 Down Expand Up @@ -88,19 +93,25 @@ 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.string(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),
)
Expand Down
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
7 changes: 6 additions & 1 deletion 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,9 +26,14 @@ 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.""",
Expand Down

0 comments on commit d7dcb29

Please sign in to comment.