Reputation: 8310
I would like to understand the reason for the error I have experienced while brushing up on go modules & the vendor directory. Apparently, having the same package in the main module and the vendor folder results in an error and I would like to understand why. As far as I understand, if there is a go.mod
go
shouldn't even check the vendor
folder for dependencies. Once I run go run ./main.go
I get the following error:
main.go:3:8: ambiguous import: found package test/testpkg in multiple directories:
/Users/mic4ael/dev/mine/something-in-go/testpkg
/Users/mic4ael/dev/mine/something-in-go/vendor/test/testpkg
However, this doesn't happen when go build -mod=mod
is used to build a binary. I would appreciate explanation why this is the case.
GO111MODULE=""
$ tree
.
├── go.mod
├── main.go
├── test
├── testpkg
│ └── lib.go
└── vendor
└── test
└── testpkg
└── lib.go
go.mod
module test
go 1.15
main.go
package main
import "test/testpkg"
func main() {
testpkg.Echo("Test")
}
vendor/test/testpkg/lib.go
package testpkg
import "fmt"
func Echo(str string) {
fmt.Printf("From vendored package %s\n", str)
}
testpkg/lib.go
package testpkg
import "fmt"
func Echo(str string) {
fmt.Printf("From internal pkg: %s\n", str)
}
Upvotes: 0
Views: 739
Reputation: 51582
There are two packages with the same name, and that is the ambiguity. When you import test/testpkg
it can be imported from the project itself or from the vendor directory.
Using go modules does not change the vendoring behavior. In fact, you can use go mod vendor
to vendor modules locally. If a package appears under vendor/
it will be used from the vendored copy, otherwise it will be downloaded and used from the module cache. However, if you have a package in your project with the identical name as one of the packages under vendor, there is an ambiguity.
Upvotes: 2