github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/testdata/script/build_trimpath.txt (about)

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