...

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

Documentation: cmd/go/testdata/script

     1[short] skip
     2
     3# If GOROOT_FINAL is set, 'go build -trimpath' bakes that into the resulting
     4# binary instead of GOROOT. Explicitly unset it here.
     5env GOROOT_FINAL=
     6
     7# Set up two identical directories that can be used as GOPATH.
     8env GO111MODULE=on
     9mkdir $WORK/a/src/paths $WORK/b/src/paths
    10cp paths.go $WORK/a/src/paths
    11cp paths.go $WORK/b/src/paths
    12cp overlay.json $WORK/a/src/paths
    13cp overlay.json $WORK/b/src/paths
    14cp go.mod $WORK/a/src/paths/
    15cp go.mod $WORK/b/src/paths/
    16
    17
    18# A binary built without -trimpath should contain the module root dir
    19# and GOROOT for debugging and stack traces.
    20cd $WORK/a/src/paths
    21go build -o $WORK/paths-dbg.exe .
    22exec $WORK/paths-dbg.exe $WORK/paths-dbg.exe
    23stdout 'binary contains module root: true'
    24stdout 'binary contains GOROOT: true'
    25
    26# A binary built with -trimpath should not contain the current workspace
    27# or GOROOT.
    28go build -trimpath -o $WORK/paths-a.exe .
    29exec $WORK/paths-a.exe $WORK/paths-a.exe
    30stdout 'binary contains module root: false'
    31stdout 'binary contains GOROOT: false'
    32
    33# A binary from an external module built with -trimpath should not contain
    34# the current workspace or GOROOT.
    35go get rsc.io/fortune
    36go install -trimpath rsc.io/fortune
    37exec $WORK/paths-a.exe $GOPATH/bin/fortune$GOEXE
    38stdout 'binary contains module root: false'
    39stdout 'binary contains GOROOT: false'
    40go mod edit -droprequire rsc.io/fortune
    41
    42# Two binaries built from identical packages in different directories
    43# should be identical.
    44cd $WORK/b/src/paths
    45go build -trimpath -o $WORK/paths-b.exe
    46cmp -q $WORK/paths-a.exe $WORK/paths-b.exe
    47
    48
    49# Same sequence of tests but with overlays.
    50# A binary built without -trimpath should contain the module root dir
    51# and GOROOT for debugging and stack traces.
    52cd $WORK/a/src/paths
    53go build -overlay overlay.json -o $WORK/paths-dbg.exe ./overlaydir
    54exec $WORK/paths-dbg.exe $WORK/paths-dbg.exe
    55stdout 'binary contains module root: true'
    56stdout 'binary contains GOROOT: true'
    57
    58# A binary built with -trimpath should not contain the current workspace
    59# or GOROOT.
    60go build -overlay overlay.json -trimpath -o $WORK/paths-a.exe ./overlaydir
    61exec $WORK/paths-a.exe $WORK/paths-a.exe
    62stdout 'binary contains module root: false'
    63stdout 'binary contains GOROOT: false'
    64
    65# Two binaries built from identical packages in different directories
    66# should be identical.
    67cd $WORK/b/src/paths
    68go build -overlay overlay.json -trimpath -o $WORK/paths-b.exe ./overlaydir
    69cmp -q $WORK/paths-a.exe $WORK/paths-b.exe
    70
    71
    72# Same sequence of tests but in GOPATH mode.
    73# A binary built without -trimpath should contain GOPATH and GOROOT.
    74env GO111MODULE=off
    75cd $WORK
    76env GOPATH=$WORK/a
    77go build -o paths-dbg.exe paths
    78exec ./paths-dbg.exe paths-dbg.exe
    79stdout 'binary contains GOPATH: true'
    80stdout 'binary contains GOROOT: true'
    81
    82# A binary built with -trimpath should not contain GOPATH or GOROOT.
    83go build -trimpath -o paths-a.exe paths
    84exec ./paths-a.exe paths-a.exe
    85stdout 'binary contains GOPATH: false'
    86stdout 'binary contains GOROOT: false'
    87
    88# Two binaries built from identical packages in different GOPATH roots
    89# should be identical.
    90env GOPATH=$WORK/b
    91go build -trimpath -o paths-b.exe paths
    92cmp -q paths-a.exe paths-b.exe
    93
    94
    95# Same sequence of tests but with gccgo.
    96# gccgo does not support builds in module mode.
    97[!exec:gccgo] stop
    98[cross] stop  # gccgo can't necessarily cross-compile
    99env GOPATH=$WORK/a
   100
   101# A binary built with gccgo without -trimpath should contain the current
   102# GOPATH and GOROOT.
   103go build -compiler=gccgo -o paths-dbg.exe paths
   104exec ./paths-dbg.exe paths-dbg.exe
   105stdout 'binary contains GOPATH: true'
   106stdout 'binary contains GOROOT: false' # gccgo doesn't load std from GOROOT.
   107
   108# A binary built with gccgo with -trimpath should not contain GOPATH or GOROOT.
   109go build -compiler=gccgo -trimpath -o paths-a.exe paths
   110exec ./paths-a.exe paths-a.exe
   111stdout 'binary contains GOPATH: false'
   112stdout 'binary contains GOROOT: false'
   113
   114# Two binaries built from identical packages in different directories
   115# should be identical.
   116env GOPATH=$WORK/b
   117go build -compiler=gccgo -trimpath -o paths-b.exe paths
   118cmp -q paths-a.exe paths-b.exe
   119
   120-- paths.go --
   121package main
   122
   123import (
   124	"bytes"
   125	"fmt"
   126	"io/ioutil"
   127	"log"
   128	"os"
   129	"os/exec"
   130	"path/filepath"
   131	"strings"
   132)
   133
   134func main() {
   135	exe := os.Args[1]
   136	data, err := ioutil.ReadFile(exe)
   137	if err != nil {
   138		log.Fatal(err)
   139	}
   140
   141	if os.Getenv("GO111MODULE") == "on" {
   142		out, err := exec.Command("go", "env", "GOMOD").Output()
   143		if err != nil {
   144			log.Fatal(err)
   145		}
   146		modRoot := filepath.Dir(strings.TrimSpace(string(out)))
   147		check(data, "module root", modRoot)
   148	} else {
   149		check(data, "GOPATH", os.Getenv("GOPATH"))
   150	}
   151	check(data, "GOROOT", os.Getenv("GOROOT"))
   152}
   153
   154func check(data []byte, desc, dir string) {
   155	containsDir := bytes.Contains(data, []byte(dir))
   156	containsSlashDir := bytes.Contains(data, []byte(filepath.ToSlash(dir)))
   157	fmt.Printf("binary contains %s: %v\n", desc, containsDir || containsSlashDir)
   158}
   159-- overlay.json --
   160{ "Replace": { "overlaydir/paths.go": "paths.go" } }
   161-- go.mod --
   162module paths
   163
   164go 1.14

View as plain text