Alessio
Alessio

Reputation: 2068

Golang vendor package with vendored sub-packages

Situation:

Let's assume I've a package called mypackage. It exposes one method called Build(), that returns a concrete struct exposed by a third party library called thirdpartypackage, e.g. like shown as follows:

package mypackage

import tpp "github.com/thirdpartycompany/thirdpartypackage"

func Build() *tpp.SharedStruct{
   //...implementation
}

The package that exposes SharedStruct is vendored inside mypackage, because I want to distribute it and make that package independent.

Problem:

After importing mypackage into another project and using thirdpartypackage in my (integration) tests, I'm getting the following error:

cannot use XXXX (type "github.com/mycompany/mymainproject/vendor/github.com/mycompany/mypackage/vendor/github.com/thirdcompany/thirdpartypackage-go".Token) as type "github.com/empatica/mycompany/vendor/github.com/thirdcompany/thirdpartypackage"

Basically the compiler is distinguishing the vendored third-party package inside my library and the vendored package that I put in my main project.

Question:

Are there ways to solve this issue, that are not removing the vendored dependencies from my library, like suggested here? Am I missing something?

Upvotes: 2

Views: 2825

Answers (1)

Abhishek Soni
Abhishek Soni

Reputation: 1827

I think what you are missing is this point:

Code below a directory named "vendor" is importable only by code in the directory tree rooted at the parent of "vendor", and only using an import path that omits the prefix up to and including the vendor element. (go command)

What I understand from your problem is your mypackage has vendor as its child, so when it's imported by any other package at same level, then for obvious reason they cannot see that vendored list.

You can understand this as being similar to a protected feature.

So if you want to use that version in any other package, then this vendor should be present at same level. For example

ParentDirectory
 > mypackage
 > otherpackage (this will be importing mypackage)
 > vendor (vendor should be present in here so that all children under ParentDirectory can access this)

I hope I am clear with my point.

Upvotes: 3

Related Questions