go: x/tools/cmd/gotype: "could not import" packages

gotype fails to import local (not relative) packages within a Docker container.

Context go is installed in a Docker container, the base image is alpine:3.2. See the Dockerfile.

Steps 1. Mount the current path on the host dynamically in the container to fit in the GOPATH within the container: let’s say my PWD is ~/go/foo/bla on my host, then I run the following command:

$ docker run --rm \
  -v $PWD:/root/go/src/github.com/foo/bla \
  -w /root/go/src/github.com/foo/bla \
  -it mycompany/go bash

2. Run gotype . in the project folder (located in the GOPATH)

Then I get the following errors:

cli.go:8:2: could not import github.com/foo/bla/logger (can't find import: github.com/foo/bla/logger)
cli.go:9:2: could not import github.com/foo/bla/processor (can't find import: github.com/foo/bla/processor)
cli.go:125:2: undeclared name: logger
cli.go:130:4: undeclared name: logger
cli.go:133:3: undeclared name: logger
cli.go:136:7: undeclared name: processor

The project local dependencies can’t be imported. The other imports are OK though.

Environment

$ go version
go version go1.5.1 linux/amd64
$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT=""
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

The gotype tool assumes that you have recently done a “go install” of all dependency packages that are imported by the source files that it type checks, since it parses the pkg.a object code files produced by the compiler in order to obtain type information. (This limitation is unfortunate and not well advertised.)

(The golang.org/x/tools/go/loader package provides an alternative means of getting type information for dependencies, by loading and type checking the whole program from source. However, gotype doesn’t use it, not least because it is not in the standard library.)

FWIW, go test -i is a workaround for the import issue if the issue is with test sources. I just submitted a patch for this issue for the gometalinter tool.

re the fork repo, my needs are for regression testing only, so I have no need for the latest version in that repo. coala can add a deprecation notice, and/or wrap the stdlib version. Added https://github.com/coala/coala-bears/issues/1488 about that.