So I am working on a project using proto buff where I need to generate golang code using .proto files. My issue is that it is importing wrong package when I generate golang code.
So this is how my project structure looks:-
myproject/
├── protos/
│ ├── models/
│ │ ├── ad.proto
│ │ └── enums.proto
│ └── requests/
│ └── ad_request.proto
The "ad_request.proto" file requires some models or messages from "ad.proto" and "enums.proto" files For that I imported these files and used those messages like this-
syntax = "proto3";
package my_project.requests;
import "models/ad.proto";
import "models/enums.proto";
option go_package = "github.com/name/my_project/protos/requests;requests";
so now after I generate code, a go file is made - "ad_requests.pb.go"
But the problem is that it is importing wrong package
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc (unknown)
// source: requests/ad_request.proto
package requests
import (
models "github.com/name/my_project/protos/models"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
instead of importing github.com/name/my_project/models it is importing file from proto folder but there is no golang files in proto folder
Anyone know how to resolve this issue
This is some info on ad.proto
syntax = "proto3";
package my_project.models;
import "google/protobuf/timestamp.proto";
import "models/enums.proto";
option go_package = "github.com/name/my_project/protos/models;models";
This is the make command I use to auto generate code
.PHONY: gen proto-gen
gen proto-gen:
buf generate protos --template protos/buf.gen.yaml
protoc-go-inject-tag -input=./internal/models/*.pb.go -remove_tag_comment
protoc-go-inject-tag -input=./internal/requests/*.pb.go -remove_tag_comment
protoccommand are you using?option go_packagelines to point to the right place, where you want to do the imports from. Sooption go_package = github.com/name/my_project/modelsfor example.