Skip to content

Commit

Permalink
Fix tool environment
Browse files Browse the repository at this point in the history
Makes available the environment returned by a tool target through the
RunEnvironmentInfo provider.
  • Loading branch information
PingWriter committed Oct 12, 2024
1 parent c22fd5c commit 1441e40
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 12 additions & 1 deletion bazel_env.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def _tool_impl(ctx):
name = ctx.label.name.rpartition("/")[-1]
out = ctx.actions.declare_file(ctx.label.name)

extra_env = {}
if ctx.attr.path:
vars = {
k: v
Expand All @@ -152,10 +153,16 @@ def _tool_impl(ctx):
transitive_files.append(toolchain[DefaultInfo].files)
runfiles = ctx.runfiles(transitive_files = depset(transitive = transitive_files))
else:
# There is only ever a single target, the attribute only takes an array value because of the transition.
target = ctx.attr.target[0]

rlocation_path = _rlocation_path(ctx, ctx.executable.target)

runfiles = ctx.runfiles(ctx.files.target)
runfiles = runfiles.merge(ctx.attr.target[0][DefaultInfo].default_runfiles)
runfiles = runfiles.merge(target[DefaultInfo].default_runfiles)

if RunEnvironmentInfo in target:
extra_env = target[RunEnvironmentInfo].environment

ctx.actions.expand_template(
template = ctx.file._launcher,
Expand All @@ -164,6 +171,10 @@ def _tool_impl(ctx):
substitutions = {
"{{bazel_env_label}}": str(ctx.label).removeprefix("@@").removesuffix("/bin/" + name),
"{{rlocation_path}}": rlocation_path,
"{{extra_env}}": "\n".join([
"export {}={}".format(k, repr(v))
for k, v in extra_env.items()
]),
},
)

Expand Down
2 changes: 2 additions & 0 deletions launcher.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export JAVA_RUNFILES="${RUNFILES_DIR}"
export PYTHON_RUNFILES="${RUNFILES_DIR}"
# Let rules_js' js_binary work by not having it try to cd into BINDIR.
export JS_BINARY__NO_CD_BINDIR=1
# Environment of the executable target.
{{extra_env}}

BUILD_WORKING_DIRECTORY="$(pwd)"
export BUILD_WORKING_DIRECTORY
Expand Down

0 comments on commit 1441e40

Please sign in to comment.