Go: cmd/go: go mod tidy adds an indirect dependency and then `go mod why' tells I don't need it

Created on 15 Sep 2018  路  8Comments  路  Source: golang/go

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

go1.11 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/opennota/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/opennota/gocode"
GOPROXY=""
GORACE=""
GOROOT="/home/opennota/go"
GOTMPDIR=""
GOTOOLDIR="/home/opennota/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-build560781445=/tmp/go-build -gno-record-gcc-switches"

What did you do?

~/gocode/src/gitlab.com/opennota/fb2index $ GO111MODULE=on go mod tidy
go: finding google.golang.org/appengine/cloudsql latest

~/gocode/src/gitlab.com/opennota/fb2index $ cat go.mod 
module gitlab.com/opennota/fb2index

require (
        github.com/go-sql-driver/mysql v1.4.0 // indirect
        github.com/jmoiron/sqlx v0.0.0-20180614180643-0dae4fefe7c0
        github.com/lib/pq v1.0.0 // indirect
        github.com/mattn/go-sqlite3 v1.9.0
        golang.org/x/text v0.3.0
        google.golang.org/appengine v1.1.0 // indirect
)

~/gocode/src/gitlab.com/opennota/fb2index $ GO111MODULE=on go mod why google.golang.org/appengine
go: finding github.com/golang/protobuf/proto latest
go: finding golang.org/x/net/context latest
go: finding golang.org/x/net latest
# google.golang.org/appengine
(main module does not need package google.golang.org/appengine)

What did you expect to see?

go mod why shows why go mod tidy added google.golang.org/appengine to go.mod.

What did you see instead?

(main module does not need package google.golang.org/appengine)

FrozenDueToAge NeedsInvestigation modules

Most helpful comment

The code is here: https://gitlab.com/opennota/fb2index
I can try to minimize it tomorrow.
google.golang.org/appengine seems to be coming from /home/opennota/gocode/pkg/mod/github.com/go-sql-driver/[email protected]/appengine.go, but go mod why doesn't see this. go-sql-driver/mysql is itself an indirect dependency.

All 8 comments

In order to track this down, we're probably going to need an input that reproduces it.

Can you share a (hopefully minimal) set of Go source files that reproduce the problem?

The code is here: https://gitlab.com/opennota/fb2index
I can try to minimize it tomorrow.
google.golang.org/appengine seems to be coming from /home/opennota/gocode/pkg/mod/github.com/go-sql-driver/[email protected]/appengine.go, but go mod why doesn't see this. go-sql-driver/mysql is itself an indirect dependency.

Thanks, that's at least a good start. I assume it still reproduces at the current commit (ee9d8829)?

Yes.

Here is a simpler reproducer:

package a

import _ "github.com/jmoiron/sqlx"
go mod init example
go mod tidy
cat go.mod
go mod why google.golang.org/appengine

Looks like I was doing it wrong. go mod why -m google.golang.org/appengine does show why the module is in go.mod. Without -m it doesn't. go help mod why says that

Why shows a shortest path in the import graph from the main module to
each of the listed packages. If the -m flag is given, why treats the
arguments as a list of modules and finds a path to any package in each
of the modules.

The main module depends on google.golang.org/appengine/cloudsql, not on google.golang.org/appengine.

Thanks for the explanation. It's always good to find out what went wrong even when things are working as documented: if we notice a pattern we can at least improve the documentation.

Perhaps go mod why could detect when it is given a name present in go.mod without -m and print a warning.

Was this page helpful?
0 / 5 - 0 ratings