golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.34k stars 17.7k forks source link

cmd/go: go mod why lies #31351

Closed mirtchovski closed 5 years ago

mirtchovski commented 5 years ago

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

go version go1.12.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/me/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/me/go"
GOPROXY=""
GORACE=""
GOROOT="/Users/me/sdk/go1.12.1"
GOTMPDIR=""
GOTOOLDIR="/Users/me/sdk/go1.12.1/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/me/orbital/go.mod"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sp/06p28g2d0vs7gd2vhf26wl9m0000gn/T/go-build849154512=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

$ grep bson go.mod 
$ grep labix go.mod
        labix.org/v2/mgo v0.0.0-20140701140051-000000000287 // indirect
$ go mod why labix.org/v2/mgo
# labix.org/v2/mgo
(main module does not need package labix.org/v2/mgo)
$ go mod why labix.org/v2/mgo/bson
# labix.org/v2/mgo/bson
/very/important/package
github.com/nats-io/go-nats-streaming
github.com/nats-io/go-nats-streaming.test
github.com/nats-io/nats-streaming-server/server
github.com/hashicorp/go-msgpack/codec
github.com/hashicorp/go-msgpack/codec.test
labix.org/v2/mgo/bson
$ go mod vendor
$ find vendor -name bson
$

What did you expect to see?

At least some mention of the package "bson" anywhere in go mod's output.

What did you see instead?

Zero mention of the package "bson" anywhere in go mod's output.

mirtchovski commented 5 years ago

also, there doesn't seem to be an import "bson" anywhere in the vendor/ directory:

$ find vendor -type f -print0 | xargs -0 grep bson
vendor/gopkg.in/yaml.v2/yaml.go:// The code in this section was copied from mgo/bson.

the problem is that the go mod tool always tries to download, cache, and reference this repository, even though there appears to be no need for it. this is causing grief primarily because it requires specialized tools to download (bzr).

bcmills commented 5 years ago

go mod vendor prunes out tests of dependencies. go mod why does not.

bcmills commented 5 years ago

@mirtchovski, the need for specialized tools will be mitigated by public module proxies. (And note that if labix.org/v2/mgo/bson isn't actually needed to resolve any direct imports in the packages you're building and testing, then you only need its go.mod file.)

go mod why seems to be giving you correct information: go mod why labix.org/v2/mgo searches for a package dependency, not a module dependency. (For the latter, you would want go mod why -m labix.org/v2/mgo.)

So it seems like this is, at best, a documentation issue. Did any particular document point you toward the use of go mod why? If so, perhaps we can clarify it.

mirtchovski commented 5 years ago

I believe I missed the -m from go mod why. With that it tells me exactly what I wanted. You can close this.

meling commented 4 years ago

This has bitten me many times now, and I find it confusing. How do I know whether or not something in my go.mod file is a package or module? My primary use of go mod why is to find out why go.mod contains a dependency; are there any other uses?

Whether or not my dependency is a package or module, is confusing to me (and probably others), and I would argue that the go mod why command should return a result either way. If the tool finds that the main module does not depend on the supplied package it should check if it depends on a corresponding module instead, obviating the need for the -m flag. Be helpful to users; make the default use case easy.

jayconrod commented 4 years ago

@meling This issue is closed and is no longer tracked. If you want to propose a change or report a bug, please open a new issue.