Skip to content

go/build: Import doesn't work as expected with modules #31821

Closed
@dimes

Description

@dimes

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 env

GOARCH="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/[email protected]/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/[email protected]/proto. Another peculiarity is that the omitted packages are pulled from Github Enterprise repos.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions