diff --git a/main.go b/main.go index 95bce4a..74b464e 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,8 @@ func (f seedFlag) String() string { func (f *seedFlag) Set(s string) error { if s == "random" { + f.random = true // to show the random seed we chose + f.bytes = make([]byte, 16) // random 128 bit seed if _, err := cryptorand.Read(f.bytes); err != nil { return fmt.Errorf("error generating random seed: %v", err) @@ -234,18 +236,20 @@ func main1() int { usage() return 2 } + + // If a random seed was used, the user won't be able to reproduce the + // same output or failure unless we print the random seed we chose. + // If the build failed and a random seed was used, + // the failure might not reproduce with a different seed. + // Print it before we exit. + if flagSeed.random { + fmt.Fprintf(os.Stderr, "-seed chosen at random: %s\n", base64.RawStdEncoding.EncodeToString(flagSeed.bytes)) + } if err := mainErr(args); err != nil { if code, ok := err.(errJustExit); ok { return int(code) } fmt.Fprintln(os.Stderr, err) - - // If the build failed and a random seed was used, - // the failure might not reproduce with a different seed. - // Print it before we exit. - if flagSeed.random { - fmt.Fprintf(os.Stderr, "random seed: %s\n", base64.RawStdEncoding.EncodeToString(flagSeed.bytes)) - } return 1 } return 0 diff --git a/testdata/script/seed.txtar b/testdata/script/seed.txtar index d21ba60..0fd4b38 100644 --- a/testdata/script/seed.txtar +++ b/testdata/script/seed.txtar @@ -63,6 +63,7 @@ cp stderr importedpkg-seed-static-2 # Use a random seed, which should always trigger a full build. garble -seed=random build -v +stderr -count=1 '^-seed chosen at random: .+' stderr -count=1 '^runtime$' stderr -count=1 '^test/main$' exec ./main$exe