From 4a60d752c261bae0f04952f7fbee22494085531c Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Wed, 4 Dec 2024 18:00:27 +0000 Subject: [PATCH 1/3] Add env_inherit to py_test Fixes https://github.com/aspect-build/rules_py/issues/457 --- examples/pytest/BUILD.bazel | 1 + py/private/py_binary.bzl | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/examples/pytest/BUILD.bazel b/examples/pytest/BUILD.bazel index 24c0d607..41b34215 100644 --- a/examples/pytest/BUILD.bazel +++ b/examples/pytest/BUILD.bazel @@ -11,6 +11,7 @@ py_test( "foo_test.py", ":__test__", ], + env_inherit = ["FOO"], imports = ["../.."], main = ":__test__.py", package_collisions = "warning", diff --git a/py/private/py_binary.bzl b/py/private/py_binary.bzl index cd9cad45..d661cee6 100644 --- a/py/private/py_binary.bzl +++ b/py/private/py_binary.bzl @@ -1,8 +1,8 @@ """Implementation for the py_binary and py_test rules.""" -load("@rules_python//python:defs.bzl", "PyInfo") -load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path") load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_variables") +load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path") +load("@rules_python//python:defs.bzl", "PyInfo") load("//py/private:py_library.bzl", _py_library = "py_library_utils") load("//py/private:py_semantics.bzl", _py_semantics = "semantics") load("//py/private/toolchain:types.bzl", "PY_TOOLCHAIN", "VENV_TOOLCHAIN") @@ -123,6 +123,9 @@ def _py_binary_rule_impl(ctx): uses_shared_libraries = False, ), instrumented_files_info, + RunEnvironmentInfo( + inherited_environment = getattr(ctx.attr, "env_inherit", {}), + ), ] _attrs = dict({ @@ -168,6 +171,13 @@ A collision can occour when multiple packages providing the same file are instal _attrs.update(**_py_library.attrs) +_test_attrs = dict({ + "env_inherit": attr.string_list( + doc = "Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test.", + default = [], + ), +}) + def _python_version_transition_impl(_, attr): if not attr.python_version: return {} @@ -182,6 +192,7 @@ _python_version_transition = transition( py_base = struct( implementation = _py_binary_rule_impl, attrs = _attrs, + test_attrs = _test_attrs, toolchains = [ PY_TOOLCHAIN, VENV_TOOLCHAIN, @@ -201,7 +212,7 @@ py_binary = rule( py_test = rule( doc = "Run a Python program under Bazel. Most users should use the [py_test macro](#py_test) instead of loading this directly.", implementation = py_base.implementation, - attrs = py_base.attrs, + attrs = py_base.attrs | py_base.test_attrs, toolchains = py_base.toolchains, test = True, cfg = py_base.cfg, From 54122cd398deeb6836b44f35dc2bb1cc8ccedde6 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Wed, 4 Dec 2024 11:58:32 -0800 Subject: [PATCH 2/3] Update py_binary.bzl --- py/private/py_binary.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/private/py_binary.bzl b/py/private/py_binary.bzl index d661cee6..6adb53e7 100644 --- a/py/private/py_binary.bzl +++ b/py/private/py_binary.bzl @@ -124,7 +124,7 @@ def _py_binary_rule_impl(ctx): ), instrumented_files_info, RunEnvironmentInfo( - inherited_environment = getattr(ctx.attr, "env_inherit", {}), + inherited_environment = getattr(ctx.attr, "env_inherit", []), ), ] From eaa6605b2e4feca5c6464ade0964195679bdc5f2 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 5 Dec 2024 09:53:54 -0800 Subject: [PATCH 3/3] chore: docgen --- docs/py_test.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/py_test.md b/docs/py_test.md index 225fb79d..d1b440a5 100644 --- a/docs/py_test.md +++ b/docs/py_test.md @@ -42,8 +42,8 @@ python.toolchain(python_version = "3.9", is_default = True) ## py_test_rule
-py_test_rule(name, data, deps, env, imports, main, package_collisions, python_version, resolutions,
-             srcs)
+py_test_rule(name, data, deps, env, env_inherit, imports, main, package_collisions, python_version,
+             resolutions, srcs)
 
Run a Python program under Bazel. Most users should use the [py_test macro](#py_test) instead of loading this directly. @@ -57,6 +57,7 @@ Run a Python program under Bazel. Most users should use the [py_test macro](#py_ | data | Runtime dependencies of the program.

The transitive closure of the data dependencies will be available in the .runfiles folder for this binary/test. The program may optionally use the Runfiles lookup library to locate the data files, see https://pypi.org/project/bazel-runfiles/. | List of labels | optional | [] | | deps | Targets that produce Python code, commonly py_library rules. | List of labels | optional | [] | | env | Environment variables to set when running the binary. | Dictionary: String -> String | optional | {} | +| env_inherit | Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test. | List of strings | optional | [] | | imports | List of import directories to be added to the PYTHONPATH. | List of strings | optional | [] | | main | Script to execute with the Python interpreter. | Label | required | | | package_collisions | The action that should be taken when a symlink collision is encountered when creating the venv. A collision can occour when multiple packages providing the same file are installed into the venv. The possible values are:

* "error": When conflicting symlinks are found, an error is reported and venv creation halts. * "warning": When conflicting symlinks are found, an warning is reported, however venv creation continues. * "ignore": When conflicting symlinks are found, no message is reported and venv creation continues. | String | optional | "error" |