btcd: ambiguous import: found package github.com/btcsuite/btcd/chaincfg/chainhash in multiple modules

Hello,

I am using the beta version (btcd@v0.22.0-beta) and recently I got this error while building

github.com/btcsuite/btcd/btcec tested by
github.com/btcsuite/btcd/btcec.test imports
github.com/btcsuite/btcd/chaincfg/chainhash: ambiguous import: found package github.com/btcsuite/btcd/chaincfg/chainhash in multiple modules:
github.com/btcsuite/btcd v0.22.0-beta (/Users/madhurshrimal/go/pkg/mod/github.com/btcsuite/btcd@v0.22.0-beta/chaincfg/chainhash)
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0 (/Users/madhurshrimal/go/pkg/mod/github.com/btcsuite/btcd/chaincfg/chainhash@v1.0.0)

It didn’t use to come before and suddenly it popped up. Any idea how I can solve this?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 9
  • Comments: 51 (26 by maintainers)

Commits related to this issue

Most upvoted comments

The same here, what worked for me was forcing the version of btcd and go-ethereum at go.mod

module example

go 1.16

require (
	github.com/btcsuite/btcd v0.22.0-beta
	github.com/ethereum/go-ethereum v1.10.16	
)

Confirmed the issues are now resolved:

$ go mod init foo; go get github.com/btcsuite/btcd; go get github.com/btcsuite/btcd/btcec/v2; go get github.com/ethereum/go-ethereum
go: creating new go.mod: module foo
go: added github.com/aead/siphash v1.0.1
go: added github.com/btcsuite/btcd v0.22.1
go: added github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1
go: added github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
go: added github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
go: added github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd
go: added github.com/btcsuite/goleveldb v1.0.0
go: added github.com/btcsuite/snappy-go v1.0.0
go: added github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792
go: added github.com/btcsuite/winsvc v1.0.0
go: added github.com/davecgh/go-spew v1.1.1
go: added github.com/decred/dcrd/lru v1.0.0
go: added github.com/jessevdk/go-flags v1.4.0
go: added github.com/jrick/logrotate v1.0.0
go: added github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23
go: added golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
go: added github.com/btcsuite/btcd/btcec/v2 v2.1.3
go: added github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
go: added github.com/ethereum/go-ethereum v1.10.17
go: upgraded golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 => v0.0.0-20210322153248-0c34fe9e7dc2
go: upgraded golang.org/x/sys v0.0.0-20190412213103-97732733099d => v0.0.0-20210816183151-1e6c022a8912

Also the documentation is correct again as expected.

I was having the same issue building a downstream script that uses ethclient (four or five layers removed from btcsuite/btcd/chaincfg/chainhash).

github.com/myrepo/myrepo imports
        github.com/ethereum/go-ethereum/accounts/abi/bind imports
        github.com/ethereum/go-ethereum/crypto imports
        github.com/btcsuite/btcd/btcec/v2/ecdsa tested by
        github.com/btcsuite/btcd/btcec/v2/ecdsa.test imports
        github.com/btcsuite/btcd/chaincfg/chainhash: ambiguous import: found package github.com/btcsuite/btcd/chaincfg/chainhash in multiple modules:
        github.com/btcsuite/btcd v0.20.1-beta (/home/user/go/pkg/mod/github.com/btcsuite/btcd@v0.20.1-beta/chaincfg/chainhash)
        github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0 (/home/user/go/pkg/mod/github.com/btcsuite/btcd/chaincfg/chainhash@v1.0.0)

I solved it by forcing go.mod to resolve the package to the latest version (v1.0.0), by appending:

require github.com/btcd/chaincfg/chainhash v1.0.0 in my go.mod

Pretty sure the issue in gossamer is the require on the old btcutil module that requires an old btcd. I think you’ll want to change your btcutil import:

diff --git a/go.mod b/go.mod
index 50ff8f49..a13f3bfa 100644
--- a/go.mod
+++ b/go.mod
@@ -5,7 +5,7 @@ require (
        github.com/ChainSafe/go-schnorrkel v1.0.1-0.20220711122024-027d287d27bf
        github.com/OneOfOne/xxhash v1.2.8
        github.com/breml/rootcerts v0.2.6
-       github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
+       github.com/btcsuite/btcd/btcutil v1.1.2
        github.com/chyeh/pubip v0.0.0-20170203095919-b7e679cf541c
        github.com/cosmos/go-bip39 v1.0.0
        github.com/dgraph-io/badger/v2 v2.2007.4

and update your import paths too e.g.

--- a/dot/rpc/modules/system_integration_test.go
+++ b/dot/rpc/modules/system_integration_test.go
@@ -13,7 +13,7 @@
        "testing"
        "time"
 
-       "github.com/btcsuite/btcutil/base58"
+       "github.com/btcsuite/btcd/btcutil/base58"

etc

Glad you got this resolved @shrimalmadhur! I’m not entirely sure your go get is needed or doing what’s intended in your deps target given it’s purposed change in recent Go versions to specifically updating the go.mod rather than building anything, but as long as you’ve got things building without hacks, I’m very pleased with the resolution.

Pushed a new tag here after the merge of #1851: https://github.com/btcsuite/btcd/releases/tag/v0.22.1

All consumer now should be able update to that tag, and then freely use the new chainhash and btcec modules w/o the import ambiguity weirdness.

From looking into this a bit there are a few issues at play and the cleanest way to resolve them, basing on my experience with managing all of the dcrd modules for a few years now is that what @chappjc recapped is the best way to resolve the issue.

Namely, as can be seen in the module release workflow docs, versions without pre-release components are preferred over those with them. In other words, v0.22.0-beta is seen as before v0.18.1, so currently anyone just importing or doing a plain go get github.com/btcsuite/btcd is going to attempt to get v0.18.1 which is a broken version. Addressing that first issue implies that a new tag without a pre-release version on it is required.

Next, the latest pre-release tag v0.22.0-beta has chaincfg/chainhash as a package, but there is also now a separate module with the same path. This is 100% guaranteed to break consumers who try to use both v0.22.0-beta and anything that simultaneously uses the new module such as btcec/v2, which there are already consumers in the ecosystem that need to do that. Addressing that issue also requires a new tag that is seen as later than v0.18.1 in which the chaincfg/chainhash package has been removed.

Finally, since there are a lot of other breaking changes and changes that are not necessarily ready for a release, what is really needed is a new e.g. v0.23.0 tag that is code branched off of v0.22.0-beta with only the problematic package removed and the code updated to use the module instead.

That way v0.23.0 will be seen as the latest release module by consumers, it will not have the package that is causing conflicts in it, and it will not include a bunch of code that is not yet ready for release.

You could also opt for a v0.22.1, btw, since it will be seen as newer as well. The important parts are that:

  1. The new tag is not a pre-release version so that it is seen as the most recent proper release by the go tooling
  2. The new tag must use the new chaincfg/chainhash module and not contain the chaincfg/chainhash package
  3. The new tag does not contain code that is not ready to be released yet.

EDIT: Note that this approach will also fix the fact that https://pkg.go.dev/github.com/btcsuite/btcd is showing v0.14.6 and the incorrect README and all.

@richardbertozzo yea seems like it happens when I upgrade go-ethereum to v1.10.17 keeping the btcd version constant. I want to update to the newer version due to a security update https://github.com/coinbase/rosetta-sdk-go/security/dependabot/1

@erwanor adding that line to go.mod is still not helping my case.