Skip to content

Commit b176d66

Browse files
committed
cmd/go: make malformed go.sum a fatal error
In CL 197062, many errors related to go.sum were changed from base.Fatal to error returns. The malformed go.sum error was lost in the process. Currently, when go encounters a malformed go.sum file, go will read the well-formed part of the file and then silently ignore the rest. The motivation behind moving away from base.Fatal was to make the errors show up in -json output. Simply propagating the malformed go.sum error would not achieve this: - For an argument-less 'go mod download -json' with a go>=1.17 module, a malformed go.sum causes an error during LoadModGraph already, before go ever starts downloading modules and printing their JSON. - In other cases, a malformed go.sum would be reported as Error for one of the modules (presumably the one which gets downloaded first) but none of the others. - In either case, 'go mod download' manages to download enough data to succeed on a re-run, making the error look intermittent. Switch the error back to a Fatal one, but give 'go mod tidy' an exception to let it fix broken go.sum files. Fixes #62345
1 parent 972fc05 commit b176d66

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/cmd/go/internal/modfetch/fetch.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ const emptyGoModHash = "h1:G7mAYYxgmS0lVkHyy2hEOLQCFB0DlQFTMLWggykrydY="
504504

505505
// readGoSum parses data, which is the content of file,
506506
// and adds it to goSum.m. The goSum lock must be held.
507-
func readGoSum(dst map[module.Version][]string, file string, data []byte) error {
507+
func readGoSum(dst map[module.Version][]string, file string, data []byte) {
508508
lineno := 0
509509
for len(data) > 0 {
510510
var line []byte
@@ -521,7 +521,12 @@ func readGoSum(dst map[module.Version][]string, file string, data []byte) error
521521
continue
522522
}
523523
if len(f) != 3 {
524-
return fmt.Errorf("malformed go.sum:\n%s:%d: wrong number of fields %v", file, lineno, len(f))
524+
if cfg.CmdName == "mod tidy" {
525+
// ignore malformed line so that go mod tidy can fix go.sum
526+
continue
527+
} else {
528+
base.Fatalf("malformed go.sum:\n%s:%d: wrong number of fields %v\n", file, lineno, len(f))
529+
}
525530
}
526531
if f[2] == emptyGoModHash {
527532
// Old bug; drop it.
@@ -530,7 +535,6 @@ func readGoSum(dst map[module.Version][]string, file string, data []byte) error
530535
mod := module.Version{Path: f[0], Version: f[1]}
531536
dst[mod] = append(dst[mod], f[2])
532537
}
533-
return nil
534538
}
535539

536540
// HaveSum returns true if the go.sum file contains an entry for mod.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
! go mod download
2+
stderr '^malformed go.sum:\n.*/go.sum:3: wrong number of fields 2\n$'
3+
4+
go mod tidy
5+
cmp go.sum go.sum.after-tidy
6+
7+
-- go.mod --
8+
module m
9+
10+
go 1.20
11+
12+
require rsc.io/quote v1.5.2
13+
14+
require (
15+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
16+
rsc.io/sampler v1.3.0 // indirect
17+
)
18+
19+
-- go.sum --
20+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:pvCbr/wm8HzDD3fVywevekufpn6tCGPY3spdHeZJEsw=
21+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
22+
<<<<<<< HEAD
23+
rsc.io/quote v1.5.3-pre1 h1:FoxyADIfUXoYwpK03H8YrG0FWCsiVwFD+VXRUAKWV30=
24+
rsc.io/quote v1.5.3-pre1/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
25+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
26+
rsc.io/sampler v1.3.1 h1:xSM03igIPJPBhXwXb/arCAK2o0xQOmd51BOKx6AtiNE=
27+
rsc.io/sampler v1.3.1/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
28+
=======
29+
rsc.io/quote v1.5.2 h1:3fEykkD9k7lYzXqCYrwGAf7iNhbk4yCjHmKBN9td4L0=
30+
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
31+
rsc.io/sampler v1.3.0 h1:HLGR/BgEtI3r0uymSP/nl2uPLsUnNJX8toRyhfpBTII=
32+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
33+
>>>>>>> 506449d (no-prereleases)
34+
35+
-- main.go --
36+
package main
37+
38+
import (
39+
"fmt"
40+
41+
"rsc.io/quote"
42+
)
43+
44+
func main() {
45+
fmt.Println(quote.Hello())
46+
}
47+
48+
-- go.sum.after-tidy --
49+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:pvCbr/wm8HzDD3fVywevekufpn6tCGPY3spdHeZJEsw=
50+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
51+
rsc.io/quote v1.5.2 h1:3fEykkD9k7lYzXqCYrwGAf7iNhbk4yCjHmKBN9td4L0=
52+
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
53+
rsc.io/sampler v1.3.0 h1:HLGR/BgEtI3r0uymSP/nl2uPLsUnNJX8toRyhfpBTII=
54+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
55+
rsc.io/testonly v1.0.0 h1:K/VWHdO+Jv7woUXG0GzVNx1czBXUt3Ib1deaMn+xk64=
56+
rsc.io/testonly v1.0.0/go.mod h1:OqmGbIFOcF+XrFReLOGZ6BhMM7uMBiQwZsyNmh74SzY=

0 commit comments

Comments
 (0)