golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.66k stars 1.58k forks source link

Cannot switch to google.golang.org from your github verison! ) #1514

Closed comdiv closed 1 year ago

comdiv commented 1 year ago

All my team installs protoc and protoc-gen-go same way:

(OS: Linux Mint)

  1. install protoc from https://grpc.io/docs/protoc-installation/, now version v3.21.12
  2. install protoc-gen-go with go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28

All my teammates and CI got:

import(
   anypb "google.golang.org/protobuf/types/known/anypb"
)

but at my computer i got:

import (
    any1 "github.com/golang/protobuf/ptypes/any"
)

All checked - $GOPATH, $GOROOT, caches cleaned, mods cleaned - no existence of github.com/golang/... related staff on disk. Github version protoc-gen-go was removed in 2021, no mods or bins of it in disk.

So I switched to google.golang.org version of protoc-gen-go, but it still generates old Any reference, no Idea why. Have discussed it on https://stackoverflow.com/questions/75106683/invalid-proto-any-import-in-golang-protobuf-definiton-github-com-golang-vs-go

All suggestions was to cleanup $GOPATH, $PATH and so on. Especially interesting that it same for new accounts - created fresh account at same computer - install go, protoc, protoc-gen-go - still generates invalid ones!

What I missed?

comdiv commented 1 year ago

sudo find / -name "github" | grep proto - empty sudo find / -name "protoc" - only one `/usr/bin/protoc (3.21.12) sudo find / -name "protoc-gen-go":

~/go/pkg/mod/google.golang.org/protobuf@v1.28.1/cmd/protoc-gen-go
~/go/bin/protoc-gen-go

no idea why it could generate old type reference

puellanivis commented 1 year ago

What are your results from running protoc-gen-go --version?

puellanivis commented 1 year ago

I remember someone else having a similar problem, and searching through the issues, 🤔 this can be a complex set of issues. Could you run a sudo find / -name "anypb" and seeing if you have any local copies of that protobuf installed?

comdiv commented 1 year ago

protoc-gen-go --version -> protoc-gen-go v1.28.1

comdiv commented 1 year ago

find:

~/go/pkg/mod/google.golang.org/protobuf@v1.28.1/types/known/anypb
~/go/src/google.golang.org/protobuf/types/known/anypb
puellanivis commented 1 year ago

Hm, ok, try sudo find / -name "any.proto"

The only thing I can think here is that you have an any.proto installed somewhere, which is why I suggested the search for anypb but silly me, that’s not the directory the proto is from, but the directory it gets built into.

neild commented 1 year ago

Are you certain you're using protoc --go_out, and not --gogo_out or some other plugin? What's the exact protoc line?

One thought on verifying you're using the protoc-gen-go version you expect:

$ DIR=$(dirname `which protoc`)
$ echo $DIR
/usr/local/bin (or whatever directory contains protoc)

$ rm $DIR/protoc-gen-go
(remove any protoc-gen-go in that directory)

$ mkdir /tmp/out
$ PATH=$DIR protoc --go_out=/tmp/out your.proto
This should fail with an error that `protoc-gen-go` is not found, since it isn't in $PATH.

$ mkdir /tmp/bin
$ GOBIN=/tmp/bin go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
Install protoc-gen-go in /tmp.

$ PATH=$DIR:/tmp/bin protoc --go_out=/tmp/out your.proto
Use the newly-installed protoc.

$ /tmp/bin/protoc-gen-go --version
comdiv commented 1 year ago

echo $(dirnamewhich protoc) -> /usr/bin

protoc-gen-go not in same directory, it is at ~/go/bin

delete it

Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go_out: protoc-gen-go: Plugin failed with status code 1.

/tmp/bin/protoc-gen-go --version protoc-gen-go v1.28.1

But.... less my.pb.go

any1 "github.com/golang/protobuf/ptypes/any"

is it possible, that during install it uses not valid src from GOPATH/src?

comdiv commented 1 year ago

checked version of src - no src from github

neild commented 1 year ago

Check your copy of google/golang/any.proto? If protoc is in /usr/bin it should be in (I think) /usr/include/google/protobuf/any.proto and should include a line:

option go_package = "google.golang.org/protobuf/types/known/anypb";
comdiv commented 1 year ago

oops:

syntax = "proto3";

package google.protobuf;

option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option go_package = "github.com/golang/protobuf/ptypes/any";
option java_package = "com.google.protobuf";
option java_outer_classname = "AnyProto";
option java_multiple_files = true;
option objc_class_prefix = "GPB";
comdiv commented 1 year ago

catched, but how to fix?

neild commented 1 year ago

You should have gotten updated versions of the well-known type .proto files along with protoc. The binary distributions available from https://github.com/protocolbuffers/protobuf/releases all include them.

comdiv commented 1 year ago

Thank you very much! Replaced with new one from tar /src/google/protobuf and reinstall go install ... protoc-gen-go and now all work well. Don't know what was the moment of previous include dir creation and why it was not updated when i installed new versions before...