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 go mod tidy # populate go.sum
10
11 # Build a binary and ensure that it can output its own debug info.
12 # The debug info should be accessible before main starts (golang.org/issue/29628).
13 go build
14 exec ./x$GOEXE
15 stderr 'mod\s+x\s+\(devel\)'
16 stderr 'dep\s+rsc.io/quote\s+v1.5.2\s+'
17 stderr '=>\s+rsc.io/quote\s+v1.0.0\s+h1:'
18 stderr 'Hello, world.'
19
20 [short] skip
21
22 # Build a binary that accesses its debug info by reading the binary directly
23 # (rather than through debug.ReadBuildInfo).
24 # The debug info should still be present (golang.org/issue/28753).
25 cd unused
26 go build
27 exec ./unused$GOEXE
28
29 -- x/go.mod --
30 module x
31
32 -- x/lib/lib.go --
33 // Package lib accesses runtime/debug.modinfo before package main's init
34 // functions have run.
35 package lib
36
37 import "runtime/debug"
38
39 func init() {
40 m, ok := debug.ReadBuildInfo()
41 if !ok {
42 panic("failed debug.ReadBuildInfo")
43 }
44 println("mod", m.Main.Path, m.Main.Version)
45 for _, d := range m.Deps {
46 println("dep", d.Path, d.Version, d.Sum)
47 if r := d.Replace; r != nil {
48 println("=>", r.Path, r.Version, r.Sum)
49 }
50 }
51 }
52
53 -- x/main.go --
54 package main
55
56 import (
57 "rsc.io/quote"
58 _ "x/lib"
59 )
60
61 func main() {
62 println(quote.Hello())
63 }
64
65 -- x/unused/main.go --
66 // The unused binary does not access runtime/debug.modinfo.
67 package main
68
69 import (
70 "bytes"
71 "encoding/hex"
72 "log"
73 "os"
74
75 _ "rsc.io/quote"
76 )
77
78 func main() {
79 b, err := os.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 }
90
View as plain text