-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
cmd/protoc-gen-go-grpc: add code generator #3453
Conversation
|
||
g.P("// ", clientName, " is the client API for ", service.GoName, " service.") | ||
g.P("//") | ||
g.P("// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.") |
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.
Should we make this link to go.dev
?
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.
Done.
cmd/protoc-gen-go-grpc/main.go
Outdated
// protoc-gen-go is a plugin for the Google protocol buffer compiler to generate | ||
// Go code. Install it by building this program and making it accessible within | ||
// your PATH with the name: | ||
// protoc-gen-go | ||
// | ||
// The 'go' suffix becomes part of the argument for the protocol compiler, | ||
// such that it can be invoked as: | ||
// protoc --go_out=paths=source_relative:. path/to/file.proto | ||
// | ||
// This generates Go bindings for the protocol buffer defined by file.proto. | ||
// With that input, the output will be written to: | ||
// path/to/file.pb.go | ||
// | ||
// See the README and documentation for protocol buffers to learn more: | ||
// https://developers.google.com/protocol-buffers/ | ||
package main |
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.
This looks wrong
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.
Oops, forgot to update the doc comment when copying this file. Fixed.
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.
Is this something that can be merged soon?
I decided to start a v2 of my protoreflect library, to update it to use the latest APIv2 for the protobuf runtime. But I quickly wound up stuck because the new protoc-gen-go
can't generate gRPC stuff. I am keen to start kicking the tires on these things.
I guess in the meantime, I'll just check out this pull request locally and try out the protoc-gen-go-grpc
command that way.
cmd/protoc-gen-go-grpc/go.mod
Outdated
@@ -0,0 +1,8 @@ | |||
module google.golang.org/grpc/cmd/protoc-gen-go-grpc | |||
|
|||
go 1.14 |
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 know Go 1.11 is not technically supported by the Go team anymore. But if AppEngine is still a supported target, it still has a Go 1.11 runtime. And Go 1.11 does not gracefully handle these go
version declarations.
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.
1.14 was inadvertent. Changed to 1.9, which is the oldest Go version supported by the protobuf module(s).
It's unfortunate that 1.9 is different than the version supported by the top-level gRPC module, but I think it's better (and possibly necessary) for this to remain in sync with the protobuf module which will import it.
Seems vet is failing this.
Begs the question, why use underscore in the package name, going against stated package naming guidelines? I was initially wondering if this worked similar to the This is also annoying when opening the project in VSCode, which in the default setup has vet running and calls these out as warnings... |
The intent was to have a clear visual indication to users that this isn't a package intended for general use. Running "internalgengogrpc" together makes the "internal" name fade into the background, for me at least. Changed to just "gengogrpc", since this package is less internal than it was when it lived in the |
@neild if you did not want to have it public, why didn't you just put it in subfolder like |
We want the To allow |
g.P(deprecationComment) | ||
} | ||
serviceDescVar := "_" + service.GoName + "_serviceDesc" | ||
g.P("func Register", service.GoName, "Server(s *", grpcPackage.Ident("Server"), ", srv ", serverType, ") {") |
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.
Would it be possible to have Register${service.GoName}Server
take an interface with RegisterService
on it instead of the concrete type? This would be useful in testing to allow registration in other objects.
I attempted this PR in the other protobuf repo, but it seems the princess might be in this castle instead golang/protobuf#1060
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.
This PR shouldn't introduce any changes from the current implementation; it moves where the generator is located, but not any of the semantics. Any changes to semantics should happen as a followup PR once this is merged.
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.
That's very reasonable. Do you know what the timeline on that is? I'm not looking for a date, O(days), O(weeks), or O(months)? I'm trying to scope of I can postpone my project, or if I should temporarily apply patches to the compiler in my repo.
Also, I'm so excited for the V2 API. It looks like you've done a wonderful job. Thanks for all your hard work!
Is anyone here. Sorry, because many people are waiting, it is very painful |
This is on my plate. Sorry for the delay - I'm behind on many things right now, not just this. I hope to get to it soon. |
Hopefully this can get in this week! |
Is this really a blocker? We bumped our github.com/golang/protobuf/protoc-gen-go plugin to v1.4.0 and are generating v2 proto and gRPC stubs. It should not be necessary to block on this PR to start using the v2 APIs in gRPC setups. Just bump github.com/golang/protobuf/protoc-gen-go to 1.4.0 and run with |
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.
Sorry for the excessive delay in reviewing.
// TODO: Remove this. We don't need to include these references any more. | ||
g.P("// Reference imports to suppress errors if they are not otherwise used.") | ||
g.P("var _ ", contextPackage.Ident("Context")) | ||
g.P("var _ ", grpcPackage.Ident("ClientConnInterface")) | ||
g.P() |
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.
Why is this a TODO, then?
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.
Removed.
g.P("// Unimplemented", serverType, " can be embedded to have forward compatible implementations.") | ||
g.P("type Unimplemented", serverType, " struct {") |
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.
Is this an acceptable time to change behavior and require this to be embedded for all services? Or is it required that no changes are needed in your code when switching from v1 to v2 proto?
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.
Up to you and whatever your compatibility guarantees are. This would, of course, be a breaking change which would require that all users change their code before using the new generator.
If we were to leave the generator in github.com/golang/protobuf
unchanged and independent of the one here, then this might be a reasonable time to make a breaking change--since this is a brand new generator, it has no existing users. On the other hand, doing so will serve as an additional barrier for users migrating from the old one.
Either way, my preference would be to submit this CL with the existing API to provide a baseline identical to the current version and for you to make whatever changes you want in a followup change.
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.
This would be a pretty good opportunity to do this, but it's probably not reasonable to require code changes in order to run the generator from a new location. But if we check this code in without that requirement, then I don't think we'll ever be able to do it.
// This package is exported to permit github.com/golang/protobuf/protoc-gen-go | ||
// to depend on it. No other packages should depend on 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.
So the plan is to delete the code there and reference this code instead?
This has pros and cons:
- Pro: v1 and v2 codegen do not need to be maintained separately
- Con: changes here will impact the v1 codegen, meaning we need to test the v1 codegen (in the other repo) if we want to make changes here.
Might it be better to just have separate implementations and allow them to drift? (Also: clearly marking the v1 codegen as deprecated and no longer receiving new features.)
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.
My plan had been to delete the generator from github.com/golang/protobuf
and reference the one here. If you'd rather we deprecate that one and let it drift, I'm fine with that approach. What's your preference?
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.
Not sure that the con is as significant, since github.com/golang/protobuf
can specify that they depend on a specific version of the generator herein. Since v1 is not meant to be receiving new features, they should not advance to newer versions of the grpc gen, except for important bug fixes that advance only the minor version number. This should reduce the need for cross-repo testing to those bug fixes that are deemed necessary to backport to v1. That said, I think both strategies are fine.
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'd rather let them drift and mark the old one as deprecated. Then we don't need to export code from here that is internal-only, which I see as a win. Even though anyone else would be crazy to use it, if we were to change it, it would be a breaking change for the code in the proto repo referencing it.
Even with this merged, it is unreleased (v0.0) and subject to change. We do plan to add a requirement that the Unimplemented service implementation be included in all registered services, with an escape hatch in the form of a command-line arg as suggested by @neild. |
So happy to finally see this merged, great work! |
@dfawley - is there an issue I can use to track the release? |
Let's use #3669 to track this work for now. |
@dfawley is that the correct issue number? That issue looks unrelated, unless I misunderstand something? |
Somewhat related: would you consider moving the code-generation parts of this out of package |
@jeffwidman yes, that's the right one :) That's the only thing blocking a 1.0 release of the tool.
Probably not, but it would be interesting to hear your use case(s). I'd rather not have to maintain a separate library for this, guaranteeing backward compatibility forever, unless it's important to do so. |
As the e.g. something like a A caller would have two opportunities to adapt the output:
|
Interesting. This is a very high level function. Since it's so high level, it should be possible to accomplish these things as a separate step before/after running the generator as a binary. But on the other hand, it would be possible to support this kind of API with minimal effort. Would you mind filing an issue for this? |
I'm not certain what this means. The We do not support modifying the code generated by |
You can modify the generated Go identifiers in |
As mentioned in another comment, mutation of data structures returned by |
Then why does the |
Because other things need read-only access to them are there is no language feature for immutability. |
Perhaps your team should add some documentation to |
The protogen package doesn't produce generated code. We don't provide any public packages which produce generated protocol buffer code. It's used by the tools which produce generated code (protoc-gen-go, protoc-gen-go-grpc). |
We’re well aware. We use https://github.com/alta/protopatch |
Commit `f18fcd9b` updated the following dependencies: - github.com/golang/protobuf: v1.3.2 => v1.4.2 - google.golang.org/genproto - google.golang.org/grpc: v1.26.0 => v1.27.0 However, the protobuf generated files were not re-generated which is what this commit addresses. The newly generated files should be backward compatible. However, the following warning is now emitted when generating them: WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated. A future release of golang/protobuf will delete this package, which has long been excluded from the compatibility promise. This is because this package was apparently never meant to be public as explained in this comment[0]. The fix for this is to migrate to `google.golang.org/protobuf/compiler/protogen` but this can't be done until a release of `grpc-go` which includes the following patch[1] is out. Update the Makefile to output a note with regard to this when generating the files It appears that with the newly generated files, protobuf now needs to be marked as an explicit dep. Fix it by by running: go mod tidy && go mod vendor && go mod verify Fix: f18fcd9 [0]: golang/protobuf#1104 (comment) [1]: grpc/grpc-go#3453 Signed-off-by: Robin Hahling <[email protected]>
Commit `f18fcd9b` updated the following dependencies: - github.com/golang/protobuf: v1.3.2 => v1.4.2 - google.golang.org/genproto - google.golang.org/grpc: v1.26.0 => v1.27.0 However, the protobuf generated files were not re-generated which is what this commit addresses. The newly generated files should be backward compatible. However, the following warning is now emitted when generating them: WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated. A future release of golang/protobuf will delete this package, which has long been excluded from the compatibility promise. This is because this package was apparently never meant to be public as explained in this comment[0]. The fix for this is to migrate to `google.golang.org/protobuf/compiler/protogen` but this can't be done until a release of `grpc-go` which includes the following patch[1] is out. Update the Makefile to output a note with regard to this when generating the files It appears that with the newly generated files, protobuf now needs to be marked as an explicit dep. Fix it by by running: go mod tidy && go mod vendor && go mod verify Fix: f18fcd9 [0]: golang/protobuf#1104 (comment) [1]: grpc/grpc-go#3453 Signed-off-by: Robin Hahling <[email protected]>
Add a code generator for gRPC services.
No tests included here. A followup PR updates code generation to use
this generator, which acts as a test of the generator as well.