Skip to content

Commit

Permalink
Don't bake env into binary (aspect-build#464)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
keith authored Dec 5, 2024
1 parent f22eff9 commit bf9fdb2
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions py/private/py_binary.bzl
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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",
Expand All @@ -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,
),
Expand Down Expand Up @@ -123,6 +124,9 @@ def _py_binary_rule_impl(ctx):
uses_shared_libraries = False,
),
instrumented_files_info,
RunEnvironmentInfo(
environment = passed_env,
),
]

_attrs = dict({
Expand Down

0 comments on commit bf9fdb2

Please sign in to comment.