简体   繁体   中英

Dependency issue within two projects that use same lib

I have two projects:

/myproject
/sharedproject

both of them are managed by dep, I have execute go get -u github.com/golang/dep/cmd/dep in order to have latest dep version, and run dep ensure on both projects.

When I run myproject I get following error:

cannot use op (type *"myproject/vendor/github.com/go-openapi/spec".Operation) as type *"sharedproject/vendor/github.com/go-openapi/spec".Operation

What is wrong and how to fix this?

Looks like the situation is that sharedproject vendors the github.com/go-openapi/spec dependency while myproject gets both sharedproject and github.com/go-openapi/spec dependencies from GOPATH .

Now when you refer to github.com/go-openapi/spec in sharedproject , it refers to the package inside the vendor directory, which is technically different from the same package in GOPATH , even if both have the same content. So when you pass a variable of type *github.com/go-openapi/spec.Operation from myproject to a function in sharedproject , the package of the type differs from what's expected and compilation fails.

To solve this, make sure sharedproject is vendored inside myproject . When you do this, dep ensure will put a copy of sharedproject without its vendor directory into myproject 's vendor directory. After this, both myproject and sharedproject will use the github.com/go-openapi/spec package from myproject 's vendor directory.

That does make local development hard if you change sharedproject often and want to immediately use those changes in myproject (can't use dep till the changes are pushed to the Git remote). I'd work around that by copying over sharedproject into myproject 's vendor directory manually without using dep (excluding the vendor directory of course). Be careful to not commit those manually copied changes to Git though!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM