grpc / grpc-go

The Go language implementation of gRPC. HTTP/2 based RPC
https://grpc.io
Apache License 2.0
21.08k stars 4.38k forks source link

protoc-gen-go-grpc: Running `protoc --go-grpc_out=. /path/to/file.proto` not generating message declarations #3750

Closed AyushG3112 closed 4 years ago

AyushG3112 commented 4 years ago

Please see the FAQ in our main README.md, then answer the questions below before submitting your issue.

protoc version?

3.7.1

go.mod contents?

module [REDACTED]

go 1.14

require (
    google.golang.org/grpc v1.30.0 // indirect
    google.golang.org/grpc/cmd/protoc-gen-go-grpc v0.0.0-20200716233830-6dc7938fe875 // indirect
)

What version of gRPC are you using?

google.golang.org/grpc v1.30.0 // indirect

What version of Go are you using (go version)?

go version go1.14 linux/amd64

What operating system (Linux, Windows, …) and version?

Ubuntu 18.04

What did you do?

For a sample proto file

syntax = "proto3";

option go_package="[REDACTED]/pbs";

message Book {
    int64 isbn = 1;
    string title = 2;
    string author = 3;
}

message GetBookRequest {
    int64 isbn = 1;
}

service BookService {
    rpc GetBook (GetBookRequest) returns (Book) {}
}

Run protoc --go-grpc_out=pbs /path/to/file.proto

I get the following generated file.

// Code generated by protoc-gen-go-grpc. DO NOT EDIT.

package pbs

import (
    context "context"
    grpc "google.golang.org/grpc"
    codes "google.golang.org/grpc/codes"
    status "google.golang.org/grpc/status"
)

// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion6

// BookServiceClient is the client API for BookService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type BookServiceClient interface {
    GetBook(ctx context.Context, in *GetBookRequest, opts ...grpc.CallOption) (*Book, error)
}

type bookServiceClient struct {
    cc grpc.ClientConnInterface
}

func NewBookServiceClient(cc grpc.ClientConnInterface) BookServiceClient {
    return &bookServiceClient{cc}
}

func (c *bookServiceClient) GetBook(ctx context.Context, in *GetBookRequest, opts ...grpc.CallOption) (*Book, error) {
    out := new(Book)
    err := c.cc.Invoke(ctx, "/BookService/GetBook", in, out, opts...)
    if err != nil {
        return nil, err
    }
    return out, nil
}

// BookServiceServer is the server API for BookService service.
// All implementations must embed UnimplementedBookServiceServer
// for forward compatibility
type BookServiceServer interface {
    GetBook(context.Context, *GetBookRequest) (*Book, error)
    mustEmbedUnimplementedBookServiceServer()
}

// UnimplementedBookServiceServer must be embedded to have forward compatible implementations.
type UnimplementedBookServiceServer struct {
}

func (*UnimplementedBookServiceServer) GetBook(context.Context, *GetBookRequest) (*Book, error) {
    return nil, status.Errorf(codes.Unimplemented, "method GetBook not implemented")
}
func (*UnimplementedBookServiceServer) mustEmbedUnimplementedBookServiceServer() {}

func RegisterBookServiceServer(s *grpc.Server, srv BookServiceServer) {
    s.RegisterService(&_BookService_serviceDesc, srv)
}

func _BookService_GetBook_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
    in := new(GetBookRequest)
    if err := dec(in); err != nil {
        return nil, err
    }
    if interceptor == nil {
        return srv.(BookServiceServer).GetBook(ctx, in)
    }
    info := &grpc.UnaryServerInfo{
        Server:     srv,
        FullMethod: "/BookService/GetBook",
    }
    handler := func(ctx context.Context, req interface{}) (interface{}, error) {
        return srv.(BookServiceServer).GetBook(ctx, req.(*GetBookRequest))
    }
    return interceptor(ctx, in, info, handler)
}

var _BookService_serviceDesc = grpc.ServiceDesc{
    ServiceName: "BookService",
    HandlerType: (*BookServiceServer)(nil),
    Methods: []grpc.MethodDesc{
        {
            MethodName: "GetBook",
            Handler:    _BookService_GetBook_Handler,
        },
    },
    Streams:  []grpc.StreamDesc{},
    Metadata: "a.proto",
}

What did you expect to see?

The generated file should declare of GetBookRequest and Book

What did you see instead?

Declarations of GetBookRequest and Book are missing

AyushG3112 commented 4 years ago

As a note, using protoc --go_out=plugins=grpc:. /path/to/file.proto.proto works fine.

menghanl commented 4 years ago

This is working as intended. The grpc codegen plugin (--go-grpc_out) only generates grpc code. The golang protobuf codegen (--go_out) generates the messages.

AyushG3112 commented 4 years ago

@menghanl Does that mean I'll need both protoc-gen-go and protoc-gen-go-grpc to generate my gRPC service definitions, and that protoc-gen-go is only deprecating the support for gRPC plugin and not messages?

menghanl commented 4 years ago

Yes.