From e46445cb5de7fd40f0a9965a7d84ce54aa4e97ff Mon Sep 17 00:00:00 2001 From: Jeff Moser Date: Sat, 27 Feb 2021 05:53:29 +0000 Subject: [PATCH] Add support for additional protoc arguments in Grpc.Tools (#25374) Using "optional" presence tracking in proto3 (before protobuf 3.15) required the `--experimental_allow_proto3_optional` protoc option but there was no existing Grpc.Tools feature that would allow specifying these arguments. This commit adds an optional `Protobuf.AdditionalProtocArguments` option that allows you to specify arbitrary protoc arguments. For example: ``` ``` Fixes #22975 --- .../ProtoCompileCommandLineGeneratorTest.cs | 9 +++++++++ src/csharp/Grpc.Tools/ProtoCompile.cs | 15 +++++++++++++++ .../build/_protobuf/Google.Protobuf.Tools.targets | 1 + 3 files changed, 25 insertions(+) diff --git a/src/csharp/Grpc.Tools.Tests/ProtoCompileCommandLineGeneratorTest.cs b/src/csharp/Grpc.Tools.Tests/ProtoCompileCommandLineGeneratorTest.cs index 5f6a53b6713..89c6b274e13 100644 --- a/src/csharp/Grpc.Tools.Tests/ProtoCompileCommandLineGeneratorTest.cs +++ b/src/csharp/Grpc.Tools.Tests/ProtoCompileCommandLineGeneratorTest.cs @@ -140,6 +140,15 @@ namespace Grpc.Tools.Tests Does.Contain("--grpc_opt=baz,quux")); } + [Test] + public void AdditionalProtocArguments() + { + _task.AdditionalProtocArguments = new[] { "--experimental_allow_proto3_optional" }; + ExecuteExpectSuccess(); + Assert.That(_task.LastResponseFile, + Does.Contain("--experimental_allow_proto3_optional")); + } + [Test] public void DirectoryArgumentsSlashTrimmed() { diff --git a/src/csharp/Grpc.Tools/ProtoCompile.cs b/src/csharp/Grpc.Tools/ProtoCompile.cs index 820cbfed9b2..b81656a5276 100644 --- a/src/csharp/Grpc.Tools/ProtoCompile.cs +++ b/src/csharp/Grpc.Tools/ProtoCompile.cs @@ -278,6 +278,12 @@ namespace Grpc.Tools /// public string[] OutputOptions { get; set; } + /// + /// Additional arguments that will be passed unmodified to protoc (and before any file names). + /// For example, "--experimental_allow_proto3_optional" + /// + public string[] AdditionalProtocArguments { get; set; } + /// /// Full path to the gRPC plugin executable. If specified, gRPC generation /// is enabled for the files. @@ -428,6 +434,15 @@ namespace Grpc.Tools } cmd.AddSwitchMaybe("dependency_out", DependencyOut); cmd.AddSwitchMaybe("error_format", "msvs"); + + if (AdditionalProtocArguments != null) + { + foreach (var additionalProtocOption in AdditionalProtocArguments) + { + cmd.AddArg(additionalProtocOption); + } + } + foreach (var proto in Protobuf) { cmd.AddArg(proto.ItemSpec); diff --git a/src/csharp/Grpc.Tools/build/_protobuf/Google.Protobuf.Tools.targets b/src/csharp/Grpc.Tools/build/_protobuf/Google.Protobuf.Tools.targets index fb7dc5fa03d..f06186896e1 100644 --- a/src/csharp/Grpc.Tools/build/_protobuf/Google.Protobuf.Tools.targets +++ b/src/csharp/Grpc.Tools/build/_protobuf/Google.Protobuf.Tools.targets @@ -288,6 +288,7 @@ GrpcPluginExe="%(_Protobuf_OutOfDateProto.GrpcPluginExe)" GrpcOutputDir="%(_Protobuf_OutOfDateProto.GrpcOutputDir)" GrpcOutputOptions="%(_Protobuf_OutOfDateProto._GrpcOutputOptions)" + AdditionalProtocArguments="%(_Protobuf_OutOfDateProto.AdditionalProtocArguments)" >