Skip to content

Commit

Permalink
Add support for additional protoc arguments in Grpc.Tools (#25374)
Browse files Browse the repository at this point in the history
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:

```
<Protobuf Include="**\*.proto" CompileOutputs="true" AdditionalProtocArguments="--experimental_allow_proto3_optional" />
```

Fixes #22975
  • Loading branch information
moserware committed Feb 27, 2021
1 parent d861ece commit e46445c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ public void GenerateGrpcWithOptions()
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()
{
Expand Down
15 changes: 15 additions & 0 deletions src/csharp/Grpc.Tools/ProtoCompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ public class ProtoCompile : ToolTask
/// </summary>
public string[] OutputOptions { get; set; }

/// <summary>
/// Additional arguments that will be passed unmodified to protoc (and before any file names).
/// For example, "--experimental_allow_proto3_optional"
/// </summary>
public string[] AdditionalProtocArguments { get; set; }

/// <summary>
/// Full path to the gRPC plugin executable. If specified, gRPC generation
/// is enabled for the files.
Expand Down Expand Up @@ -428,6 +434,15 @@ protected override string GenerateResponseFileCommands()
}
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@
GrpcPluginExe="%(_Protobuf_OutOfDateProto.GrpcPluginExe)"
GrpcOutputDir="%(_Protobuf_OutOfDateProto.GrpcOutputDir)"
GrpcOutputOptions="%(_Protobuf_OutOfDateProto._GrpcOutputOptions)"
AdditionalProtocArguments="%(_Protobuf_OutOfDateProto.AdditionalProtocArguments)"
>
<Output TaskParameter="GeneratedFiles" ItemName="_Protobuf_GeneratedFiles"/>
</ProtoCompile>
Expand Down

0 comments on commit e46445c

Please sign in to comment.