From 18febfa9946bc90be36020921e7cba44c4e1cca3 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 12 Aug 2024 06:16:53 -0700 Subject: [PATCH 1/6] 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 db576337043..ede5fa257af 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.") } From 043e85232567b3af12384eaacd9a47bda9c6d3eb Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 12 Aug 2024 06:20:17 -0700 Subject: [PATCH 2/6] remove 'tidy' ci.go subcommand --- build/ci.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/build/ci.go b/build/ci.go index ede5fa257af..3202c281921 100644 --- a/build/ci.go +++ b/build/ci.go @@ -172,8 +172,6 @@ func main() { doSanityCheck() case "generate": doGenerate() - case "tidy": - doGoModTidy() default: log.Fatal("unknown command ", os.Args[1]) } From b398df9e3bca44e649ab32174927c0482ff53edd Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 12 Aug 2024 06:26:06 -0700 Subject: [PATCH 3/6] move build none back to previous spot --- build/ci.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/build/ci.go b/build/ci.go index 3202c281921..983d6c0b178 100644 --- a/build/ci.go +++ b/build/ci.go @@ -1,6 +1,3 @@ -//go:build none -// +build none - // Copyright 2016 The go-ethereum Authors // This file is part of the go-ethereum library. // @@ -16,6 +13,10 @@ // // 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. From 726b00856e344bd0116b483814467476c849a792 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 12 Aug 2024 06:26:55 -0700 Subject: [PATCH 4/6] remove unused code path --- build/ci.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/build/ci.go b/build/ci.go index 983d6c0b178..5838d6b3ee9 100644 --- a/build/ci.go +++ b/build/ci.go @@ -380,9 +380,6 @@ func hashAllSourceFiles() (map[string]common.Hash, error) { } 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) From 9b7f91c9550b8ddcb0770e84a5be29b0187bd5cf Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 12 Aug 2024 06:28:06 -0700 Subject: [PATCH 5/6] function comment --- build/ci.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build/ci.go b/build/ci.go index 5838d6b3ee9..d605a3c6adc 100644 --- a/build/ci.go +++ b/build/ci.go @@ -379,6 +379,8 @@ func hashAllSourceFiles() (map[string]common.Hash, error) { return res, nil } +// hashSourceFiles iterates the provided set of filepaths (relative to the top-level geth project directory) +// computing the hash of each file. func hashSourceFiles(files []string) (map[string]common.Hash, error) { res := make(map[string]common.Hash) for _, filePath := range files { From 91aac5328d1c15bbba9dc77ef665b026ea4e1880 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 12 Aug 2024 06:44:28 -0700 Subject: [PATCH 6/6] dedup code --- build/ci.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/build/ci.go b/build/ci.go index d605a3c6adc..009d69e6e41 100644 --- a/build/ci.go +++ b/build/ci.go @@ -411,14 +411,15 @@ func compareHashedFilesets(preHashes map[string]common.Hash, postHashes map[stri } func doGoModTidy() { - preHashes, err := hashSourceFiles([]string{"go.mod", "go.sum"}) + targetFiles := []string{"go.mod", "go.sum"} + preHashes, err := hashSourceFiles(targetFiles) 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"}) + postHashes, err := hashSourceFiles(targetFiles) updates := compareHashedFilesets(preHashes, postHashes) for _, updatedFile := range updates { fmt.Fprintf(os.Stderr, "changed file %s\n", updatedFile)