From e3a59eae073dadba90d4d7b362dd8b6280603a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 22 Apr 2022 15:44:35 +0100 Subject: [PATCH] add missing context to two unmarshal errors Returning a json or gob error directly to the user is generally not helpful, as it lacks any form of context. For example, from the json unmarshal of "go env -json": $ garble build invalid character 'w' looking for beginning of value Also improve the error when the user ran garble in the wrong way, resulting in no shared gob file. The context is now shorter, and we also include the os.Open error in case it contains any useful details. While here, apply Go tip's gofmt, which reformatted a godoc list. For #523. --- main.go | 2 +- shared.go | 4 ++-- testdata/scripts/basic.txt | 2 +- testdata/scripts/help.txt | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 4ba8cbe..5396fa4 100644 --- a/main.go +++ b/main.go @@ -2058,7 +2058,7 @@ To install Go, see: https://go.dev/doc/install return errJustExit(1) } if err := json.Unmarshal(out, &cache.GoEnv); err != nil { - return err + return fmt.Errorf(`cannot unmarshal from "go env -json": %w`, err) } cache.GOGARBLE = os.Getenv("GOGARBLE") if cache.GOGARBLE != "" { diff --git a/shared.go b/shared.go index d3c3ab5..578ee0f 100644 --- a/shared.go +++ b/shared.go @@ -64,14 +64,14 @@ func loadSharedCache() error { startTime := time.Now() f, err := os.Open(filepath.Join(sharedTempDir, "main-cache.gob")) if err != nil { - return fmt.Errorf(`cannot open shared file, this is most likely due to not running "garble [command]"`) + return fmt.Errorf(`cannot open shared file: %v; did you run "garble [command]"?`, err) } defer func() { debugf("shared cache loaded in %s from %s", debugSince(startTime), f.Name()) }() defer f.Close() if err := gob.NewDecoder(f).Decode(&cache); err != nil { - return err + return fmt.Errorf("cannot decode shared file: %v", err) } return nil } diff --git a/testdata/scripts/basic.txt b/testdata/scripts/basic.txt index cec5446..c6ec453 100644 --- a/testdata/scripts/basic.txt +++ b/testdata/scripts/basic.txt @@ -29,7 +29,7 @@ stdout 'unknown' # Check that we fail if the user used "go build -toolexec garble" instead of "garble build" ! go build -toolexec=garble -o=main$exe garble_main.go -stderr 'not running "garble \[command\]"' +stderr 'run "garble \[command\]"' # Also check that the binary is reproducible. # No packages should be rebuilt either, thanks to the build cache. diff --git a/testdata/scripts/help.txt b/testdata/scripts/help.txt index 884dcf2..bd819ab 100644 --- a/testdata/scripts/help.txt +++ b/testdata/scripts/help.txt @@ -75,7 +75,7 @@ stderr 'must precede command, like: garble -seed=random build \./pkg' [!windows] ! garble /does/not/exist/compile [windows] ! garble C:\does\not\exist\compile -stderr 'not running "garble \[command\]"' +stderr 'run "garble \[command\]"' # Test the version command. Note that test binaries exclude VCS build info, # and we reuse the test binary for garble itself, so that's missing.