weichao
weichao

Reputation: 3381

Protobuf import "cannot find file" when use Golang1.11 with module

I'm use Golang 1.11 with module support, So my project is not put in $GOPATH

I am want to compile proto file,

My files structure

enter image description here


my TaskInfo.proto

syntax = "proto3";
package chaochaogege.filecatcher.common;

option go_package = "common";

import "chaochaogege.com/filecatcher/common/ChunkInfo.proto";

message TaskInfo{
    string name = 1;
    string storePath = 2;
    uint32 workersNum = 3;
    uint32 totalSize = 4;
    repeated chaochaogege.filecatcher.common.ChunkInfo chunkInfo = 5;
}

ChunkInfo.proto

syntax = "proto3";
package chaochaogege.filecatcher.common;

option go_package = "common";

message ChunkInfo{
    uint32 startBytes = 1;
    uint32 endBytes = 2;
    uint32 totalBytes = 3;
    uint32 downloaded = 4;
    uint32 orderId = 5;

}

go.mod

module chaochaogege.com/filecatcher

require (
  github.com/golang/protobuf v1.2.0
)

When I run follow (in filecatcher/common directory)

protoc --go_out=./ TaskInfo.proto

protoc said:

chaochaogege.com/filecatcher/common/ChunkInfo.proto: File not found.TaskInfo.proto: Import "chaochaogege.com/filecatcher/common/ChunkInfo.proto" was not found or had errors.
TaskInfo.proto:13:14: "chaochaogege.filecatcher.common.ChunkInfo" is notdefined.

I have googled, But all of questions are not about go module

Did I use wrong import path or My config is not correct?

If Stackoverflow cannot anwser my question, I think it's a bug. I should go to Github issue for report..

Upvotes: 1

Views: 7892

Answers (2)

George.
George.

Reputation: 1008

I know this question already has an accepted answer, but I wanted to share what worked for me, and to re-enforce one of the two proposals the accepted solution stated.

I discovered that regardless of where I was running the protoc command from (whether it was from the source of the project directory or the subdirectory containing the proto files), shortening the import path to just the file name (ie. ChunkInfo.proto) worked. Note that all my proto files were in the same subdirectory.

Importing the full path from the source of the project directory to the proto file has not worked for me, and I am not exactly sure why. Hope this helps.

Upvotes: 0

GreatDharmatma
GreatDharmatma

Reputation: 667

Looks like you are mixing up paths

Your import path is

import "chaochaogege.com/filecatcher/common/ChunkInfo.proto";

If you want to run protoc from inside filecatcher/common then you would want to shorten your import to just import "ChunkInfo.proto" and run the command you are trying to run now. It will work.

But if you want to keep your import statement as it is, then you would have to navigate to the parent directory of chaochaogege.com and run the following command from there as follows:

protoc -I chaochaogege.com/filecatcher/common/ chaogege.com/filecatcher/common/ChunkInfo.proto --go_out=chaochaogege.com/filecatcher/common/

Upvotes: 1

Related Questions