I have two dependencies in my project.
go.mod
:
module github.com/test-org/test-repo
go 1.12
require (
github.com/foo/bar v1.0.0
github.com/raz/mataz v1.0.0
)
After running go mod download
, those two dependencies result in two different versions of a github.com/shared/dependency
to be downloaded. The interesting thing is that the github.com/shared/dependency
contains sub-modules, e.g.:
dependency
-- go.mod
-- api
-- go.mod
Inspecting the downloaded modules shows two versions are downloaded to my local machine:
ls ${GOPATH}/pkg/mod/github.com/shared
:
[dir] dependency [dir] [email protected]
ls ${GOPATH}/pkg/mod/github.com/shared/dependency
:
[dir] [email protected]
Looking at the contents of these directories:
${GOPATH}/pkg/mod/github.com/shared/[email protected]
:
The file contents of the whole repo in v1.1.0, including the api
folder with its own go.mod
file.
${GOPATH}/pkg/mod/github.com/shared/dependency/[email protected]
:
The file contents of the api
folder of the repo in v1.2.0, including the go.mod
file.
Finally, I have a .go
file in my test-repo
with the following setup:
package test-package
import (
"github.com/foo/bar"
)
func MyFunc() {...bar.NewBar()...}
When I try to run a test of MyFunc
(that exists elsewhere), I get an unknown import path...ambiguous import...
error message. e.g.
go test github.com/test-org/test-repo/test-package -test.run=TestMyFunc -v
:
unknown import path "github.com/shared/dependency/api": ambiguous import: found github.com/shared/dependency/api in multiple modules:
github.com/shared/dependency v1.1.0 (${GOPATH}/pkg/mod/github.com/shared/[email protected]/api)
github.com/shared/dependency v1.2.0 (${GOPATH}/pkg/mod/github.com/shared/dependency/[email protected])
The error points to the import
line of the .go
file importing github.com/shared/dependency/api
in the github.com/foo/bar
repo. It doesn't know which api
to choose in my local ${GOPATH}/pkg/mod
folder, given the two versions available:
${GOPATH}/pkg/mod/github.com/shared/[email protected]/api
${GOPATH}/pkg/mod/github.com/shared/dependency/[email protected]
Is there any way that I can make that go test
call work (solve the dependency conflicts)? Neither of my two dependencies explicitly call out downloading the full shared/[email protected]
, but for some reason it gets pulled in. If that weren't there, it would seemingly fix the issue.
v1.12
and other isv1.0
, that is the same major version, thus thev1.12
must be compatible withv1.0
, and so the go tool will choosev1.12
which should work. If you need a different major versions, starting withv2
it must be part of the import path, thus it will count as a different package / dependency, and all different major version will be included separately. – Wingardv1
vs.v2
, but withv0
vs.v1
. So your suggestion doesn't apply. I was unaware of thev2
+ specifics you mentioned, though. Good to know. – Decaliterv0.x.x
version of the package, and the other dependency requires av1.x.x
version, the only solution is to change the requirement that one of those dependencies has in itsgo.mod
file? Is that correct? Initially with the go modules thing, I had the impression that sub-modules might be able to get away with different versions. For example, if I made a secondgo.mod
file in my repo for my package that needed the other version of the dependency, I thought that might work, but I could be way off course with what go modules are capable of. – Decaliterv0.x.x
vs.v1.x.x
, but the other is with av1.4.x
vs. av1.1.x
. Thus, the expected go tool behavior of automatically selecting doesn't appear to be working in this case. Note that this might be an edge case, as it involves importing a sub-module for one of those two versions (v1.4.x
andv1.1.x
). – Decaliter