From 6a5b33607a24af1e29dbbe050963747e0ae2cccd Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Thu, 5 Dec 2024 00:33:23 +0000 Subject: [PATCH] Don't bake env into binary This mirrors the behavior of rules_python's py_binary, and other *_binary rules. Previously any env set on the binary was baked into the script, and therefore included if the script was run with --run_under, or through a genrule. Now this env is passed through to RunEnvironmentInfo so it only applies in the default cases. --- py/private/py_binary.bzl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/py/private/py_binary.bzl b/py/private/py_binary.bzl index cd9cad45..43b722af 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") @@ -49,14 +49,15 @@ def _py_binary_rule_impl(ctx): content = pth_lines, ) - env = dict({ + default_env = { "BAZEL_TARGET": str(ctx.label).lstrip("@"), "BAZEL_WORKSPACE": ctx.workspace_name, "BAZEL_TARGET_NAME": ctx.attr.name, - }, **ctx.attr.env) + } - for k, v in env.items(): - env[k] = expand_variables( + passed_env = dict(ctx.attr.env) + for k, v in passed_env.items(): + passed_env[k] = expand_variables( ctx, expand_locations(ctx, v, ctx.attr.data), attribute_name = "env", @@ -75,7 +76,7 @@ def _py_binary_rule_impl(ctx): "{{ARG_VENV_NAME}}": ".{}.venv".format(ctx.attr.name), "{{ARG_PTH_FILE}}": to_rlocation_path(ctx, site_packages_pth_file), "{{ENTRYPOINT}}": to_rlocation_path(ctx, ctx.file.main), - "{{PYTHON_ENV}}": "\n".join(_dict_to_exports(env)).strip(), + "{{PYTHON_ENV}}": "\n".join(_dict_to_exports(default_env)).strip(), "{{EXEC_PYTHON_BIN}}": "python{}".format( py_toolchain.interpreter_version_info.major, ), @@ -123,6 +124,9 @@ def _py_binary_rule_impl(ctx): uses_shared_libraries = False, ), instrumented_files_info, + RunEnvironmentInfo( + environment = passed_env, + ), ] _attrs = dict({