ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.86k stars 295 forks source link

go.mod placement introduces an ambiguous import error #279

Closed bcmills closed 5 years ago

bcmills commented 5 years ago

The go.mod file introduced for #259 effectively splits the ugorji/go repository into two separate modules: github.com/ugorji/go and github.com/ugorji/go/codec.

A repository without a go.mod file is effectively a single module, so prior to the introduction of that file, the latest tagged version (v1.1.1) had all of the packages in module github.com/ugorji/go, and some existing users already depended on that module (https://github.com/etcd-io/etcd/blob/329be66e8b3f9e2e6af83c123ff89297e49ebd15/go.mod#L45).

Now, if some program combines those existing users with a user of the new module, they receive two distinct copies of each package (via the two distinct modules), and the build fails with an ambiguous import error:

scratch$ go mod init golang.org/issue/scratch
go: creating new go.mod: module golang.org/issue/scratch

scratch$ go get -m github.com/ugorji/go@latest
go: finding github.com/ugorji/go v1.1.1

scratch$ go get -m github.com/ugorji/go/codec@latest
go: finding github.com/ugorji/go/codec latest

scratch$ go list github.com/ugorji/go/...
go: downloading github.com/ugorji/go v1.1.1
go: extracting github.com/ugorji/go v1.1.1
go: downloading github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2
go: extracting github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2
can't load package: package github.com/ugorji/go/codec: unknown import path "github.com/ugorji/go/codec": ambiguous import: found github.com/ugorji/go/codec in multiple modules:
        github.com/ugorji/go v1.1.1 (/tmp/tmp.m35NK6Fpqr/_gopath/pkg/mod/github.com/ugorji/go@v1.1.1/codec)
        github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 (/tmp/tmp.m35NK6Fpqr/_gopath/pkg/mod/github.com/ugorji/go/codec@v0.0.0-20181209151446-772ced7fd4c2)
can't load package: package github.com/ugorji/go/codec/codecgen: unknown import path "github.com/ugorji/go/codec/codecgen": ambiguous import: found github.com/ugorji/go/codec/codecgen in multiple modules:
        github.com/ugorji/go v1.1.1 (/tmp/tmp.m35NK6Fpqr/_gopath/pkg/mod/github.com/ugorji/go@v1.1.1/codec/codecgen)
        github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 (/tmp/tmp.m35NK6Fpqr/_gopath/pkg/mod/github.com/ugorji/go/codec@v0.0.0-20181209151446-772ced7fd4c2/codecgen)

To resolve this problem, I recommend that you add the following line to github.com/ugorji/go/codec/go.mod:

require github.com/ugorji/go v1.1.2-0.20180831062425-e253f1f20942

Until golang/go#27899 is resolved, you may need to re-add the line whenever you run go mod tidy, or add another .go source file to ensure that it is not removed (as described in https://github.com/golang/go/issues/27899#issuecomment-432663636).

bcmills commented 5 years ago

As a workaround in the interim, anyone running into the ambiguous import error can apply the fix themselves by running

go get github.com/ugorji/go@v1.1.2-0.20180831062425-e253f1f20942
ugorji commented 5 years ago

why v1.1.2-XXX ? I am a big confused, and I don't want to make assumptions.

ugorji commented 5 years ago

As a workaround in the interim, anyone running into the ambiguous import error can apply the fix themselves by running

go get github.com/ugorji/go@v1.1.2-0.20180831062425-e253f1f20942

I don't understand why this works? There's no tag v1.1.2. Is this a git thing, and if so, can you explain?

I truly don't understand the quirks of modules yet. I just want to support a multi-module repo, which is what I had been wanting from the beginning, and this feels like the best time to get it setup. I want folks to treat github.com/ugorji/go/codec as a module in itself (not github.com/ugorji/go)

ugorji commented 5 years ago

This should be fixed now, as at 8fd0f8d . Please let me know if this is fixed for you.

bcmills commented 5 years ago

Thanks for the fix!

why v1.1.2-XXX ?

That's the pseudo-version at which the packages were removed from the root module, so that requirement ensures that only the github.com/ugorji/go/codec module provides those packages.

ugorji commented 5 years ago

Hi folks,

Please see https://github.com/ugorji/go/issues/299 and add your thoughts/ideas/etc. Thanks.

ugorji commented 5 years ago

FYI: I just released a go-codec production release - version 1.1.7 (finally)

First, it resolves the go.mod impasse where we had different import paths (github.com/ugorji/go and github.com/ugorji/go/codec) causing the ambiguous import error.

This is now fixed by leveraging import cycles to ensure that either one works well and resolves to the same bits.

Please let me know if seeing any issues. If all is well over the next few days, I will close this github issue.