Skip to content

Commit bf9fdb2

Browse files
authored
Don't bake env into binary (aspect-build#464)
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.
1 parent f22eff9 commit bf9fdb2

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

py/private/py_binary.bzl

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Implementation for the py_binary and py_test rules."""
22

3-
load("@rules_python//python:defs.bzl", "PyInfo")
4-
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path")
53
load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_variables")
4+
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path")
5+
load("@rules_python//python:defs.bzl", "PyInfo")
66
load("//py/private:py_library.bzl", _py_library = "py_library_utils")
77
load("//py/private:py_semantics.bzl", _py_semantics = "semantics")
88
load("//py/private/toolchain:types.bzl", "PY_TOOLCHAIN", "VENV_TOOLCHAIN")
@@ -49,14 +49,15 @@ def _py_binary_rule_impl(ctx):
4949
content = pth_lines,
5050
)
5151

52-
env = dict({
52+
default_env = {
5353
"BAZEL_TARGET": str(ctx.label).lstrip("@"),
5454
"BAZEL_WORKSPACE": ctx.workspace_name,
5555
"BAZEL_TARGET_NAME": ctx.attr.name,
56-
}, **ctx.attr.env)
56+
}
5757

58-
for k, v in env.items():
59-
env[k] = expand_variables(
58+
passed_env = dict(ctx.attr.env)
59+
for k, v in passed_env.items():
60+
passed_env[k] = expand_variables(
6061
ctx,
6162
expand_locations(ctx, v, ctx.attr.data),
6263
attribute_name = "env",
@@ -75,7 +76,7 @@ def _py_binary_rule_impl(ctx):
7576
"{{ARG_VENV_NAME}}": ".{}.venv".format(ctx.attr.name),
7677
"{{ARG_PTH_FILE}}": to_rlocation_path(ctx, site_packages_pth_file),
7778
"{{ENTRYPOINT}}": to_rlocation_path(ctx, ctx.file.main),
78-
"{{PYTHON_ENV}}": "\n".join(_dict_to_exports(env)).strip(),
79+
"{{PYTHON_ENV}}": "\n".join(_dict_to_exports(default_env)).strip(),
7980
"{{EXEC_PYTHON_BIN}}": "python{}".format(
8081
py_toolchain.interpreter_version_info.major,
8182
),
@@ -123,6 +124,9 @@ def _py_binary_rule_impl(ctx):
123124
uses_shared_libraries = False,
124125
),
125126
instrumented_files_info,
127+
RunEnvironmentInfo(
128+
environment = passed_env,
129+
),
126130
]
127131

128132
_attrs = dict({

0 commit comments

Comments
 (0)