Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force go mod to accept a module that declares its path being different from its go.mod?

Tags:

When I run go mod tidy, it breaks because a package imported by my project imports another package using path github.com/coreos/bbolt, but when it fetches the package from this path its go.mod says its path is go.etcd.io/bbolt.

The problem is that both the importing package and the imported package are 3rd party packages. I know I could edit the go module cache to fix it, but it would be a real hell fixing it when new versions of these packages become available.

Partial echoed messages are shown below:

    github.com/coreos/etcd/client tested by
    github.com/coreos/etcd/client.test imports
    github.com/coreos/etcd/integration imports
    github.com/coreos/etcd/etcdserver imports
    github.com/coreos/etcd/mvcc/backend imports
    github.com/coreos/bbolt: github.com/coreos/[email protected]: parsing go.mod:
    module declares its path as: go.etcd.io/bbolt
            but was required as: github.com/coreos/bbolt

So, how can I fix or work around this situation?

like image 650
vuco Avatar asked Mar 25 '21 05:03

vuco


People also ask

What does Go mod tidy do?

go mod tidy ensures that the go. mod file matches the source code in the module. It adds any missing module requirements necessary to build the current module's packages and dependencies, if there are some not used dependencies go mod tidy will remove those from go.

What is indirect in Go mod file?

The go command automatically adds // indirect comments for some requirements. An // indirect comment indicates that no package from the required module is directly imported by any package in the main module.

What is Go version in Go mod?

The go. mod file includes an explicit require directive for each module that provides any package transitively imported by a package or test in the main module. (At go 1.16 and lower, an indirect dependency is included only if minimal version selection would otherwise select a different version.)


2 Answers

You can fix this solution by using the replace directive

Simply add:

replace github.com/coreos/bbolt v1.3.5 => go.etcd.io/bbolt v1.3.5

at the end of your go.mod file

like image 168
keser Avatar answered Oct 13 '22 01:10

keser


The mismatched path implies that your dependency (github.com/coreos/etcd/mvcc/backend) is written against an old version of the bbolt repository — one that predates commit e65d4d.


I notice that the current go.mod file in the github.com/etcd-io/etcd repo specifies its module path as go.etcd.io/etcd/v3.

So the most robust fix for you is probably to update to that path, which you can do by changing your import statements to refer to the new canonical import path and running go mod tidy to update your dependencies accordingly:

sed -i s,github.com/coreos/etcd,go.etcd.io/etcd/v3,g $(find . -name '*.go')
go mod tidy

Barring that, you could explicitly choose a version of github.com/coreos/bbolt that matches the older import path. I notice that the highest version for that module listed at https://beta.pkg.go.dev/github.com/etcd-io/bbolt?tab=versions is v1.3.3, and v1.3.4 does seem to add a go.mod file with the updated path. So as a fallback, you could try:

go get -d github.com/coreos/[email protected]

The downside to that approach is that v1.3.3 is the end of the line: you wouldn't be able to pull in bug-fixes after that point, because those fixes are all at the go.etcd.io path.

like image 43
bcmills Avatar answered Oct 13 '22 00:10

bcmills