|
7 | 7 | go mod edit -require=rsc.io/ [email protected]
|
8 | 8 | go mod edit -replace=rsc.io/ [email protected]=rsc.io/ [email protected]
|
9 | 9 |
|
10 |
| -go run main.go |
11 |
| - |
12 |
| -stderr 'Hello, world.' |
| 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 |
13 | 14 | stderr 'mod\s+x\s+\(devel\)'
|
14 | 15 | stderr 'dep\s+rsc.io/quote\s+v1.5.2\s+'
|
15 | 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 |
16 | 27 |
|
17 | 28 | -- x/go.mod --
|
18 | 29 | module x
|
19 | 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 | + |
20 | 52 | -- x/main.go --
|
21 | 53 | package main
|
22 | 54 |
|
23 |
| -import "runtime/debug" |
24 |
| -import "rsc.io/quote" |
| 55 | +import ( |
| 56 | + "rsc.io/quote" |
| 57 | + _ "x/lib" |
| 58 | +) |
25 | 59 |
|
26 | 60 | func main() {
|
27 |
| - println(quote.Hello()) |
28 |
| - |
29 |
| - m, ok := debug.ReadBuildInfo() |
30 |
| - if !ok { |
31 |
| - panic("failed debug.ReadBuildInfo") |
32 |
| - } |
33 |
| - println("mod", m.Main.Path, m.Main.Version) |
34 |
| - for _, d := range m.Deps { |
35 |
| - println("dep", d.Path, d.Version, d.Sum) |
36 |
| - if r := d.Replace; r != nil { |
37 |
| - println("=>", r.Path, r.Version, r.Sum) |
38 |
| - } |
39 |
| - } |
| 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") |
40 | 89 | }
|
0 commit comments