diff --git a/bazel_env.bzl b/bazel_env.bzl index 956a0bc..b8eec04 100644 --- a/bazel_env.bzl +++ b/bazel_env.bzl @@ -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 @@ -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, @@ -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() + ]), }, ) diff --git a/launcher.sh.tpl b/launcher.sh.tpl index daf69a9..187fb87 100644 --- a/launcher.sh.tpl +++ b/launcher.sh.tpl @@ -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