在Go 1.7中使用嵌套的供应商目录。

huangapple go评论127阅读模式
英文:

Using nested vendor directories in go 1.7

问题

我有一个Go项目,其中包含以下包:

  1. go list ./... | grep -v /vendor/
  2. github.com/jab/JSes
  3. github.com/jab/JSes/src/common/config
  4. github.com/jab/JSes/src/common/logger
  5. github.com/jab/JSes/src/common/monitor
  6. github.com/jab/JSes/src/handlers/healthcheck
  7. github.com/jab/JSes/src/handlers/sessionuser
  8. github.com/jab/JSes/src/test/servicetest

所有外部依赖项都添加在vendor文件夹中:

  1. vendor/
  2. bitbucket.org
  3. m
  4. ses-ser

现在,ses-ser包含以下包:

  1. ses-ser
  2. vendor // 包含所有外部依赖项
  3. api
  4. constants
  5. exceptions
  6. idgen
  7. jsonDecoder
  8. log
  9. model
  10. monitor
  11. persistence
  12. redis
  13. routes
  14. src/bddtest/servicetest
  15. util

Makefile如下所示:

  1. .PHONY: deploy
  2. LOGLEVEL ?= 2
  3. GOFLAGS ?= $(GOFLAGS:)
  4. PKG = $(shell go list ./... | grep -v /vendor/ | tr "\n" " ")
  5. PWD = $(shell pwd)
  6. export GOPATH = $(PWD):$(shell echo $$GOPATH)
  7. export GOBIN = $(PWD)/bin
  8. export GOROOT = $(shell echo $$GOROOT)
  9. deploy: clean format build install conf
  10. build:
  11. @rm -rf pkg/ 2>/dev/null
  12. @rm -rf _libs/pkg/ 2>/dev/null
  13. @go build $(GOFLAGS) $(PKG)

但是在执行make build时,我遇到了以下错误:

  1. jab-MacBook-Pro-4:JSe debraj$ make build
  2. panic: runtime error: slice bounds out of range
  3. goroutine 1 [running]:
  4. panic(0x3e1b40, 0xc420010130)
  5. /usr/local/go/src/runtime/panic.go:500 +0x1a1
  6. main.vendoredImportPath(0xc4203df200, 0xc42037f301, 0x2f, 0x3, 0xc41fff4f00)
  7. /usr/local/go/src/cmd/go/pkg.go:463 +0x66c
  8. main.loadImport(0xc42037f301, 0x2f, 0xc4201605b0, 0x64, 0xc4203df200, 0xc4201c99e0, 0xc42022f680, 0x1, 0x1, 0x1, ...)
  9. /usr/local/go/src/cmd/go/pkg.go:333 +0x9ea
  10. main.(*Package).load(0xc4203df200, 0xc4201c99e0, 0xc420298e00, 0x0, 0x0, 0x4)
  11. /usr/local/go/src/cmd/go/pkg.go:940 +0x12b6
  12. main.loadImport(0xc420356f01, 0x29, 0xc4203203c0, 0x45, 0xc4203de480, 0xc4201c99e0, 0xc4203570e0, 0x1, 0x1, 0x1, ...)
  13. /usr/local/go/src/cmd/go/pkg.go:374 +0x470
  14. main.(*Package).load(0xc4203de480, 0xc4201c99e0, 0xc420293c00, 0x0, 0x0, 0x4)
  15. /usr/local/go/src/cmd/go/pkg.go:940 +0x12b6
  16. main.loadImport(0xc42019c6b1, 0xd, 0xc42019e7c0, 0x33, 0xc4201ac480, 0xc4201c99e0, 0xc420192e70, 0x1, 0x1, 0x1, ...)
  17. /usr/local/go/src/cmd/go/pkg.go:374 +0x470
  18. main.(*Package).load(0xc4201ac480, 0xc4201c99e0, 0xc420185180, 0x0, 0x0, 0xc)
  19. /usr/local/go/src/cmd/go/pkg.go:940 +0x12b6
  20. main.loadImport(0x7fff5fbff891, 0x1a, 0xc420018044, 0x33, 0x0, 0xc4201c99e0, 0x0, 0x0, 0x0, 0x0, ...)
  21. /usr/local/go/src/cmd/go/pkg.go:374 +0x470
  22. main.loadPackage(0x7fff5fbff891, 0x1a, 0xc4201c99e0, 0x0)
  23. /usr/local/go/src/cmd/go/pkg.go:1658 +0x28f
  24. main.packagesAndErrors(0xc420152c80, 0x7, 0x7, 0xc42019c520, 0x20, 0x3f3e60)
  25. /usr/local/go/src/cmd/go/pkg.go:1703 +0x337
  26. main.packagesForBuild(0xc42000c0b0, 0x7, 0x7, 0x493df8, 0xc420018180, 0x0)
  27. /usr/local/go/src/cmd/go/pkg.go:1719 +0x75
  28. main.runBuild(0x628540, 0xc42000c0b0, 0x7, 0x7)
  29. /usr/local/go/src/cmd/go/build.go:440 +0xd4
  30. main.main()
  31. /usr/local/go/src/cmd/go/main.go:181 +0x624
  32. make: *** [build] Error 2

