Skip to content

Commit 6232dad

Browse files
marwan-at-workJay Conrod
authored and
Jay Conrod
committed
cmd/go: consistent output for -json failures
When the -json flag is passed to go mod download, the sumdb error is embedded in the json Error field. Other errors for the same command behave this way as well such as module not found. The fix is done by changing base.Fatalf into proper error returns. Fixes #34485 Change-Id: I2727a5c70c7ab03988cad8661894d0f8ec71a768 Reviewed-on: https://go-review.googlesource.com/c/go/+/197062 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 2620467 commit 6232dad

File tree

5 files changed

+57
-26
lines changed

5 files changed

+57
-26
lines changed

src/cmd/go/internal/modfetch/cache.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ func (r *cachingRepo) GoMod(version string) ([]byte, error) {
230230

231231
text, err = r.r.GoMod(version)
232232
if err == nil {
233-
checkGoMod(r.path, version, text)
233+
if err := checkGoMod(r.path, version, text); err != nil {
234+
return cached{text, err}
235+
}
234236
if err := writeDiskGoMod(file, text); err != nil {
235237
fmt.Fprintf(os.Stderr, "go: writing go.mod cache: %v\n", err)
236238
}
@@ -490,7 +492,9 @@ func readDiskGoMod(path, rev string) (file string, data []byte, err error) {
490492
}
491493

492494
if err == nil {
493-
checkGoMod(path, rev, data)
495+
if err := checkGoMod(path, rev, data); err != nil {
496+
return "", nil, err
497+
}
494498
}
495499

496500
return file, data, err

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

+39-22
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ func downloadZip(mod module.Version, zipfile string) (err error) {
250250
if err != nil {
251251
return err
252252
}
253-
checkModSum(mod, hash)
253+
if err := checkModSum(mod, hash); err != nil {
254+
return err
255+
}
254256

255257
if err := renameio.WriteFile(zipfile+"hash", []byte(hash), 0666); err != nil {
256258
return err
@@ -282,21 +284,22 @@ var goSum struct {
282284
}
283285

284286
// initGoSum initializes the go.sum data.
285-
// It reports whether use of go.sum is now enabled.
287+
// The boolean it returns reports whether the
288+
// use of go.sum is now enabled.
286289
// The goSum lock must be held.
287-
func initGoSum() bool {
290+
func initGoSum() (bool, error) {
288291
if GoSumFile == "" {
289-
return false
292+
return false, nil
290293
}
291294
if goSum.m != nil {
292-
return true
295+
return true, nil
293296
}
294297

295298
goSum.m = make(map[module.Version][]string)
296299
goSum.checked = make(map[modSum]bool)
297300
data, err := renameio.ReadFile(GoSumFile)
298301
if err != nil && !os.IsNotExist(err) {
299-
base.Fatalf("go: %v", err)
302+
return false, err
300303
}
301304
goSum.enabled = true
302305
readGoSum(goSum.m, GoSumFile, data)
@@ -314,7 +317,7 @@ func initGoSum() bool {
314317
}
315318
goSum.modverify = alt
316319
}
317-
return true
320+
return true, nil
318321
}
319322

320323
// emptyGoModHash is the hash of a 1-file tree containing a 0-length go.mod.
@@ -324,7 +327,7 @@ const emptyGoModHash = "h1:G7mAYYxgmS0lVkHyy2hEOLQCFB0DlQFTMLWggykrydY="
324327

325328
// readGoSum parses data, which is the content of file,
326329
// and adds it to goSum.m. The goSum lock must be held.
327-
func readGoSum(dst map[module.Version][]string, file string, data []byte) {
330+
func readGoSum(dst map[module.Version][]string, file string, data []byte) error {
328331
lineno := 0
329332
for len(data) > 0 {
330333
var line []byte
@@ -341,7 +344,7 @@ func readGoSum(dst map[module.Version][]string, file string, data []byte) {
341344
continue
342345
}
343346
if len(f) != 3 {
344-
base.Fatalf("go: malformed go.sum:\n%s:%d: wrong number of fields %v", file, lineno, len(f))
347+
return fmt.Errorf("malformed go.sum:\n%s:%d: wrong number of fields %v", file, lineno, len(f))
345348
}
346349
if f[2] == emptyGoModHash {
347350
// Old bug; drop it.
@@ -350,6 +353,7 @@ func readGoSum(dst map[module.Version][]string, file string, data []byte) {
350353
mod := module.Version{Path: f[0], Version: f[1]}
351354
dst[mod] = append(dst[mod], f[2])
352355
}
356+
return nil
353357
}
354358

355359
// checkMod checks the given module's checksum.
@@ -377,7 +381,9 @@ func checkMod(mod module.Version) {
377381
base.Fatalf("verifying %v", module.VersionError(mod, fmt.Errorf("unexpected ziphash: %q", h)))
378382
}
379383

380-
checkModSum(mod, h)
384+
if err := checkModSum(mod, h); err != nil {
385+
base.Fatalf("%s", err)
386+
}
381387
}
382388

383389
// goModSum returns the checksum for the go.mod contents.
@@ -389,37 +395,42 @@ func goModSum(data []byte) (string, error) {
389395

390396
// checkGoMod checks the given module's go.mod checksum;
391397
// data is the go.mod content.
392-
func checkGoMod(path, version string, data []byte) {
398+
func checkGoMod(path, version string, data []byte) error {
393399
h, err := goModSum(data)
394400
if err != nil {
395-
base.Fatalf("verifying %s %s go.mod: %v", path, version, err)
401+
return &module.ModuleError{Path: path, Version: version, Err: fmt.Errorf("verifying go.mod: %v", err)}
396402
}
397403

398-
checkModSum(module.Version{Path: path, Version: version + "/go.mod"}, h)
404+
return checkModSum(module.Version{Path: path, Version: version + "/go.mod"}, h)
399405
}
400406

401407
// checkModSum checks that the recorded checksum for mod is h.
402-
func checkModSum(mod module.Version, h string) {
408+
func checkModSum(mod module.Version, h string) error {
403409
// We lock goSum when manipulating it,
404410
// but we arrange to release the lock when calling checkSumDB,
405411
// so that parallel calls to checkModHash can execute parallel calls
406412
// to checkSumDB.
407413

408414
// Check whether mod+h is listed in go.sum already. If so, we're done.
409415
goSum.mu.Lock()
410-
inited := initGoSum()
416+
inited, err := initGoSum()
417+
if err != nil {
418+
return err
419+
}
411420
done := inited && haveModSumLocked(mod, h)
412421
goSum.mu.Unlock()
413422

414423
if done {
415-
return
424+
return nil
416425
}
417426

418427
// Not listed, so we want to add them.
419428
// Consult checksum database if appropriate.
420429
if useSumDB(mod) {
421430
// Calls base.Fatalf if mismatch detected.
422-
checkSumDB(mod, h)
431+
if err := checkSumDB(mod, h); err != nil {
432+
return err
433+
}
423434
}
424435

425436
// Add mod+h to go.sum, if it hasn't appeared already.
@@ -428,6 +439,7 @@ func checkModSum(mod module.Version, h string) {
428439
addModSumLocked(mod, h)
429440
goSum.mu.Unlock()
430441
}
442+
return nil
431443
}
432444

433445
// haveModSumLocked reports whether the pair mod,h is already listed in go.sum.
@@ -461,22 +473,23 @@ func addModSumLocked(mod module.Version, h string) {
461473

462474
// checkSumDB checks the mod, h pair against the Go checksum database.
463475
// It calls base.Fatalf if the hash is to be rejected.
464-
func checkSumDB(mod module.Version, h string) {
476+
func checkSumDB(mod module.Version, h string) error {
465477
db, lines, err := lookupSumDB(mod)
466478
if err != nil {
467-
base.Fatalf("verifying %s@%s: %v", mod.Path, mod.Version, err)
479+
return module.VersionError(mod, fmt.Errorf("verifying module: %v", err))
468480
}
469481

470482
have := mod.Path + " " + mod.Version + " " + h
471483
prefix := mod.Path + " " + mod.Version + " h1:"
472484
for _, line := range lines {
473485
if line == have {
474-
return
486+
return nil
475487
}
476488
if strings.HasPrefix(line, prefix) {
477-
base.Fatalf("verifying %s@%s: checksum mismatch\n\tdownloaded: %v\n\t%s: %v"+sumdbMismatch, mod.Path, mod.Version, h, db, line[len(prefix)-len("h1:"):])
489+
return module.VersionError(mod, fmt.Errorf("verifying module: checksum mismatch\n\tdownloaded: %v\n\t%s: %v"+sumdbMismatch, h, db, line[len(prefix)-len("h1:"):]))
478490
}
479491
}
492+
return nil
480493
}
481494

482495
// Sum returns the checksum for the downloaded copy of the given module,
@@ -586,7 +599,11 @@ func WriteGoSum() {
586599
func TrimGoSum(keep map[module.Version]bool) {
587600
goSum.mu.Lock()
588601
defer goSum.mu.Unlock()
589-
if !initGoSum() {
602+
inited, err := initGoSum()
603+
if err != nil {
604+
base.Fatalf("%s", err)
605+
}
606+
if !inited {
590607
return
591608
}
592609

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
env GO111MODULE=on
2+
env GOPROXY=$GOPROXY/quiet
3+
env GOSUMDB=$sumdb' '$proxy/sumdb-wrong
4+
5+
# download -json with version should print JSON on sumdb failure
6+
! go mod download -json 'rsc.io/quote@<=v1.5.0'
7+
stdout '"Error": ".*verifying module'
8+
9+
-- go.mod --
10+
module m

src/cmd/go/testdata/script/mod_sumdb.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ env dbname=localhost.localdev/sumdb
99
cp go.mod.orig go.mod
1010
env GOSUMDB=$sumdb' '$proxy/sumdb-wrong
1111
! go get -d rsc.io/quote
12-
stderr 'verifying rsc.io/[email protected]: checksum mismatch'
12+
stderr 'go get rsc.io/quote: rsc.io/quote@v1.5.2: verifying module: checksum mismatch'
1313
stderr 'downloaded: h1:3fEy'
1414
stderr 'localhost.localdev/sumdb: h1:wrong'
1515
stderr 'SECURITY ERROR\nThis download does NOT match the one reported by the checksum server.'

src/cmd/go/testdata/script/mod_sumdb_file_path.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ env GOPATH=$WORK/gopath1
1313
[windows] env GOPROXY=file:///$WORK/sumproxy,https://proxy.golang.org
1414
[!windows] env GOPROXY=file://$WORK/sumproxy,https://proxy.golang.org
1515
! go get -d golang.org/x/[email protected]
16-
stderr '^verifying golang.org/x/[email protected]: golang.org/x/[email protected]: reading file://.*/sumdb/sum.golang.org/lookup/golang.org/x/[email protected]: (no such file or directory|.*cannot find the file specified.*)'
16+
stderr '^go get golang.org/x/[email protected]: golang.org/x/[email protected]: verifying module: golang.org/x/[email protected]: reading file://.*/sumdb/sum.golang.org/lookup/golang.org/x/[email protected]: (no such file or directory|.*cannot find the file specified.*)'
1717

1818
# If the proxy does not claim to support the database,
1919
# checksum verification should fall through to the next proxy,

0 commit comments

Comments
 (0)