Skip to content

Commit f5f8aff

Browse files
authored
Automatically deleting corrupted Go module cache (#2194)
**What type of PR is this?** Feature **What package or component does this PR mostly affect?** go_repository **What does this PR do? Why is it needed?** When the Go module sum mismatches, the most effective way is to delete the corrupted Go module, instead of deleting the whole Go module cache, which may contain many other unrelated modules. Because fetch_repo already know where the Go module is, it can automatically delete it and ask people to retry. **Other notes for review** New experience: ``` $ export GO_REPOSITORY_USE_HOST_CACHE=1 # intentionally corrupt a module $ rm $(go env GOMODCACHE)/github.com/mattn/[email protected]/static_mock.go $ bazel clean --expunge --async $ bazel fetch --force @com_github_mattn_go_sqlite3//:go-sqlite3 ... ERROR: com_github_mattn_go_sqlite3: fetch_repo: resulting module with sum h1:NlqjV/4w2ZBKwamqKNXcb6I12S9NTFOi18B0VlNju6g=; expected sum h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=. Please try again. If the problem persists, please try clearing your host module cache with `go clean -modcache` ```
1 parent a0a42d4 commit f5f8aff

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

cmd/fetch_repo/module.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
package main
1717

1818
import (
19+
"errors"
1920
"fmt"
2021
"os"
2122

@@ -65,10 +66,19 @@ func fetchModule(dest, importpath, version, sum string) error {
6566
}
6667

6768
if repoSum != sum {
69+
errMsg := fmt.Sprintf("resulting module with sum %s; expected sum %s.", repoSum, sum)
70+
// The module cache is corrupt. Remove the downloaded directory.
71+
if err := os.RemoveAll(dl.Dir); err != nil {
72+
errMsg += fmt.Sprintf(" Additionally, failed to remove corrupt module cache directory %q: %v. Please remove it manaully and retry.", dl.Dir, err)
73+
} else {
74+
errMsg += " Please retry."
75+
}
6876
if goModCache := os.Getenv("GOMODCACHE"); goModCache != "" {
69-
return fmt.Errorf("resulting module with sum %s; expected sum %s, Please try clearing your module cache directory %q", repoSum, sum, goModCache)
77+
errMsg += fmt.Sprintf(" If the problem persists, please try clearing your host module cache with `go clean -modcache`")
78+
} else {
79+
errMsg += fmt.Sprintf(" If the problem persists, please try clearing Bazel output directory with `bazel clean --expunge`")
7080
}
71-
return fmt.Errorf("resulting module with sum %s; expected sum %s", repoSum, sum)
81+
return errors.New(errMsg)
7282
}
7383

7484
return nil

0 commit comments

Comments
 (0)