这似乎与16656有关,该问题将在Go 1.8中修复。

作为一种解决方法,我尝试将所有外部依赖项放在_libs下,并在Makefile中修改了$GOPATH,如下所示:

  1. export GOPATH = $(PWD)/_libs:$(PWD)/_libs/src/bitbucket.org/m/ses-ser/vendor:$(PWD):$(shell echo $$GOPATH).

但是,然后它开始给我报错,如下所示:

  1. src/handlers/healthcheck/health_check.go:23: cannot use func literal (type func(*"github.com/valyala/fasthttp".RequestCtx)) as type func(*"bitbucket.org/m/ses-ser/vendor/github.com/valyala/fasthttp".RequestCtx) in field value

有人能提供解决此问题的方法吗?

环境:

  • go version go1.7.3 darwin/amd64

  • Mac OS X - Version 10.11.6

英文:

I have a go project having the following packages:-

  1. go list ./... | grep -v /vendor/
  2. github.com/jab/JSes
  3. github.com/jab/JSes/src/common/config
  4. github.com/jab/JSes/src/common/logger
  5. github.com/jab/JSes/src/common/monitor
  6. github.com/jab/JSes/src/handlers/healthcheck
  7. github.com/jab/JSes/src/handlers/sessionuser
  8. github.com/jab/JSes/src/test/servicetest

All external dependensies are added inside the vendor folder:-

  1. vendor/
  2. bitbucket.org
  3. m
  4. ses-ser

Now the ses-ser is having the following packages:-

  1. ses-ser
  2. vendor //Contains all the external dependencies
  3. api
  4. constants
  5. exceptions
  6. idgen
  7. jsonDecoder
  8. log
  9. model
  10. monitor
  11. persistence
  12. redis
  13. routes
  14. src/bddtest/servicetest
  15. util

The Makefile looks like below:-

  1. .PHONY: deploy
  2. LOGLEVEL ?= 2
  3. GOFLAGS ?= $(GOFLAGS:)
  4. PKG = $(shell go list ./... | grep -v /vendor/ | tr "\n" " ")
  5. PWD = $(shell pwd)
  6. export GOPATH = $(PWD):$(shell echo $$GOPATH)
  7. export GOBIN = $(PWD)/bin
  8. export GOROOT = $(shell echo $$GOROOT)
  9. deploy: clean format build install conf
  10. build:
  11. @rm -rf pkg/ 2>/dev/null
  12. @rm -rf _libs/pkg/ 2>/dev/null
  13. @go build $(GOFLAGS) $(PKG)

But on doing make build I am getting the below panic:-

  1. jab-MacBook-Pro-4:JSe debraj$ make build
  2. panic: runtime error: slice bounds out of range
  3. goroutine 1 [running]:
  4. panic(0x3e1b40, 0xc420010130)
  5. /usr/local/go/src/runtime/panic.go:500 +0x1a1
  6. main.vendoredImportPath(0xc4203df200, 0xc42037f301, 0x2f, 0x3, 0xc41fff4f00)
  7. /usr/local/go/src/cmd/go/pkg.go:463 +0x66c
  8. main.loadImport(0xc42037f301, 0x2f, 0xc4201605b0, 0x64, 0xc4203df200, 0xc4201c99e0, 0xc42022f680, 0x1, 0x1, 0x1, ...)
  9. /usr/local/go/src/cmd/go/pkg.go:333 +0x9ea
  10. main.(*Package).load(0xc4203df200, 0xc4201c99e0, 0xc420298e00, 0x0, 0x0, 0x4)
  11. /usr/local/go/src/cmd/go/pkg.go:940 +0x12b6
  12. main.loadImport(0xc420356f01, 0x29, 0xc4203203c0, 0x45, 0xc4203de480, 0xc4201c99e0, 0xc4203570e0, 0x1, 0x1, 0x1, ...)
  13. /usr/local/go/src/cmd/go/pkg.go:374 +0x470
  14. main.(*Package).load(0xc4203de480, 0xc4201c99e0, 0xc420293c00, 0x0, 0x0, 0x4)
  15. /usr/local/go/src/cmd/go/pkg.go:940 +0x12b6
  16. main.loadImport(0xc42019c6b1, 0xd, 0xc42019e7c0, 0x33, 0xc4201ac480, 0xc4201c99e0, 0xc420192e70, 0x1, 0x1, 0x1, ...)
  17. /usr/local/go/src/cmd/go/pkg.go:374 +0x470
  18. main.(*Package).load(0xc4201ac480, 0xc4201c99e0, 0xc420185180, 0x0, 0x0, 0xc)
  19. /usr/local/go/src/cmd/go/pkg.go:940 +0x12b6
  20. main.loadImport(0x7fff5fbff891, 0x1a, 0xc420018044, 0x33, 0x0, 0xc4201c99e0, 0x0, 0x0, 0x0, 0x0, ...)
  21. /usr/local/go/src/cmd/go/pkg.go:374 +0x470
  22. main.loadPackage(0x7fff5fbff891, 0x1a, 0xc4201c99e0, 0x0)
  23. /usr/local/go/src/cmd/go/pkg.go:1658 +0x28f
  24. main.packagesAndErrors(0xc420152c80, 0x7, 0x7, 0xc42019c520, 0x20, 0x3f3e60)
  25. /usr/local/go/src/cmd/go/pkg.go:1703 +0x337
  26. main.packagesForBuild(0xc42000c0b0, 0x7, 0x7, 0x493df8, 0xc420018180, 0x0)
  27. /usr/local/go/src/cmd/go/pkg.go:1719 +0x75
  28. main.runBuild(0x628540, 0xc42000c0b0, 0x7, 0x7)
  29. /usr/local/go/src/cmd/go/build.go:440 +0xd4
  30. main.main()
  31. /usr/local/go/src/cmd/go/main.go:181 +0x624
  32. make: *** [build] Error 2

This seems to be related to 16656 which will be fixed in Go 1.8.

As a workaround I tried to put all the external dependencies under _libs. And modified my $GOPATH in Makefile like below:-

  1. export GOPATH = $(PWD)/_libs:$(PWD)/_libs/src/bitbucket.org/m/ses-ser/vendor:$(PWD):$(shell echo $$GOPATH).

But then it started giving me error like below:-

  1. src/handlers/healthcheck/health_check.go:23: cannot use func literal (type func(*"github.com/valyala/fasthttp".RequestCtx)) as type func(*"bitbucket.org/m/ses-ser/vendor/github.com/valyala/fasthttp".RequestCtx) in field value

Can someone suggest someway of getting around this issue?

Environment:-

  • go version go1.7.3 darwin/amd64
  • Mac OS X - Version 10.11.6

答案1

得分: 1

尽管构建恐慌是一个已经修复的错误,但嵌套的供应商目录会导致包之间的类型冲突。

你的项目应该只有一个供应商目录。如果你的任何依赖项有自己的供应商目录,它们应该被剥离并合并到你的顶级供应商目录中。

专门用于供应商化依赖项的工具应该可以为你完成这个任务。

英文:

While the build panic was a bug which has been fixed, having nested vendor directories will cause type conflicts between packages.

You should only have a single vendor directory for your project. If any of your dependencies have vendor directories of their own, they should be stripped out and combined into your single top-level vendor directory.

Tools designed to vendor your dependencies should do this for you.

huangapple
  • 本文由 发表于 2017年1月12日 18:52:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/41611279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定