Black Lives Matter. Support the Equal Justice Initiative.

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

View as plain text