go: go/build: Import doesn't work as expected with modules
What version of Go are you using (go version
)?
$ go version go version go1.12.4 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 envGOARCH=“amd64” GOBIN=“” GOCACHE=“/Users/maxgale/Library/Caches/go-build” GOEXE=“” GOFLAGS=“” GOHOSTARCH=“amd64” GOHOSTOS=“darwin” GOOS=“darwin” GOPATH=“/Users/maxgale/go” GOPROXY=“” GORACE=“” GOROOT=“/usr/local/go” GOTMPDIR=“” GOTOOLDIR=“/usr/local/go/pkg/tool/darwin_amd64” GCCGO=“gccgo” CC=“clang” CXX=“clang++” CGO_ENABLED=“1” GOMOD=“/Users/maxgale/Dev/my-test-package/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/pd/4cgwkjzx609_9yb4xdb2lfp80000gp/T/go-build151436703=/tmp/go-build -gno-record-gcc-switches -fno-common”
What did you do?
packageName := "github.com/dimes/my-test-package"
imported, err := build.Default.Import(packageName, ".", build.FindOnly)
if err != nil {
return nil, errors.Wrapf(err, "Error importing package %s", packageName)
}
packages, err := parser.ParseDir(fileSet, imported.Dir, nil, 0)
if err != nil {
return nil, errors.Wrapf(err, "Error parsing package %s", packageName)
}
for _, astPkg := range packages {
var files []*ast.File
for _, file := range astPkg.Files {
files = append(files, file)
}
info := &types.Info{
Defs: make(map[*ast.Ident]types.Object),
}
conf := types.Config{
Importer: importer.ForCompiler(fileSet, "source", nil),
}
_, err := conf.Check(packageName, fileSet, files, info)
if err != nil {
panic(err) // Panics here
}
}
What did you expect to see?
I expected no error from conf.Check
What did you see instead?
panic: /Users/maxgale/Dev/my-test-package/bindings/bindings.go:6:2: could not import github.com/dimes/my-test-package/prototype (type-checking package "github.com/dimes/my-test-package/prototype" failed (/Users/maxgale/Dev/my-test-package/prototype/basic.go:3:8: could not import <omitted> could not import github.com/golang/protobuf/ptypes/wrappers (type-checking package "github.com/golang/protobuf/ptypes/wrappers" failed (/Users/maxgale/go/pkg/mod/github.com/golang/protobuf@v1.2.0/ptypes/wrappers/wrappers.pb.go:6:14: could not import github.com/golang/protobuf/proto (cannot find package "github.com/golang/protobuf/proto" in any of:
/usr/local/go/src/github.com/golang/protobuf/proto (from $GOROOT)
/Users/maxgale/go/src/github.com/golang/protobuf/proto (from $GOPATH))))))))))))))))
However, if from the command line, I run go list -f '{{ .Dir }}' github.com/golang/protobuf/proto
I get /Users/maxgale/go/pkg/mod/github.com/golang/protobuf@v1.3.1/proto
. Another peculiarity is that the omitted packages are pulled from Github Enterprise repos.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 17 (10 by maintainers)
Ok, great. Thanks for the help. Using the
packages
package simplified things a lot:@dimes
x/tools/go/packages
should support your use case. Please let me know if it doesn’t so we can try to fix it.