github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/testdata/script/mod_modinfo.txt (about) 1 # Test to ensure runtime/debug.ReadBuildInfo parses 2 # the modinfo embedded in a binary by the go tool 3 # when module is enabled. 4 env GO111MODULE=on 5 6 cd x 7 go mod edit -require=rsc.io/quote@v1.5.2 8 go mod edit -replace=rsc.io/quote@v1.5.2=rsc.io/quote@v1.0.0 9 10 # Build a binary and ensure that it can output its own debug info. 11 # The debug info should be accessible before main starts (golang.org/issue/29628). 12 go build 13 exec ./x$GOEXE 14 stderr 'mod\s+x\s+\(devel\)' 15 stderr 'dep\s+rsc.io/quote\s+v1.5.2\s+' 16 stderr '=>\s+rsc.io/quote\s+v1.0.0\s+h1:' 17 stderr 'Hello, world.' 18 19 [short] skip 20 21 # Build a binary that accesses its debug info by reading the binary directly 22 # (rather than through debug.ReadBuildInfo). 23 # The debug info should still be present (golang.org/issue/28753). 24 cd unused 25 go build 26 exec ./unused$GOEXE 27 28 -- x/go.mod -- 29 module x 30 31 -- x/lib/lib.go -- 32 // Package lib accesses runtime/debug.modinfo before package main's init 33 // functions have run. 34 package lib 35 36 import "runtime/debug" 37 38 func init() { 39 m, ok := debug.ReadBuildInfo() 40 if !ok { 41 panic("failed debug.ReadBuildInfo") 42 } 43 println("mod", m.Main.Path, m.Main.Version) 44 for _, d := range m.Deps { 45 println("dep", d.Path, d.Version, d.Sum) 46 if r := d.Replace; r != nil { 47 println("=>", r.Path, r.Version, r.Sum) 48 } 49 } 50 } 51 52 -- x/main.go -- 53 package main 54 55 import ( 56 "rsc.io/quote" 57 _ "x/lib" 58 ) 59 60 func main() { 61 println(quote.Hello()) 62 } 63 64 -- x/unused/main.go -- 65 // The unused binary does not access runtime/debug.modinfo. 66 package main 67 68 import ( 69 "bytes" 70 "encoding/hex" 71 "io/ioutil" 72 "log" 73 "os" 74 75 _ "rsc.io/quote" 76 ) 77 78 func main() { 79 b, err := ioutil.ReadFile(os.Args[0]) 80 if err != nil { 81 log.Fatal(err) 82 } 83 84 infoStart, _ := hex.DecodeString("3077af0c9274080241e1c107e6d618e6") 85 if !bytes.Contains(b, infoStart) { 86 log.Fatal("infoStart not found in binary") 87 } 88 log.Println("ok") 89 }