Skip to content

Commit bc95452

Browse files
authored
build: run 'go mod tidy' check as part of lint (#30291)
1 parent ab03c57 commit bc95452

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

build/ci.go

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,10 @@ func downloadSpecTestFixtures(csdb *build.ChecksumDB, cachedir string) string {
349349
return filepath.Join(cachedir, base)
350350
}
351351

352-
// hashSourceFiles iterates all files under the top-level project directory
352+
// hashAllSourceFiles iterates all files under the top-level project directory
353353
// computing the hash of each file (excluding files within the tests
354354
// subrepo)
355-
func hashSourceFiles() (map[string]common.Hash, error) {
355+
func hashAllSourceFiles() (map[string]common.Hash, error) {
356356
res := make(map[string]common.Hash)
357357
err := filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error {
358358
if strings.HasPrefix(path, filepath.FromSlash("tests/testdata")) {
@@ -379,6 +379,56 @@ func hashSourceFiles() (map[string]common.Hash, error) {
379379
return res, nil
380380
}
381381

382+
// hashSourceFiles iterates the provided set of filepaths (relative to the top-level geth project directory)
383+
// computing the hash of each file.
384+
func hashSourceFiles(files []string) (map[string]common.Hash, error) {
385+
res := make(map[string]common.Hash)
386+
for _, filePath := range files {
387+
f, err := os.OpenFile(filePath, os.O_RDONLY, 0666)
388+
if err != nil {
389+
return nil, err
390+
}
391+
hasher := sha256.New()
392+
if _, err := io.Copy(hasher, f); err != nil {
393+
return nil, err
394+
}
395+
res[filePath] = common.Hash(hasher.Sum(nil))
396+
}
397+
return res, nil
398+
}
399+
400+
// compareHashedFilesets compares two maps (key is relative file path to top-level geth directory, value is its hash)
401+
// and returns the list of file paths whose hashes differed.
402+
func compareHashedFilesets(preHashes map[string]common.Hash, postHashes map[string]common.Hash) []string {
403+
updates := []string{}
404+
for path, postHash := range postHashes {
405+
preHash, ok := preHashes[path]
406+
if !ok || preHash != postHash {
407+
updates = append(updates, path)
408+
}
409+
}
410+
return updates
411+
}
412+
413+
func doGoModTidy() {
414+
targetFiles := []string{"go.mod", "go.sum"}
415+
preHashes, err := hashSourceFiles(targetFiles)
416+
if err != nil {
417+
log.Fatal("failed to hash go.mod/go.sum", "err", err)
418+
}
419+
tc := new(build.GoToolchain)
420+
c := tc.Go("mod", "tidy")
421+
build.MustRun(c)
422+
postHashes, err := hashSourceFiles(targetFiles)
423+
updates := compareHashedFilesets(preHashes, postHashes)
424+
for _, updatedFile := range updates {
425+
fmt.Fprintf(os.Stderr, "changed file %s\n", updatedFile)
426+
}
427+
if len(updates) != 0 {
428+
log.Fatal("go.sum and/or go.mod were updated by running 'go mod tidy'")
429+
}
430+
}
431+
382432
// doGenerate ensures that re-generating generated files does not cause
383433
// any mutations in the source file tree: i.e. all generated files were
384434
// updated and committed. Any stale generated files are updated.
@@ -395,7 +445,7 @@ func doGenerate() {
395445
var preHashes map[string]common.Hash
396446
if *verify {
397447
var err error
398-
preHashes, err = hashSourceFiles()
448+
preHashes, err = hashAllSourceFiles()
399449
if err != nil {
400450
log.Fatal("failed to compute map of source hashes", "err", err)
401451
}
@@ -410,17 +460,11 @@ func doGenerate() {
410460
return
411461
}
412462
// Check if files were changed.
413-
postHashes, err := hashSourceFiles()
463+
postHashes, err := hashAllSourceFiles()
414464
if err != nil {
415465
log.Fatal("error computing source tree file hashes", "err", err)
416466
}
417-
updates := []string{}
418-
for path, postHash := range postHashes {
419-
preHash, ok := preHashes[path]
420-
if !ok || preHash != postHash {
421-
updates = append(updates, path)
422-
}
423-
}
467+
updates := compareHashedFilesets(preHashes, postHashes)
424468
for _, updatedFile := range updates {
425469
fmt.Fprintf(os.Stderr, "changed file %s\n", updatedFile)
426470
}
@@ -443,6 +487,8 @@ func doLint(cmdline []string) {
443487
linter := downloadLinter(*cachedir)
444488
lflags := []string{"run", "--config", ".golangci.yml"}
445489
build.MustRunCommandWithOutput(linter, append(lflags, packages...)...)
490+
491+
doGoModTidy()
446492
fmt.Println("You have achieved perfection.")
447493
}
448494

0 commit comments

Comments
 (0)