Skip to content

Commit 219ee55

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 219ee55

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-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,51 @@
1+
! go mod download
2+
stderr '^malformed go.sum:\n.*go.sum:3: wrong number of fields 5\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+
rsc.io/testonly v1.0.0 // indirect
18+
)
19+
20+
-- go.sum --
21+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:pvCbr/wm8HzDD3fVywevekufpn6tCGPY3spdHeZJEsw=
22+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
23+
rsc.io/quote v1.5.2 # invalid line
24+
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
25+
rsc.io/sampler v1.3.0 h1:HLGR/BgEtI3r0uymSP/nl2uPLsUnNJX8toRyhfpBTII=
26+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
27+
rsc.io/testonly v1.0.0 h1:K/VWHdO+Jv7woUXG0GzVNx1czBXUt3Ib1deaMn+xk64=
28+
rsc.io/testonly v1.0.0/go.mod h1:OqmGbIFOcF+XrFReLOGZ6BhMM7uMBiQwZsyNmh74SzY=
29+
30+
-- main.go --
31+
package main
32+
33+
import (
34+
"fmt"
35+
36+
"rsc.io/quote"
37+
)
38+
39+
func main() {
40+
fmt.Println(quote.Hello())
41+
}
42+
43+
-- go.sum.after-tidy --
44+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:pvCbr/wm8HzDD3fVywevekufpn6tCGPY3spdHeZJEsw=
45+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
46+
rsc.io/quote v1.5.2 h1:3fEykkD9k7lYzXqCYrwGAf7iNhbk4yCjHmKBN9td4L0=
47+
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
48+
rsc.io/sampler v1.3.0 h1:HLGR/BgEtI3r0uymSP/nl2uPLsUnNJX8toRyhfpBTII=
49+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
50+
rsc.io/testonly v1.0.0 h1:K/VWHdO+Jv7woUXG0GzVNx1czBXUt3Ib1deaMn+xk64=
51+
rsc.io/testonly v1.0.0/go.mod h1:OqmGbIFOcF+XrFReLOGZ6BhMM7uMBiQwZsyNmh74SzY=

0 commit comments

Comments
 (0)