Skip to content

Commit

Permalink
Merge pull request #226 from gonzojive/customize-env
Browse files Browse the repository at this point in the history
Allow greater customization of environment variables by plugins.
  • Loading branch information
aaliddell authored Dec 4, 2022
2 parents 643523a + ecdc994 commit e9b7119
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
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 @@ -328,7 +332,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 @@ -338,14 +342,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:
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 @@ -14,6 +14,7 @@ def _proto_plugin_impl(ctx):
outputs = ctx.attr.outputs,
out = ctx.attr.out,
output_directory = ctx.attr.output_directory,
env = ctx.attr.env,
extra_protoc_args = ctx.attr.extra_protoc_args,
exclusions = ctx.attr.exclusions,
data = ctx.files.data,
Expand Down Expand Up @@ -49,6 +50,10 @@ proto_plugin = rule(
doc = "Flag that indicates that the plugin should only output a directory. Used for plugins that have no direct mapping from source file name to output name. Cannot be used in conjunction with outputs or out",
default = False,
),
"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 = {},
),
"extra_protoc_args": attr.string_list(
doc = "A list of extra command line arguments to pass directly to protoc, not as plugin options",
),
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

0 comments on commit e9b7119

Please sign in to comment.