-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Add support for additional protoc arguments in Grpc.Tools #25374
Add support for additional protoc arguments in Grpc.Tools #25374
Conversation
|
/// Additional options that will be passed directly to protoc. | ||
/// For example, "experimental_allow_proto3_optional" | ||
/// </summary> | ||
public string[] AdditionalProtocOptions { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to alternative parameter name suggestions. Also, I was unsure on if people should provide the --
prefix in the options. The current implementation automatically provides the --
prefix in order to encourage folks to not to forget it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am also open to making the option explicit to experimental_allow_proto3_optional
such as:
public bool ExperimentalAllowProto3Optional { get; set; }
The downside is that any future flags wouldn't be supported without a code update. The upside however is that it'd be safer (i.e. you couldn't inject arbitrary options)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having a way of passing arbitrary command line flags is useful. In fact even having a way of passing extra command line args (not necessarily --
flags) can be useful at time.
Of course the users would be doing this at their own risk but I think when you're passing arbitrary flags to your protoc invocation, that's kind of obvious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also technically, "Options" is not the best term here, since in protoc terminology the "options" is what gets passed to --csharp_opt=
(OutputOptions) and --grpc_opt=
(GrpcOutputOptions), not arbitrary command line flags.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jtattermusch I went ahead and renamed it to AdditionalProtocArguments
and made it so that I didn't automatically prefix it.
{ | ||
foreach (var additionalProtocOption in AdditionalProtocOptions) | ||
{ | ||
cmd.AddArg("--" + additionalProtocOption); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this automatic addition of "--" prefix is actually not very helpful as it can:
- can cause confusion in terms of whether you should pass the args with or without
--
while only offering very little extra convenience - it limits the generality (I can imagine in some use cases you'd want to add some extra arguments that are not necessarily cmdline flags to your protoc invocation and Grpc.Tools adding the extra
--
to everything would spoil that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and removed the automatic prefix.
/// Additional options that will be passed directly to protoc. | ||
/// For example, "experimental_allow_proto3_optional" | ||
/// </summary> | ||
public string[] AdditionalProtocOptions { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having a way of passing arbitrary command line flags is useful. In fact even having a way of passing extra command line args (not necessarily --
flags) can be useful at time.
Of course the users would be doing this at their own risk but I think when you're passing arbitrary flags to your protoc invocation, that's kind of obvious.
/// Additional options that will be passed directly to protoc. | ||
/// For example, "experimental_allow_proto3_optional" | ||
/// </summary> | ||
public string[] AdditionalProtocOptions { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also technically, "Options" is not the best term here, since in protoc terminology the "options" is what gets passed to --csharp_opt=
(OutputOptions) and --grpc_opt=
(GrpcOutputOptions), not arbitrary command line flags.
/// 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; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One concern is that this is a string array (one string per argument) rather than a single string (containing all arguments). I'm not sure if that'll make any practical difference in practice with how these get serialized to a "response" file that MSBuild creates. Regardless, I wanted to copy the style of other options that used arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the approach you used is the correct one.
@@ -140,6 +140,15 @@ public void GenerateGrpcWithOptions() | |||
Does.Contain("--grpc_opt=baz,quux")); | |||
} | |||
|
|||
[Test] | |||
public void AdditionalProtocOptions() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: rename the test method name to AdditionProtocArguments as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Fixed
Adhoc run of artifacts - packages - distribtest flow: https://fusion2.corp.google.com/invocations/52aa4ab0-ec46-40e2-a573-676d2efde755/targets (I'll post the results later). This will also produce a build of Grpc.Tools nupkg @moserware will you be able to test the adhoc nuget manually (I'll provide a link) and report back whether things are working as expected? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM pending green tests.
Thanks for the contribution!
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
@jtattermusch I went ahead and did a manual test by having a proto3 proto with an
I then added As discussed, the fact that it's a string array can cause a bit of a surprise. For example, if you pass:
you'll have To pass both parameters, you'd need to use the MSbuild list separator
This works and enables the experimental flag as well as outputs the descriptor set to Anything else you'd like me to test? |
@moserware can you test with the ad-hoc build of Grpc.Tools package: |
another distribtest build: https://fusion2.corp.google.com/invocations/926793c8-5b44-42dc-bdfe-61d74c2407cd/targets (the previous one has failed with a tool error). |
the tests themselves seem sufficient, but how did you obtain a build of Grpc.Tools nuget? (it's tricky to build) |
@jtattermusch I just tested it by copying
I then added the I repeated the test with a proto3 proto that had
It worked with
as well as with
which also generated the |
Thanks a lot for testing this! |
I'll merge after checking the distribtest results. |
Distribtest results: all C# distribtests are green except the dotnet5 distribtest, but it seems that the dotnet5 distribtest has failed for unrelated resons (nuget fetch): |
Hi, I've seen that |
It's in 2.37.0-pre1 (already available) and 2.37.0 (being pushed right now). |
Great! Thanks! |
Using "optional" presence tracking in proto3 (before protobuf 3.15)
required the
--experimental_allow_proto3_optional
protoc optionbut there was no existing Grpc.Tools feature that would allow specifying
these arguments.
This commit adds an optional
Protobuf.AdditionalProtocArguments
optionthat allows you to specify arbitrary protoc arguments. For example:
Fixes #22975
@jtattermusch