解决 protoc-gen-go: 无法确定 Go 导入路径问题,通过添加一个 “M” 参数来解决。

huangapple go评论91阅读模式
英文:

Solving protoc-gen-go: unable to determine Go import path problem by adding a "M" argument

问题

https://stackoverflow.com/questions/70586511/protoc-gen-go-unable-to-determine-go-import-path-for-simple-proto 相同的症状。

对于以下内容的简单 proto 文件。

syntax="proto3";

package main;

message Person {
      string name = 1;
      int32 age = 2; 
}

我尝试使用 protoc 为其生成 Go 代码。我运行了以下命令:

protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative simple.proto

我收到以下错误:

protoc-gen-go: unable to determine Go import path for "simple.proto"

Please specify either:
        • a "go_package" option in the .proto source file, or
        • a "M" argument on the command line.

所有的答案都集中在第一个选项上——在.proto源文件中添加go_package选项,但我正在寻找第二个选项——在命令行上添加一个"M"参数的答案。

https://stackoverflow.com/a/62540631/2125837 下的评论相同。

我正在寻找通过protoc更改模块路径的方法,以便为属于不同模块的客户端和服务器生成 Go 代码。我尝试使用go_opt=module,但它与source_relative不兼容。

有没有办法通过在命令行上添加_"a 'M' argument"_来使其工作,而不是在.proto源文件中添加"go_package"选项?

具体来说,对于以下文件:
https://github.com/mmcc007/go/blob/master/examples/helloworld/helloworld/helloworld.proto

这是我的失败尝试:

$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --proto_path=examples/helloworld/helloworld helloworld.proto

protoc-gen-go: unable to determine Go import path for "helloworld.proto"
. . .
--go_out: protoc-gen-go: Plugin failed with status code 1.


$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --proto_path=examples/helloworld/helloworld --go_opt=Mhelloworld.proto=github.com/mmcc007/go/blob/master/examples/helloworld/helloworld helloworld.proto

protoc-gen-go-grpc: unable to determine Go import path for "helloworld.proto"
. . .
--go_out: protoc-gen-go: Plugin failed with status code 1.
英文:

Same symptom as https://stackoverflow.com/questions/70586511/protoc-gen-go-unable-to-determine-go-import-path-for-simple-proto

> For simple proto file with following content.
>
> syntax="proto3";
>
> package main;
>
> message Person {
> string name = 1;
> int32 age = 2;
> }
>
> I am trying to generate go code for it using protoc. I run:
>
> protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative simple.proto
>
> I receive following error:
>
> protoc-gen-go: unable to determine Go import path for "simple.proto"
>
> Please specify either:
> • a "go_package" option in the .proto source file, or
> • a "M" argument on the command line.

All the answer there focus on the first option -- adding a "go_package" option in the .proto source file, but I'm looking for the answer for the second option "a "M" argument on the command line".

Same as the comments under https://stackoverflow.com/a/62540631/2125837

I'm looking for ways to change the module path via protoc, to generate Go code for both a client and server that are part of different modules, I tried using go_opt=module but it doesn't work with source_relative.

Is there any ways to make it work by adding "a "M" argument on the command line", instead of adding a "go_package" option in the .proto source file please?

Specifically, for the file of
https://github.com/mmcc007/go/blob/master/examples/helloworld/helloworld/helloworld.proto

Here are my failed attempts:

$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --proto_path=examples/helloworld/helloworld helloworld.proto 

protoc-gen-go: unable to determine Go import path for "helloworld.proto"
. . .
--go_out: protoc-gen-go: Plugin failed with status code 1.


$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --proto_path=examples/helloworld/helloworld --go_opt=Mhelloworld.proto=github.com/mmcc007/go/blob/master/examples/helloworld/helloworld helloworld.proto 

protoc-gen-go-grpc: unable to determine Go import path for "helloworld.proto"
. . .
--go_out: protoc-gen-go: Plugin failed with status code 1.

答案1

得分: 7

我发现使用--proto_path和"THE M FLAG"可以使其工作,以便轻松引用:

protoc --proto_path=./proto \
  --go_out=./go \
  --go_opt=Mhelloworld.proto=example.com/project/protos/fizz \
  ./proto/helloworld.proto

注意:helloworld.proto之前的M。

英文:

I found this to be working using --proto_path for easy reference using "THE M FLAG" 解决 protoc-gen-go: 无法确定 Go 导入路径问题,通过添加一个 “M” 参数来解决。

protoc --proto_path=./proto \
  --go_out=./go \
  --go_opt=Mhelloworld.proto=example.com/project/protos/fizz \
  ./proto/helloworld.proto

Note: The M before helloworld.proto

答案2

得分: 2

你需要在命令或proto文件中添加路径。

syntax = "proto3";

option go_package = "pkg/api";

对于Java和Python也是一样的。

option java_package = "pkg/api";
option py_generic_services = "pkg/api";

至于命令部分

Makefile

PROJ_PATH=${CURDIR}

.PHONY: proto
proto: ## 生成protobuf代码
# 在项目内编译proto文件。
	protoc api.proto --proto_path=${PROJ_PATH}/proto --go_out=. --go-grpc_out=.
英文:

You need to add the path in the command or in the proto file

syntax = "proto3";

option go_package = "pkg/api";

this is the same for java, and python as well.

option java_package = "pkg/api";
option py_generic_services = "pkg/api";

As for the command

Makefile

PROJ_PATH=${CURDIR}

.PHONY: proto
proto: ## Generate protobuf code
# Compile proto files inside the project.
	protoc api.proto --proto_path=${PROJ_PATH}/proto --go_out=. --go-grpc_out=.

huangapple
  • 本文由 发表于 2022年5月4日 12:27:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/72107940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定