Search code examples
goprotocol-buffersimporterror

Undefined import from a separate package that is part of the same project


Here is the structure of a project/repo on GitHub:

|-grpc_definitions
|--v0
|---myservice.pb.go
|---myservice.proto
|-api

I forked the project and added some things locally; here is the new structure:

|-grpc_definitions
|--v0
|---myservice.pb.go
|---myservice.proto
|---new_service.pb.go
|---new_service.proto
|-api
|--myservice.go

In api/myservice.go I import the grpc definitions:

package api

import grpc_definitions_v0 "github.com/<username>/<repo>/grpc_definitions/v0"

var s grpc_definitions_v0.NewServiceServer // this line does not compile

var ss grpc_definitions_v0.ServiceServer // this line is fine

go.mod for api:

module github.com/<<owner>>/<<repo>>/api

go 1.14

require (
   github.com/<<owner>>/<<repo>>/grpc_definitions v0.0.0-20200723024905-6c479f56d135
...
)

If I look in new_service.pb.go, NewServiceServer is defined and it is part of the correct package. It imports everything that was already there just fine, only the stuff I added isn't imported properly.

If I go to the definition of grpc_definitions_v0.ServiceServer it looks like it's importing a specific commit of grpc_definitions_v0 instead of the local files.

How do I get my code to import the files I added/changed?

EDIT: I tried to go get from a commit with my changes in it, but since I'm working on a fork of the repo it says: module declares its path as: github.com/<<owner>>/<<repo>>/grpc_definitions but was required as: github.com/<<me>>/<<repo>>/grpc_definitions


Solution

  • I fixed this by using replace for go mod in the terminal:

    $ go mod edit -replace="github.com/<<owner>>/<<repo>>/grpc_definitions=/<<localpathtoclone>>/<<repo>>/grpc_definitions"
    

    This points it to use the local files. See more at: Using forked package import in Go