...

Text file src/cmd/go/testdata/script/mod_modinfo.txt

Documentation: cmd/go/testdata/script

     1# Test to ensure runtime/debug.ReadBuildInfo parses
     2# the modinfo embedded in a binary by the go tool
     3# when module is enabled.
     4env GO111MODULE=on
     5
     6cd x
     7go mod edit -require=rsc.io/quote@v1.5.2
     8go mod edit -replace=rsc.io/quote@v1.5.2=rsc.io/quote@v1.0.0
     9go 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).
    13go build
    14exec ./x$GOEXE
    15stderr 'mod\s+x\s+\(devel\)'
    16stderr 'dep\s+rsc.io/quote\s+v1.5.2\s+'
    17stderr '=>\s+rsc.io/quote\s+v1.0.0\s+h1:'
    18stderr '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).
    25cd unused
    26go build
    27exec ./unused$GOEXE
    28
    29-- x/go.mod --
    30module x
    31
    32-- x/lib/lib.go --
    33// Package lib accesses runtime/debug.modinfo before package main's init
    34// functions have run.
    35package lib
    36
    37import "runtime/debug"
    38
    39func 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 --
    54package main
    55
    56import (
    57	"rsc.io/quote"
    58	_ "x/lib"
    59)
    60
    61func main() {
    62	println(quote.Hello())
    63}
    64
    65-- x/unused/main.go --
    66// The unused binary does not access runtime/debug.modinfo.
    67package main
    68
    69import (
    70	"bytes"
    71	"encoding/hex"
    72	"log"
    73	"os"
    74
    75	_ "rsc.io/quote"
    76)
    77
    78func 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}

View as plain text