Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I import a pre-compiled proto from another package? #1602

Open
JoyCood opened this issue Mar 18, 2024 · 4 comments
Open

How can I import a pre-compiled proto from another package? #1602

JoyCood opened this issue Mar 18, 2024 · 4 comments

Comments

@JoyCood
Copy link

JoyCood commented Mar 18, 2024

#721 (comment)

hi, i follow the step, but it's not work! anyone would help me here?

the error is: can not import common package:

// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// 	protoc-gen-go v1.31.0
// 	protoc        v4.25.0
// source: order.proto

package order

import (
	common "../common"
	protoreflect "google.golang.org/protobuf/reflect/protoreflect"
	protoimpl "google.golang.org/protobuf/runtime/protoimpl"
	reflect "reflect"
	sync "sync"
)

Makefile:

GOPATH:=$(shell go env GOPATH)

.PHONY: proto
proto:
	@protoc -I. --proto_path=./proto/common --micro_out=./proto/common --go_out=:./proto/common code.proto
	@protoc -I. --proto_path=./proto/order --micro_out=./proto/order --go_out=:./proto/order order.proto

env:

$ protoc --version
libprotoc 25.0

$ protoc-gen-go --version
protoc-gen-go.exe v1.31.0

common.proto:

syntax="proto3";
package common;
option go_package="../common";

enum ErrorCode {
  SUCCESS = 0;
  CREATE_ORDER_FAILED = 40001;
}

order.proto:

syntax = "proto3";
package order;
option go_package = "../order";

import "proto/common/code.proto";

service OrderService {
  rpc CreateOrder(CreateOrderReq) returns(CreateOrderResp);
}

message CreateOrderReq {
}

message CreateOrderResp {
}
@puellanivis
Copy link
Collaborator

Try not to use relative paths for go package imports.

@JoyCood
Copy link
Author

JoyCood commented Mar 23, 2024

Try not to use relative paths for go package imports.

thanks your response, the relative paths is generate by protoc-gen-go, how to fixed this issues?

@puellanivis
Copy link
Collaborator

The relative paths come from your proto code:

option go_package = "../order";

@neild
Copy link
Contributor

neild commented Mar 23, 2024

The go_package option in your .proto file should be set to the full, non-relative import path of the Go package that will contain the generated code for that file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants