在编辑 Go 项目的时候,我用 dep ensure 总会遇到各种版本相关的错误,以前我的解决方式都是直接删掉版本有问题的库的全部依赖,然后直接重新 ensure,今天我又遇到这个问题了,但是我不准备这么做了,我尝试以一个 graceful way 去处理,下面简单说一下我的处理过程,不带个人思考以及根因定位,这些以后有时间再分享。

> dep ensure -update 
Solving failure: No versions of github.liqiang.io/testproject/blog met constraints:

查看 verbose log;

> dep ensure -update -v
Root project is "github.liqiang.io/testproject/blog"
 11 transitively valid internal packages
 44 external packages imported from 23 projects
(0)   ✓ select (root)
(1)     ? attempt github.com/asaskevich/govalidator with 1 pkgs; 12 versions to try
(1)         try github.com/asaskevich/govalidator@v9
... ...
(6)         master from (root)
(5)         try github.com/golang/mock@master
(5)     ✗   unable to update checked out version: fatal: reference is not a tree: 2105010f04ec0f4d7a7335a70547be132ebd7b7b
(5)       : command failed: [git checkout 2105010f04ec0f4d7a7335a70547be132ebd7b7b]: exit status 128
(5)         try github.com/golang/mock@go-modules
(6)     ✗   github.com/golang/mock@go-modules not allowed by constraint master:
(6)         master from (root)
(5)         try github.com/golang/mock@log-temp-remove-err
(6)     ✗   github.com/golang/mock@log-temp-remove-err not allowed by constraint master:
(6)         master from (root)
(5)       ← no more versions of github.com/golang/mock to try; begin backtrack
(4)     ← backtrack: no more versions of github.com/golang/protobuf to try
(3)     ← backtrack: no more versions of github.com/godbus/dbus to try
(2)     ← backtrack: no more versions of github.com/coreos/go-systemd to try
(1)     ← backtrack: no more versions of github.com/asaskevich/govalidator to try
  ✗ solving failed

Solver wall times by segment:
     b-list-versions: 14.990447225s
     b-source-exists:  3.151363379s
              b-gmal:  423.742825ms
         b-list-pkgs:  406.884814ms
         select-root:    4.688732ms
             satisfy:    1.024078ms
            unselect:      734.23µs
         select-atom:     726.134µs
            new-atom:     376.688µs
           backtrack:      77.343µs
  b-deduce-proj-root:      67.549µs
               other:      27.731µs

  TOTAL: 18.980160728s

进入到下载包的目录查看一下:

$ cd $GOPATH/pkg/dep/sources/https---github.com-golang-mock
$ git log --oneline

这个明显不是真正的 master 的代码,因为我看 Github 上的提交记录是这样的:

所以这里尝试手动切换到 master 看看:

$ git checkout master
$ git pull origin master

再次尝试一下 dep ensure -update,发现问题解决了,真牛逼。

这里有个问题就是为什么这个下载目录的代码版本会影响到 dep ensure,这个版本是什么时候切换的,这个留待后续观察。