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.
pull/527/head
Daniel Martí 2 years ago committed by lu4p
parent 20ff64128e
commit e3a59eae07

@ -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 != "" {

@ -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
}

@ -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.

@ -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.

Loading…
Cancel
Save