尝试从不同目录导入proto文件时出现了找不到protoc文件的错误。

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

Protoc file not found error when trying to import proto file from different directory

问题

无法弄清楚为什么在运行protoc命令生成相关的go文件时会出现文件找不到错误。

问题:尝试找出在从不同目录导入proto文件时适当的protoc命令是什么。
我已经将我的proto路径设置为~/go-workspace/github.com/xyz/project/internal

结构

project
  - internal  
    - song
      - proto 
        - *.proto 
    - search
      - proto 
        - *.proto 

Song.proto

syntax = "proto3";
package song;

option go_package = "github.com/xyz/project/internal/song/proto";
......

Search.proto

syntax = "proto3";
package search;

option go_package = "github.com/xyz/project/internal/search/proto";

import "song/proto/song.proto";


Makefile:

generate:
	 protoc --go_out=. --go_opt=paths=source_relative \
		 --go-grpc_out=. --go-grpc_opt=paths=source_relative \
		 proto/*.proto

错误:

song/proto/song.proto: 文件未找到。
proto/search.proto:6:1: 导入 "song/proto/song.proto" 未找到或存在错误。
proto/search.proto:16:12: "song.Song" 未定义。
proto/search.proto:20:12: "song.Artist" 未定义。
英文:

Unable to figure out why I'm getting a file not found error when I'm running a protoc command to generate the relevant go files.

Problem: Trying to figure out what the appropriate protoc command is while trying to import a proto file from a different directory.
I've set my proto-path in GoLand to be ~/go-workspace/github.com/xyz/project/internal

Structure

project
  - internal  
    - song
      - proto 
        - *.proto 
    - search
      - proto 
        - *.proto 

Song.proto

syntax = "proto3";
package song;

option go_package = "github.com/xyz/project/internal/song/proto";
......

Search.proto

syntax = "proto3";
package search;

option go_package = "github.com/xyz/project/internal/search/proto";

import "song/proto/song.proto";


Makefile:

generate:
	 protoc --go_out=. --go_opt=paths=source_relative \
		 --go-grpc_out=. --go-grpc_opt=paths=source_relative \
		 proto/*.proto

Error:

song/proto/song.proto: File not found.
proto/search.proto:6:1: Import "song/proto/song.proto" was not found or had errors.
proto/search.proto:16:12: "song.Song" is not defined.
proto/search.proto:20:12: "song.Artist" is not defined.

答案1

得分: 2

如评论中所提到的,您可以使用protoc编译器的--proto_path-I选项,并且您需要以internal开头进行导入。

我可以看到的另一个解决方案,不需要多个Makefile,是将以下Makefile放在项目级别:

generate:
	protoc --go_out=. \
	--go_opt=module=github.com/xyz/project \
	internal/search/proto/*.proto \
	internal/song/proto/*.proto

请注意--go_opt=module,它将修剪您的包名称并帮助您在相应的proto目录中生成protobuf代码,并且请注意我将完整路径传递给proto目录。

然后,search.proto如下所示:

syntax = "proto3";

package search;

option go_package = "github.com/xyz/project/internal/search/proto";

import "internal/song/proto/song.proto";
英文:

As mentioned in a comment, you could use the --proto_path or -I option with the protoc compiler and you need to start your import with internal.

Another solution that I can see and that would not require multiple Makefiles, is putting the following Makefile at the project level:

generate:
	protoc --go_out=. \
	--go_opt=module=github.com/xyz/project \
	internal/search/proto/*.proto \
	internal/song/proto/*.proto

Note the the --go_opt=module that will trim your package name and help you generate the protobuf code inside the respective proto directories and note that I'm passing the entire paths to the proto directories.

and then search.proto like:

syntax = "proto3";

package search;

option go_package = "github.com/xyz/project/internal/search/proto";

import "internal/song/proto/song.proto";

huangapple
  • 本文由 发表于 2022年3月15日 06:07:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/71474769.html
匿名

发表评论

匿名网友

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

确定