0

I imported proto file (validator.proto) from one of my project https://github.com/maanasasubrahmanyam-sd/customValidation to another project (test.proto) https://github.com/maanasasubrahmanyam-sd/customeValTest/tree/master/interfaces/test_server

go get github.com/maanasasubrahmanyam-sd/customValidation

protoc  \
-I. \
-I $GOPATH/src/ \
--proto_path=${GOPATH}/pkg/mod/github.com/envoyproxy/protoc-gen-validate@v0.1.0 \
--proto_path=${GOPATH}/src/github.com/google/protobuf/src \
--go_out="plugins=grpc:./generated"  \
--validate_out="lang=go:./generated" \
--govalidators_out=. \
./interfaces/test_server/*.proto

The correct import should come as github.com/maanasasubrahmanyam-sd/customValidation/validator. But test.pb.go import was coming as _ "./validator" which shows red line in Goland.

EDIT - All pb.go files are showing errors in them. I suspect it is due to bad import.

I google it but did not found any relevant information. Any suggestion experts ?

Maana
  • 640
  • 3
  • 9
  • 22

1 Answers1

1

You can address the proto path in two ways,

One: if your import proto file is in your local then you should move it to your parent directory then address it from your parent path like this:

- parentDirectory
-- directory1
--- proto1.proto
-- importDirectory
--- proto2.proto

you can build this file(proto1.proto) with this command :

protoc --proto_path=parentDirectory/directory1 --proto_path=parentDirectory --go-grpc_out=***your output path*** --go_out=***your output path*** parentDirectory/directory1/proto1.proto

  • also if you use Goland you need to add parentDirectory to your setting(File | Settings | Languages & Frameworks | Protocol Buffers) then uncheck Configure automatically and add your parent path.

Two: If your proto file is in URL: then you can add it to your build command like this:

protoc --proto_path=src \
  --go_opt=Mprotos/buzz.proto=example.com/project/protos/fizz \
  --go_opt=Mprotos/bar.proto=example.com/project/protos/foo \
  protos/buzz.proto protos/bar.proto
ttrasn
  • 4,322
  • 4
  • 26
  • 43
  • I tried option 1 still it is not resolved. I moved validator.proto to parentDirectory and changed the build command as. protoc \ -I ./interfaces/test_server \ -I $GOPATH/src/ \ --proto_path=${GOPATH}/pkg/mod/github.com/envoyproxy/protoc-gen-validate@v0.1.0 \ --proto_path=${GOPATH}/src/github.com/google/protobuf/src \ --go_out="plugins=grpc:." \ --validate_out="lang=go:." \ --govalidators_out=. \ ./interfaces/test_server/test.proto – Maana Aug 26 '21 at 18:29
  • @Maana you don't need to use $GOPATH or sth like this. edit my sample protoc command – ttrasn Aug 26 '21 at 18:48
  • now getting file not found error. protoc \ --proto_path=. \ --proto_path=./interfaces/ \ --go-grpc_out=./generated \ --go_out=./generated \ ./interfaces/test_server/test.proto validate/validate.proto: File not found. github.com/maanasasubrahmanyam-sd/customValidation/validator.proto: File not found. interfaces/test_server/test.proto:4:1: Import "validate/validate.proto" was not found or had errors. interfaces/test_server/test.proto:7:1: Import "github.com/maanasasubrahmanyam-sd/customValidation/validator.proto" was not found or had errors. – Maana Aug 26 '21 at 19:09
  • @Maana your whole protoc command is wrong. – ttrasn Aug 26 '21 at 19:33
  • Sorry, I am new to Go grpc protobuf. – Maana Aug 26 '21 at 19:40
  • @Maana see this, I hope this can help you. https://stackoverflow.com/questions/63526898/cannot-resolve-import-in-proto-file – ttrasn Aug 26 '21 at 19:43