From 18febfa9946bc90be36020921e7cba44c4e1cca3 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 12 Aug 2024 06:16:53 -0700 Subject: [PATCH] build: run 'go mod tidy' as part of lint, asserting that it produced no file changes in go.mod/go.sum --- build/ci.go | 77 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/build/ci.go b/build/ci.go index db57633704..ede5fa257a 100644 --- a/build/ci.go +++ b/build/ci.go @@ -1,3 +1,6 @@ +//go:build none +// +build none + // Copyright 2016 The go-ethereum Authors // This file is part of the go-ethereum library. // @@ -13,10 +16,6 @@ // // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . - -//go:build none -// +build none - /* The ci command is called from Continuous Integration scripts. @@ -173,6 +172,8 @@ func main() { doSanityCheck() case "generate": doGenerate() + case "tidy": + doGoModTidy() default: log.Fatal("unknown command ", os.Args[1]) } @@ -349,10 +350,10 @@ func downloadSpecTestFixtures(csdb *build.ChecksumDB, cachedir string) string { return filepath.Join(cachedir, base) } -// hashSourceFiles iterates all files under the top-level project directory +// hashAllSourceFiles iterates all files under the top-level project directory // computing the hash of each file (excluding files within the tests // subrepo) -func hashSourceFiles() (map[string]common.Hash, error) { +func hashAllSourceFiles() (map[string]common.Hash, error) { res := make(map[string]common.Hash) err := filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error { if strings.HasPrefix(path, filepath.FromSlash("tests/testdata")) { @@ -379,6 +380,56 @@ func hashSourceFiles() (map[string]common.Hash, error) { return res, nil } +func hashSourceFiles(files []string) (map[string]common.Hash, error) { + if len(files) == 0 { + return hashAllSourceFiles() + } + res := make(map[string]common.Hash) + for _, filePath := range files { + f, err := os.OpenFile(filePath, os.O_RDONLY, 0666) + if err != nil { + return nil, err + } + hasher := sha256.New() + if _, err := io.Copy(hasher, f); err != nil { + return nil, err + } + res[filePath] = common.Hash(hasher.Sum(nil)) + } + return res, nil +} + +// compareHashedFilesets compares two maps (key is relative file path to top-level geth directory, value is its hash) +// and returns the list of file paths whose hashes differed. +func compareHashedFilesets(preHashes map[string]common.Hash, postHashes map[string]common.Hash) []string { + updates := []string{} + for path, postHash := range postHashes { + preHash, ok := preHashes[path] + if !ok || preHash != postHash { + updates = append(updates, path) + } + } + return updates +} + +func doGoModTidy() { + preHashes, err := hashSourceFiles([]string{"go.mod", "go.sum"}) + if err != nil { + log.Fatal("failed to hash go.mod/go.sum", "err", err) + } + tc := new(build.GoToolchain) + c := tc.Go("mod", "tidy") + build.MustRun(c) + postHashes, err := hashSourceFiles([]string{"go.mod", "go.sum"}) + updates := compareHashedFilesets(preHashes, postHashes) + for _, updatedFile := range updates { + fmt.Fprintf(os.Stderr, "changed file %s\n", updatedFile) + } + if len(updates) != 0 { + log.Fatal("go.sum and/or go.mod were updated by running 'go mod tidy'") + } +} + // doGenerate ensures that re-generating generated files does not cause // any mutations in the source file tree: i.e. all generated files were // updated and committed. Any stale generated files are updated. @@ -395,7 +446,7 @@ func doGenerate() { var preHashes map[string]common.Hash if *verify { var err error - preHashes, err = hashSourceFiles() + preHashes, err = hashAllSourceFiles() if err != nil { log.Fatal("failed to compute map of source hashes", "err", err) } @@ -410,17 +461,11 @@ func doGenerate() { return } // Check if files were changed. - postHashes, err := hashSourceFiles() + postHashes, err := hashAllSourceFiles() if err != nil { log.Fatal("error computing source tree file hashes", "err", err) } - updates := []string{} - for path, postHash := range postHashes { - preHash, ok := preHashes[path] - if !ok || preHash != postHash { - updates = append(updates, path) - } - } + updates := compareHashedFilesets(preHashes, postHashes) for _, updatedFile := range updates { fmt.Fprintf(os.Stderr, "changed file %s\n", updatedFile) } @@ -443,6 +488,8 @@ func doLint(cmdline []string) { linter := downloadLinter(*cachedir) lflags := []string{"run", "--config", ".golangci.yml"} build.MustRunCommandWithOutput(linter, append(lflags, packages...)...) + + doGoModTidy() fmt.Println("You have achieved perfection.") }