Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: package imported multiple times from GOPATH and vendor #28207

Closed
tiancaiamao opened this issue Oct 15, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@tiancaiamao
Copy link

commented Oct 15, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.11.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

➜  test go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/genius/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/media/genius/OS/project"
GOPROXY=""
GORACE=""
GOROOT="/media/genius/OS/project/go"
GOTMPDIR=""
GOTOOLDIR="/media/genius/OS/project/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build246817751=/tmp/go-build -gno-record-gcc-switches"

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

Package p imports q, q is in p's vendor.

➜  p cd $GOPATH/src/p
➜  p tree
.
├── p.go
└── vendor
    └── q
        └── q.go

2 directories, 2 files
➜  p cat p.go 
package p

import _ "q"

There is also a q package in GOPATH, i.e, q is in both GOPATH and p's vendor:

➜  p cd $GOPATH/src/q
➜  q cat q.go 
package q

import "net/http"

func init() {
	http.HandleFunc("/debug/requests", func(w http.ResponseWriter, req *http.Request) {})
}

I have a test.go, it imports both p and q:

package main

import (
	_ "p"
	_ "q"
)

func main() {}

Then package q will be import twice, and the init function run twice, results in the following error:

➜  test ./test   
panic: http: multiple registrations for /debug/requests

goroutine 1 [running]:
net/http.(*ServeMux).Handle(0x84b560, 0x67a277, 0xf, 0x6b7420, 0x68a390)
	/media/genius/OS/project/go/src/net/http/server.go:2377 +0x221
net/http.(*ServeMux).HandleFunc(0x84b560, 0x67a277, 0xf, 0x68a390)
	/media/genius/OS/project/go/src/net/http/server.go:2395 +0x5a
net/http.HandleFunc(0x67a277, 0xf, 0x68a390)
	/media/genius/OS/project/go/src/net/http/server.go:2407 +0x4b
q.init.0()
	/media/genius/OS/project/src/q/q.go:6 +0x42

What did you expect to see?

Go should handle packages in vendor and GOPATH, don't import them twice.

What did you see instead?

The same package been import twice and their init function conflicts.

@agnivade

This comment has been minimized.

Copy link
Member

commented Oct 15, 2018

@agnivade agnivade added this to the Go1.12 milestone Oct 15, 2018

@bcmills

This comment has been minimized.

Copy link
Member

commented Oct 15, 2018

This seems to be working as designed. If you don't want package p to have its own duplicate copy of q, then don't vendor q within p.

(Consider what would happen if some other package r also vendored in q at a conflicting version: which version would p end up using? Which version would q end up using?)

As far as I can tell, this is exactly one of the problems with vendoring that modules are intended to address.

@bcmills bcmills closed this Oct 15, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.