Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow greater customization of environment variables by plugins. #226

Merged
merged 3 commits into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions internal/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ proto_compile_attrs = {
),
}

def proto_compile_impl(ctx):
def proto_compile_impl(ctx, base_env = {}):
"""
Common implementation function for lang_*_compile rules.

Args:
ctx: The Bazel rule execution context object.
base_env: Default environment to use for protoc if
"use_built_in_shell_environment" = False for a given plugin.

Returns:
Providers:
Expand All @@ -63,9 +65,9 @@ def proto_compile_impl(ctx):
extra_protoc_files = ctx.files.extra_protoc_files

# Execute with extracted attrs
return proto_compile(ctx, options, extra_protoc_args, extra_protoc_files)
return proto_compile(ctx, options, extra_protoc_args, extra_protoc_files, base_env)

def proto_compile(ctx, options, extra_protoc_args, extra_protoc_files):
def proto_compile(ctx, options, extra_protoc_args, extra_protoc_files, base_env):
"""
Common implementation function for lang_*_compile rules.

Expand All @@ -74,6 +76,8 @@ def proto_compile(ctx, options, extra_protoc_args, extra_protoc_files):
options: The mutable options dict.
extra_protoc_args: The mutable extra_protoc_args list.
extra_protoc_files: The mutable extra_protoc_files list.
base_env: Default environment to use for protoc if
"use_built_in_shell_environment" = False for a given plugin.

Returns:
Providers:
Expand Down Expand Up @@ -327,7 +331,7 @@ def proto_compile(ctx, options, extra_protoc_args, extra_protoc_files):
command = "echo '\n##### SANDBOX BEFORE RUNNING PROTOC' && find . -type l && " + command

if verbose > 3:
command = "env && " + command
command = "echo Printing environment used to invoke protoc.... && env && " + command
for f in cmd_inputs:
print("INPUT:", f.path) # buildifier: disable=print
for f in protos:
Expand All @@ -337,14 +341,23 @@ def proto_compile(ctx, options, extra_protoc_args, extra_protoc_files):
for f in plugin_outputs:
print("EXPECTED OUTPUT:", f.path) # buildifier: disable=print

# Run protoc
env = {}
if (len(env) > 0) and plugin.use_built_in_shell_environment:
aaliddell marked this conversation as resolved.
Show resolved Hide resolved
fail("env and use_built_in_shell_environment are mutually exclusive; " +
" both set for plugin {}".format(plugin.name))

if not plugin.use_built_in_shell_environment:
env = dict(base_env, **env)

# Run protoc (https://bazel.build/rules/lib/actions#run_shell)
ctx.actions.run_shell(
mnemonic = mnemonic,
command = command,
arguments = [args],
inputs = cmd_inputs,
tools = tools,
outputs = plugin_protoc_outputs,
env = env,
use_default_shell_env = plugin.use_built_in_shell_environment,
input_manifests = cmd_input_manifests,
progress_message = "Compiling protoc outputs for {} plugin on target {}".format(
Expand Down
5 changes: 5 additions & 0 deletions internal/plugin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def _proto_plugin_impl(ctx):
tool = ctx.attr.tool,
tool_executable = ctx.executable.tool,
use_built_in_shell_environment = ctx.attr.use_built_in_shell_environment,
env = ctx.attr.env,
protoc_plugin_name = ctx.attr.protoc_plugin_name,
exclusions = ctx.attr.exclusions,
data = ctx.files.data,
Expand Down Expand Up @@ -53,6 +54,10 @@ proto_plugin = rule(
doc = "Whether the tool should use the built in shell environment or not",
default = True,
),
"env": attr.string_dict(
doc = "Sets the dictionary of environment variables to use when invoking protoc. Must be None if use_built_in_shell_environment is true.",
default = {},
),
"protoc_plugin_name": attr.string(
doc = "The name used for the plugin binary on the protoc command line. Useful for targeting built-in plugins. Uses plugin name when not set",
),
Expand Down
1 change: 1 addition & 0 deletions internal/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ProtoPluginInfo = provider(
"tool": "The plugin binary. If absent, it is assumed the plugin is built-in to protoc itself and plugin_name will be used if available, otherwise the plugin name",
"tool_executable": "The plugin binary executable",
"use_built_in_shell_environment": "Whether the tool should use the built in shell environment or not",
"env": "Sets the dictionary of environment variables to use when invoking protoc. Must be None if use_built_in_shell_environment is true.",
"protoc_plugin_name": "The name used for the plugin binary on the protoc command line. Useful for targeting built-in plugins. Uses plugin name when not set",
"exclusions": "Exclusion filters to apply when generating outputs with this plugin. Used to prevent generating files that are included in the protobuf library, for example. Can exclude either by proto name prefix or by proto folder prefix",
"data": "Additional files required for running the plugin",
Expand Down