diff --git a/internal/compile.bzl b/internal/compile.bzl index fd3c05908..315dccc44 100644 --- a/internal/compile.bzl +++ b/internal/compile.bzl @@ -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: @@ -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. @@ -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: @@ -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: @@ -338,7 +342,15 @@ 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, @@ -346,6 +358,7 @@ def proto_compile(ctx, options, extra_protoc_args, extra_protoc_files): 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( diff --git a/internal/plugin.bzl b/internal/plugin.bzl index 832a8eab6..554db5999 100644 --- a/internal/plugin.bzl +++ b/internal/plugin.bzl @@ -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, @@ -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", ), diff --git a/internal/providers.bzl b/internal/providers.bzl index 18fd3c838..0b7bb5c27 100644 --- a/internal/providers.bzl +++ b/internal/providers.bzl @@ -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",