在1.7中解决供应商的'找不到包'错误

时间:2017-01-11 11:12:31

标签: go

我的项目结构如下所示: -

session-service
    _libs   //Contains all the external dependencies
    api
    constants
    exceptions
    idgen
    jsonDecoder
    log
    model
    monitor
    persistence
    redis
    routes
    src/bddtest/servicetest
    util

_libs的内容如下所示: -

github.com 
golang.org 
    x
        net
gopkg.in

我的Makefile如下所示: -

.PHONY: deploy

LOGLEVEL ?= 1
CONFIGFILE ?= 2
GOFLAGS ?= $(GOFLAGS:)

PWD = $(shell pwd)
export GOPATH = $(shell echo $$GOPATH):$(PWD)/_libs:$(PWD)
export GOBIN = $(PWD)/bin
export GOROOT = $(shell echo $$GOROOT)

deploy: clean build install 

build:
    @rm -rf pkg/ 2>/dev/null
    @rm -rf _libs/pkg/ 2>/dev/null
    @go build $(GOFLAGS) ./...

install:
    @go install ./...

clean:
    @go clean $(GOFLAGS) -i ./...


## EOF

一切都很好。现在我想搬到vendor。所以我将_libs重命名为vendor并修改了我的Makefile,如下所示: -

export GOPATH = $(shell echo $$GOPATH):$(PWD)

但在此之后我开始收到以下错误: -

vendor/golang.org/x/net/html/charset/charset.go:20:2: cannot find package "golang.org/x/text/encoding" in any of:
        /Users/debraj/golang/src/b/m/session-service/vendor/golang.org/x/text/encoding (vendor tree)
        /usr/local/go/src/golang.org/x/text/encoding (from $GOROOT)
        /Users/debraj/golang/src/golang.org/x/text/encoding (from $GOPATH)
        /Users/debraj/golang/src/b/m/session-service/src/golang.org/x/text/encoding
    vendor/golang.org/x/net/html/charset/charset.go:21:2: cannot find package "golang.org/x/text/encoding/charmap" in any of:
        /Users/debraj/golang/src/b/m/session-service/vendor/golang.org/x/text/encoding/charmap (vendor tree)
        /usr/local/go/src/golang.org/x/text/encoding/charmap (from $GOROOT)
        /Users/debraj/golang/src/golang.org/x/text/encoding/charmap (from $GOPATH)
        /Users/debraj/golang/src/b/m/session-service/src/golang.org/x/text/encoding/charmap
    vendor/golang.org/x/net/html/charset/charset.go:22:2: cannot find package "golang.org/x/text/encoding/htmlindex" in any of:
        /Users/debraj/golang/src/b/m/session-service/vendor/golang.org/x/text/encoding/htmlindex (vendor tree)
        /usr/local/go/src/golang.org/x/text/encoding/htmlindex (from $GOROOT)
        /Users/debraj/golang/src/golang.org/x/text/encoding/htmlindex (from $GOPATH)
        /Users/debraj/golang/src/b/m/session-service/src/golang.org/x/text/encoding/htmlindex
    vendor/golang.org/x/net/html/charset/charset.go:23:2: cannot find package "golang.org/x/text/transform" in any of:
        /Users/debraj/golang/src/b/m/session-service/vendor/golang.org/x/text/transform (vendor tree)
        /usr/local/go/src/golang.org/x/text/transform (from $GOROOT)
        /Users/debraj/golang/src/golang.org/x/text/transform (from $GOPATH)
        /Users/debraj/golang/src/b/m/session-service/src/golang.org/x/text/transform
    vendor/golang.org/x/net/http2/h2i/h2i.go:38:2: cannot find package "golang.org/x/crypto/ssh/terminal" in any of:
        /Users/debraj/golang/src/b/m/session-service/vendor/golang.org/x/crypto/ssh/terminal (vendor tree)
        /usr/local/go/src/golang.org/x/crypto/ssh/terminal (from $GOROOT)
        /Users/debraj/golang/src/golang.org/x/crypto/ssh/terminal (from $GOPATH)
        /Users/debraj/golang/src/b/m/session-service/src/golang.org/x/crypto/ssh/terminal

环境: -

  • go go go7.7.3 darwin / amd64
  • Mac OS X 10.11.6

有人可以告诉我为什么我会在vendor收到上述错误,但是_libs一切正常吗?

更新

在我的本地,$(go list ./... | grep -v /vendor/)输出中的换行符导致了一些问题。所以要解决这个问题,我必须稍微修改jimb的解决方案。我在Makefile PKG = $(shell go list ./... | grep -v /vendor/ | tr "\n" " ")中引入了一个变量,然后在go install&中使用了该变量。 go build就像@go build $(GOFLAGS) $(PKG)

一样

1 个答案:

答案 0 :(得分:3)

^ABCD([^E]|$)目录以_libs开头,_工具会忽略该目录。将包移动到go时,vendor/通配符现在包含供应商目录中的所有包。

您应该明确列出要安装的软件包,而不是依赖./...通配符。如果您仍然需要通配符行为,则可以使用./...并过滤其路径中包含go list ./...目录的任何包。根据您的具体需求,这可能很简单:

vendor/