golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.12k stars 17.68k forks source link

cmd/go: git fetch errors dropped when producing pseudo-versions for commits #53935

Closed bcmills closed 2 years ago

bcmills commented 2 years ago
--- FAIL: TestScript (0.01s)
    --- FAIL: TestScript/mod_get_direct (158.26s)
        script_test.go:270: 
            # (2022-07-14T21:40:47Z)
            # Regression test for golang.org/issue/34092: with an empty module cache,
            # 'GOPROXY=direct go get golang.org/x/tools/gopls@master' did not correctly
            # resolve the pseudo-version for its dependency on golang.org/x/tools. (158.242s)
            > [short] skip
            > [!net] skip
            > [!exec:git] skip
            > env GO111MODULE=on
            > env GOPROXY=direct
            > env GOSUMDB=off
            > go list -m cloud.google.com/go@main
            [stdout]
            cloud.google.com/go v0.0.0-20220714214024-fc3417be70cd
            > ! stdout 'v0.0.0-'
            FAIL: testdata/script/mod_get_direct.txt:14: unexpected match for `(?m)v0.0.0-` found in stdout: v0.0.0-

FAIL
FAIL    cmd/go  262.233s

greplogs -l -e 'FAIL: TestScript/mod_get_direct .*(?:\n .*)*found in stdout: v0.0.0-' 2022-07-14T21:16:23-dc00aed/linux-amd64-longtest 2022-05-19T15:38:08-e23cc08/linux-386-longtest 2022-05-18T15:25:04-1f9f7db/linux-386-longtest

bcmills commented 2 years ago

