cmd/go: mod download swallows BitBucket git errors when VCS lookup fails

oymdgrw7  于 2个月前  发布在  Go
关注(0)|答案(2)|浏览(40)

你使用的Go版本是什么( go version )?

$ go version
go version go1.16.5 linux/amd64

这个问题在最新版本的发布中是否重现?

是的

你正在使用什么操作系统和处理器架构( go env )?

go env 输出

$ go env
GOARCH="amd64"
GOOS="linux"
GOPRIVATE=bitbucket.org

操作系统是Ubuntu 18.04。

你做了什么?

go mod download -x

你期望看到什么?

我期望看到 go.mod 中指定的模块被下载。

你看到了什么?

cd .
git ls-remote https://bitbucket.org/veea/golog
go: missing Mercurial command. See https://golang.org/s/gogetcmd
go: bitbucket.org/veea/golog@v2.0.0+incompatible: reading https://api.bitbucket.org/2.0/repositories/veea/golog?fields=scm: 403 Forbidden
	server response: Access denied. You must have write or admin access.

更多配置信息

我还设置了 git config --global url."git@bitbucket.org:".insteadOf "https://bitbucket.org"

原因

这里的根本问题是git问题,而不是go问题,我会在git上报告它,但是go处理git失败的方式非常不友好。
git失败是因为使用overlay挂载Map了一个git子模块,导致 .git 指定了一个不存在的 gitdir ,而这个子模块目录是当前工作目录。在这种情况下,任何git操作都会失败,即使它不需要你在那个时候处于git仓库中。例如,上面输出中的 git ls-remote 失败实际上是:

$ git ls-remote https://bitbucket.org/veea/golog
fatal: not a git repository: /home/tkcook/git/demo/VHP-3375-veeadb-on-vhe09/build/iesv10/programs/veeadb/../../../../../../.git/worktrees/VHP-3375-veeadb-on-vhe09/modules/node/platform/ies-os/programs/veeadb

请注意,如果当前工作目录是 $HOME ,相同的命令会成功:

$ cd ~
$ git ls-remote https://bitbucket.org/veea/golog
18587b1fa99c724154ca312c6a3ca001bfb63f1e	HEAD

但是,如上所述,这个失败是由 go mod download 报告的:

go: bitbucket.org/veea/golog@v2.0.0+incompatible: reading https://api.bitbucket.org/2.0/repositories/veea/golog?fields=scm: 403 Forbidden
	server response: Access denied. You must have write or admin access.

这使得它看起来像是 GOPRIVATE 设置被忽略了。

dhxwm5r4

dhxwm5r41#

cc @bcmills@jayconrod@matloob

s8vozzvw

s8vozzvw2#

失败的URL来自这里:
go/src/cmd/go/internal/vcs/vcs.go
第1275行到第1280行:

|  | url:=&urlpkg.URL{ |
|  | Scheme: "https", |
|  | Host: "api.bitbucket.org", |
|  | Path: expand(match, "/2.0/repositories/{bitname}"), |
|  | RawQuery: "fields=scm", |
|  | } |

获取该URL确实会因为go命令报告的错误而失败:

~/tmp$ curl -i https://api.bitbucket.org/2.0/repositories/veea/golog?fields=scm
HTTP/2 403
server: nginx
vary: Authorization, Origin
cache-control: max-age=900
content-type: text/plain
x-b3-traceid: f8d0d5e9089234da
x-usage-output-ops: 0
x-dc-location: Micros
strict-transport-security: max-age=31536000; includeSubDomains; preload
date: Wed, 21 Jul 2021 18:20:22 GMT
x-usage-user-time: 0.023356
x-usage-system-time: 0.000000
x-served-by: 7e942f486f54
x-view-name: bitbucket.apps.repo2.api.v20.repo.RepositoryHandler
etag: "b81e7e7393783ab8624c6b051083dce8"
x-static-version: 05d7ee4287f2
x-render-time: 0.0276589393616
x-accepted-oauth-scopes: repository
x-usage-input-ops: 0
x-version: 05d7ee4287f2
x-request-count: 1163
x-frame-options: SAMEORIGIN
x-cache-info: caching
content-length: 51

Access denied. You must have write or admin access.

如果它失败了403(这里是这种情况),go命令回退到分别尝试githg:

go/src/cmd/go/internal/vcs/vcs.go
第1284行到第1292行:

| | // this may be a private repository. If so, attempt to determine which |
| | // VCS it uses. See issue 5375. |
| | root:=match["root"] |
| | for_, vcs:=range []string{"git", "hg"} { |
| | ifvcsByCmd(vcs).Ping("https", root) ==nil { |
| | resp.SCM=vcs |
| | break |
| | } |
| | } |

这里的`git`尝试也失败了(由于上面提到的Git bug),而`hg`尝试失败是因为[BitBucket doesn't do Mecurial any more](https://bitbucket.org/blog/sunsetting-mercurial-support-in-bitbucket)。所以`go`命令有三个可能的错误要报告,它(任意地)选择了第一个。对我来说,很明显另外两个都不会有所帮助,但鉴于BitBucket甚至不再提供Mercurial仓库,也许我们应该删除所有源代码控制探测,只假设`git`,并在失败时仅报告`git`错误。

相关问题