英文:
Resolving 'cannot find package' error with vendor in go 1.7
问题
我有一个项目结构,如下所示:-
session-service
_libs //包含所有外部依赖项
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 version go1.7.3 darwin/amd64
- Mac OS X 10.11.6
有人可以告诉我为什么我在 vendor
中遇到上述错误,但在 _libs
中一切正常吗?
更新
在我的本地环境中,$(go list ./... | grep -v /vendor/)
输出中的换行符引起了一些问题。所以为了解决这个问题,我稍微修改了 Makefile
中的 jimb
的解决方案。我在 Makefile
中引入了一个变量 PKG = $(shell go list ./... | grep -v /vendor/ | tr "\n" " ")
,然后在 go install
和 go build
中使用了该变量,如 @go build $(GOFLAGS) $(PKG)
。
英文:
I have a project structure which looks like below:-
session-service
_libs //Contains all the external dependencies
api
constants
exceptions
idgen
jsonDecoder
log
model
monitor
persistence
redis
routes
src/bddtest/servicetest
util
Content of _libs
looks like below:-
github.com
golang.org
x
net
gopkg.in
My Makefile
looks like below:-
.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
Everything is working fine. Now I am thinking of moving to vendor
. So I renamed my _libs
to vendor
and modified my Makefile
like below:-
export GOPATH = $(shell echo $$GOPATH):$(PWD)
But after this I started getting the following error:-
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
Environment:-
- go version go1.7.3 darwin/amd64
- Mac OS X 10.11.6
Can someone let me know why I am getting the above errors with vendor
but everything works fine with _libs
?
UPDATE
In my local the newlines in the output of $(go list ./... | grep -v /vendor/)
was causing some problem. So to resolve this I had to modify the jimb
's solution a bit. I introduced a variable in Makefile
PKG = $(shell go list ./... | grep -v /vendor/ | tr "\n" " ")
and then used that variable in go install
& go build
like @go build $(GOFLAGS) $(PKG)
答案1
得分: 4
_libs
目录以_
开头,并且被go
工具忽略。当你将包移动到vendor/
目录时,./...
通配符现在包括了vendor目录中的所有包。
你应该明确列出你想要安装的包,而不是依赖于./...
通配符。如果你仍然想要通配符的行为,你可以使用go list ./...
并过滤掉路径中包含vendor/
目录的任何包。根据你的具体需求,这可能只需要简单地执行以下命令:
go install $(go list ./... | grep -v vendor/)
英文:
The _libs
directory starts with _
, and is ignored by the go
tool. When you move the packages to vendor/
, the ./...
wildcard now includes all packages in the vendor directory.
You should explicitly list the package you want to install, rather than rely on the ./...
wildcard. If you still want the wildcard behavior, you can use go list ./...
and filter any package containing a vendor/
directory in their path. Depending on your specific needs, this could be as simple as:
go install $(go list ./... | grep -v vendor/)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论