dousong3760 2019-05-03 20:46
浏览 62

go / types:Config.Check找不到包

I am using the go/types package for a tool that parses Go source. My code looks like this:

packageName := "github.com/something/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
    }
}

In the above example, I get an error from conf.Check even though my code builds fine. The error is:

panic: /Users/home/Dev/my-test-package/bindings/bindings.go:6:2: could not import github.com/something/my-test-package/prototype (type-checking package "github.com/something/my-test-package/prototype" failed (/Users/home/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/home/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/home/go/src/github.com/golang/protobuf/proto (from $GOPATH))))))))))))))))

The ultimate error is: could not import github.com/golang/protobuf/proto

If I run go list -f '{{ .Dir }}' github.com/golang/protobuf/proto from the same package, I get /Users/home/go/pkg/mod/github.com/golang/protobuf@v1.3.1/proto, so clearly the package is available. I posted a bug in the Golang repo, but thought I'd also post here in case I'm doing something obviously wrong.

  • 写回答

1条回答 默认 最新

  • dsozqcx9668 2019-05-03 21:41
    关注

    This seems to be a known issue, or related to a known issue: tools that use GOPATH need special updates to work with go modules.

    A few issues similar to yours have been linked to that issue.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值