This is probably a symptom of a missing error-check somewhere, combined with an intermittent flake connecting to cloud.google.com or its VCS host (compare #51051).

Independent of the flaky cloud.google.com dependency, we should find and fix the missing error check.

bcmills commented 2 years ago

A mod_invalid_version failure with a similar pathology: 2022-08-18T19:12:13-0eb56ca/linux-386-longtest

            > go mod edit -require github.com/pierrec/lz4@473cd7ce01a1
            > go list -m github.com/pierrec/lz4
            [stdout]
            github.com/pierrec/lz4 v0.0.0-20190131084431-473cd7ce01a1
            > stdout 'github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1'
            FAIL: testdata/script/mod_invalid_version.txt:186: no match for `(?m)github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1` found in stdout
gopherbot commented 2 years ago

Change https://go.dev/cl/426079 mentions this issue: cmd/go/internal/modfetch: report error on failing to derive pseudo version from recent tag

ZekeLu commented 2 years ago

I have managed to reproduce the issue with this script:

#!/bin/bash
for i in {1..10000}; do
    echo "$(date +%T): try $i times"
    echo "$(date +%T): try $i times ======" >> log.txt
    go clean -modcache && GOPROXY=direct GOSUMDB=off gotip list -x -m github.com/pierrec/lz4@473cd7ce01a1 >>log.txt 2>&1

    if tail -n 2 log.txt | grep -F v0.0.0; then
        echo "$(date +%T): reproduced"
        exit 0
    fi
done

The script is executed in the directory with the following files:

-- go.mod --
module example.com

go 1.14
-- go.sum --

Notes:

diff between a succeeded log and a failed log **Note**: The duration before `# cd` is modified manually to reduce the noice. ```diff --- succeeded.log 2022-08-27 10:48:19.000000000 +0800 +++ failed.log 2022-08-27 02:15:11.406606020 +0800 @@ -1,46 +1,40 @@ mkdir -p /home/zeke/go/pkg/mod/cache/vcs # git3 https://github.com/pierrec/lz4 # lock /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7.lock mkdir -p /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7 # git3 https://github.com/pierrec/lz4 cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git init --bare 0.001s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git init --bare cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git remote add origin -- https://github.com/pierrec/lz4 0.001s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git remote add origin -- https://github.com/pierrec/lz4 cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git -c log.showsignature=false log --no-decorate -n1 '--format=format:%H %ct %D' 473cd7ce01a1 -- 0.001s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git -c log.showsignature=false log --no-decorate -n1 '--format=format:%H %ct %D' 473cd7ce01a1 -- cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git ls-remote -q origin 34.014s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git ls-remote -q origin cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git tag -l 0.003s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git tag -l cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch -f --depth=1 origin refs/tags/v2.0.8:refs/tags/v2.0.8 129.970s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch -f --depth=1 origin refs/tags/v2.0.8:refs/tags/v2.0.8 cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git -c log.showsignature=false log --no-decorate -n1 '--format=format:%H %ct %D' refs/tags/v2.0.8 -- 0.003s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git -c log.showsignature=false log --no-decorate -n1 '--format=format:%H %ct %D' refs/tags/v2.0.8 -- cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git -c log.showsignature=false log --no-decorate -n1 '--format=format:%H %ct %D' 08c27939df1bd95e881e2c2367a749964ad1fceb -- 0.002s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git -c log.showsignature=false log --no-decorate -n1 '--format=format:%H %ct %D' 08c27939df1bd95e881e2c2367a749964ad1fceb -- cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch -f --depth=1 origin refs/tags/v1.0.1:refs/tags/v1.0.1 120.220s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch -f --depth=1 origin refs/tags/v1.0.1:refs/tags/v1.0.1 cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch -f origin 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' 239.611s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch -f origin 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch --unshallow -f origin 121.158s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch --unshallow -f origin cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git -c log.showsignature=false log --no-decorate -n1 '--format=format:%H %ct %D' 473cd7ce01a1113208073166464b98819526150e -- 0.005s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git -c log.showsignature=false log --no-decorate -n1 '--format=format:%H %ct %D' 473cd7ce01a1113208073166464b98819526150e -- cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git cat-file blob 473cd7ce01a1113208073166464b98819526150e:go.mod 0.003s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git cat-file blob 473cd7ce01a1113208073166464b98819526150e:go.mod cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git for-each-ref --format %(refname) refs/tags --merged 473cd7ce01a1113208073166464b98819526150e 0.007s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git for-each-ref --format %(refname) refs/tags --merged 473cd7ce01a1113208073166464b98819526150e cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch -f origin 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' -0.922s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch -f origin 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' +151.841s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch -f origin 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' -cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch --unshallow -f origin -4.288s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git fetch --unshallow -f origin -cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git for-each-ref --format %(refname) refs/tags --merged 473cd7ce01a1113208073166464b98819526150e -0.007s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git for-each-ref --format %(refname) refs/tags --merged 473cd7ce01a1113208073166464b98819526150e cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git cat-file blob 473cd7ce01a1113208073166464b98819526150e:go.mod 0.003s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git cat-file blob 473cd7ce01a1113208073166464b98819526150e:go.mod -cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git merge-base --is-ancestor -- v1.0.1 473cd7ce01a1113208073166464b98819526150e -0.003s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git merge-base --is-ancestor -- v1.0.1 473cd7ce01a1113208073166464b98819526150e cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git cat-file blob 473cd7ce01a1113208073166464b98819526150e:go.mod 0.002s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git cat-file blob 473cd7ce01a1113208073166464b98819526150e:go.mod cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git cat-file blob 473cd7ce01a1113208073166464b98819526150e:go.mod 0.001s # cd /home/zeke/go/pkg/mod/cache/vcs/036595127a116abca3f2bfd7bedafedb33d890c2b102d96361815966679432e7; git cat-file blob 473cd7ce01a1113208073166464b98819526150e:go.mod -github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1 +github.com/pierrec/lz4 v0.0.0-20190131084431-473cd7ce01a1 ```

From the log I found that git fetch --unshallow -f origin is not executed when it failed. Which could happen on line 558 below:

https://github.com/golang/go/blob/a0948493ac693b46b4124a4380fd044c6a05f1b3/src/cmd/go/internal/modfetch/codehost/git.go#L549-L570

And finally I found that the error is ignored on line 610 below:

https://github.com/golang/go/blob/a0948493ac693b46b4124a4380fd044c6a05f1b3/src/cmd/go/internal/modfetch/coderepo.go#L609-L614

I can reproduce the issue by modifying src/cmd/go/internal/modfetch/codehost/git.go like this:

diff --git a/src/cmd/go/internal/modfetch/codehost/git.go b/src/cmd/go/internal/modfetch/codehost/git.go
index ac2dc2348e..921f7d32cf 100644
--- a/src/cmd/go/internal/modfetch/codehost/git.go
+++ b/src/cmd/go/internal/modfetch/codehost/git.go
@@ -554,9 +554,7 @@ func (r *gitRepo) fetchRefsLocked() error {
        // golang.org/issue/34266 and
        // https://github.com/git/git/blob/4c86140027f4a0d2caaa3ab4bd8bfc5ce3c11c8a/transport.c#L1303-L1309.)

-       if _, err := Run(r.dir, "git", "fetch", "-f", r.remote, "refs/heads/*:refs/heads/*", "refs/tags/*:refs/tags/*"); err != nil {
-           return err
-       }
+       return fmt.Errorf("Hardcoded error")

        if _, err := os.Stat(filepath.Join(r.dir, "shallow")); err == nil {
            if _, err := Run(r.dir, "git", "fetch", "--unshallow", "-f", r.remote); err != nil {

https://go.dev/cl/426079 has been sent to fix this issue.

bcmills commented 2 years ago

@gopherbot, please backport to Go 1.18 and 1.19. This can cause the go command to resolve unexpected pseudo-versions for commits and branches, and the fix is very small.

gopherbot commented 2 years ago

Backport issue(s) opened: #54733 (for 1.18), #54734 (for 1.19).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.

gopherbot commented 2 years ago

Change https://go.dev/cl/426494 mentions this issue: [release-branch.go1.19] cmd/go/internal/modfetch: report error on failing to derive pseudo version from recent tag

gopherbot commented 2 years ago

Change https://go.dev/cl/426514 mentions this issue: [release-branch.go1.18] cmd/go/internal/modfetch: report error on failing to derive pseudo version from recent tag

gopherbot commented 2 years ago

Change https://go.dev/cl/426495 mentions this issue: cmd/go/internal/modfetch: distinguish "unsupported" errors from RecentTag